1import Foundation
2
3/// Every file of the coverage site built from one export: the index, the stylesheet and
4/// a page per source file, plus the job summary. Values only; writing them is the
5/// command's job.
6public struct CoverageSite: Sendable {
7 public struct File: Sendable, Equatable {
8 /// Relative to the site's root, such as `coverage/BrowserKit/PathField.swift.html`.
9 public let path: String
10 public let contents: String
11 }
12
13 public let files: [File]
14 public let jobSummary: String
15
16 /// - Parameters:
17 /// - sourcesRoot: the absolute folder every file in the export must sit under, such
18 /// as `<checkout>/Sources`; pages are named relative to it.
19 /// - title: the site's name, shown as given in the index heading and page titles.
20 /// - tests: the run's tests; with results, the site gets `tests.html` too.
21 /// - subject: the commit's first message line; shown in headers and `totals.json`.
22 /// - historyPath: the History destination relative to the report root, if available.
23 /// - date: when the report was built; recorded in `totals.json`.
24 /// - baseline: an earlier run's totals; the index and the job summary then show each change.
25 /// - publishedRuns: earlier published runs for the trend; a named baseline is used
26 /// when no published runs are supplied. The current commit replaces its earlier point.
27 /// - readSource: the text of the source file at an absolute path, as it was when
28 /// the tests ran.
29 /// - Throws: `CoverageSiteError` when a file lies outside `sourcesRoot` or is shorter
30 /// than its coverage reaches, and whatever `readSource` throws.
31 public init(
32 export: CoverageExport, sourcesRoot: String, title: String, tests: TestRun?,
33 commit: String?, subject: String? = nil, date: Date, baseline: RunTotals? = nil,
34 historyPath: String? = nil, publishedRuns: [RunTotals] = [],
35 readSource: (String) throws -> String
3620 ) throws {
3720 let run = ReportHeader(commit: commit, subject: subject, historyPath: historyPath)
3820 let root = sourcesRoot.hasSuffix("/") ? sourcesRoot : sourcesRoot + "/"
3920 var files: [File] = []
4020 let hasTestsPage =
4120 if case .results = tests { true } else { false }
4220 var totals: [FileTotals] = []
4335 for file in export.files {
4435 let (page, lines) = try Self.sourcePage(
4535 of: file, root: root, title: title, hasTestsPage: hasTestsPage, run: run,
4635 readSource: readSource)
4732 files.append(File(path: page.sitePath, contents: page.html))
4832 totals.append(
4932 FileTotals(
5032 relativePath: page.relativePath, lines: LineSummary(lines: lines),
5132 branches: file.branchCount, conditions: file.conditionCount))
5232 }
5317 let testGroups: [RunTotals.TestGroup]? =
5417 if case .results(let results) = tests {
555 results.groups.map { RunTotals.TestGroup(label: $0.label, counts: $0.counts) }
5613 } else { nil }
5717 let runTotals = RunTotals(
5817 commit: commit, subject: subject, date: date, tests: tests?.counts, files: totals,
5917 hasTestsPage: hasTestsPage, testGroups: testGroups)
6017 let previousRuns = publishedRuns.isEmpty ? baseline.map { [$0] } ?? [] : publishedRuns
6117 let trend = CoverageTrend(
6217 runs: previousRuns.filter {
634 commit == nil || $0.commit != commit
644 } + [runTotals])
6517 files.append(
6617 File(
6717 path: IndexPage.sitePath,
6817 contents: IndexPage(
6917 title: title, files: totals, tests: tests, commit: commit, baseline: baseline,
7017 subject: subject, historyPath: historyPath, trend: trend
7117 ).html)
7217 )
7317 files.append(
7417 File(
7517 path: RunTotals.fileName,
7617 contents: String(decoding: try runTotals.json(), as: UTF8.self)))
7717 files.append(File(path: SiteStylesheet.fileName, contents: SiteStylesheet.contents))
7817 if case .results(let results) = tests {
794 files.append(
804 File(
814 path: TestsPage.sitePath,
824 contents: TestsPage(title: title, results: results, run: run).html))
834 }
8417 self.files = files
8517 self.jobSummary =
8617 JobSummary(
8717 files: totals, tests: tests?.counts, commit: commit, baseline: baseline,
8817 testGroups: testGroups ?? []
8917 ).markdown
9017 }
91
92 private static func sourcePage(
93 of file: FileCoverage, root: String, title: String, hasTestsPage: Bool, run: ReportHeader,
94 readSource: (String) throws -> String
9535 ) throws -> (SourcePage, [LineCoverage]) {
9635 guard file.path.hasPrefix(root) else {
971 throw CoverageSiteError.outsideSources(path: file.path)
9834 }
9934 let source = try readSource(file.path)
10033 let lineCount = SourcePage.lineCount(of: source)
10166 let coveredLine = file.segments.map(\.line).max() ?? 0
10233 guard coveredLine <= lineCount else {
1031 throw CoverageSiteError.sourceChanged(
1041 path: file.path, sourceLines: lineCount, coveredLine: coveredLine)
10532 }
10632 let lines = LineCoverage.lines(from: file.segments, lineCount: lineCount)
10732 let page = SourcePage(
10832 relativePath: String(file.path.dropFirst(root.count)), siteTitle: title,
10932 hasTestsPage: hasTestsPage, source: source, coverage: lines, run: run)
11032 return (page, lines)
11135 }
112}
113
114public enum CoverageSiteError: Error, Equatable {
115 case outsideSources(path: String)
116 /// The source has fewer lines than the export's segments reach, so it changed after
117 /// the tests ran and the marks would land on the wrong lines. A file that grew can't
118 /// be caught this way; the export and the checkout must come from the same build.
119 case sourceChanged(path: String, sourceLines: Int, coveredLine: Int)
120}