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 /// How many of the export's files lie outside the sources folder, so aren't reported.
16 public let filesOutsideSources: Int
17
18 /// - Parameters:
19 /// - sourcesRoot: the absolute folder whose files are reported, such as
20 /// `<checkout>/Sources`; pages are named relative to it. The export's other files
21 /// are left out and counted in `filesOutsideSources`.
22 /// - title: the site's name, shown as given in the index heading and page titles.
23 /// - tests: the run's tests; with results, the site gets `tests.html` too.
24 /// - subject: the commit's first message line; shown in headers and `totals.json`.
25 /// - historyPath: the History destination relative to the report root, if available.
26 /// - date: when the report was built; recorded in `totals.json`.
27 /// - baseline: an earlier run's totals; the index and the job summary then show each change.
28 /// - publishedRuns: earlier published runs for the trend; a named baseline is used
29 /// when no published runs are supplied. The current commit replaces its earlier point.
30 /// - readSource: the text of the source file at an absolute path, as it was when
31 /// the tests ran.
32 /// - Throws: `CoverageSiteError` when no file lies in `sourcesRoot` or one is shorter
33 /// than its coverage reaches, and whatever `readSource` throws.
34 public init(
35 export: CoverageExport, sourcesRoot: String, title: String, tests: TestRun?,
36 commit: String?, subject: String? = nil, date: Date, baseline: RunTotals? = nil,
37 historyPath: String? = nil, publishedRuns: [RunTotals] = [],
38 readSource: (String) throws -> String
3930 ) throws {
4030 let run = ReportHeader(commit: commit, subject: subject, historyPath: historyPath)
4130 let root = sourcesRoot.hasSuffix("/") ? sourcesRoot : sourcesRoot + "/"
4257 let reported = export.files.filter { $0.path.hasPrefix(root) }
4330 guard !reported.isEmpty else {
441 throw CoverageSiteError.noFilesInSources(folder: sourcesRoot)
4529 }
4629 var files: [File] = []
4729 let hasTestsPage =
4829 if case .results = tests { true } else { false }
4929 var totals: [FileTotals] = []
5052 for file in reported {
5152 let (page, lines) = try Self.sourcePage(
5252 of: file, root: root, title: title, hasTestsPage: hasTestsPage, run: run,
5352 readSource: readSource)
5450 files.append(File(path: page.sitePath, contents: page.html))
5550 totals.append(
5650 FileTotals(
5750 relativePath: page.relativePath, lines: LineSummary(lines: lines),
5850 regions: file.regionCount, branches: file.branchCount,
5950 conditions: file.conditionCount))
6050 }
6127 let testGroups: [RunTotals.TestGroup]? =
6227 if case .results(let results) = tests {
635 results.groups.map { RunTotals.TestGroup(label: $0.label, counts: $0.counts) }
6423 } else { nil }
6527 let runTotals = RunTotals(
6627 commit: commit, subject: subject, date: date, tests: tests?.counts, files: totals,
6727 hasTestsPage: hasTestsPage, testGroups: testGroups)
6827 let previousRuns = publishedRuns.isEmpty ? baseline.map { [$0] } ?? [] : publishedRuns
6927 let trend = CoverageTrend(
7027 runs: previousRuns.filter {
714 commit == nil || $0.commit != commit
724 } + [runTotals])
7327 files.append(
7427 File(
7527 path: IndexPage.sitePath,
7627 contents: IndexPage(
7727 title: title, files: totals, tests: tests, commit: commit, baseline: baseline,
7827 subject: subject, historyPath: historyPath, trend: trend
7927 ).html)
8027 )
8127 files.append(
8227 File(
8327 path: RunTotals.fileName,
8427 contents: String(decoding: try runTotals.json(), as: UTF8.self)))
8527 files.append(File(path: SiteStylesheet.fileName, contents: SiteStylesheet.contents))
8627 if case .results(let results) = tests {
874 files.append(
884 File(
894 path: TestsPage.sitePath,
904 contents: TestsPage(title: title, results: results, run: run).html))
914 }
9227 self.files = files
9327 self.filesOutsideSources = export.files.count - reported.count
9427 self.jobSummary =
9527 JobSummary(
9627 files: totals, tests: tests?.counts, commit: commit, baseline: baseline,
9727 testGroups: testGroups ?? []
9827 ).markdown
9927 }
100
101 private static func sourcePage(
102 of file: FileCoverage, root: String, title: String, hasTestsPage: Bool, run: ReportHeader,
103 readSource: (String) throws -> String
10452 ) throws -> (SourcePage, [LineCoverage]) {
10552 let source = try readSource(file.path)
10651 let lineCount = SourcePage.lineCount(of: source)
10786 let coveredLine = file.segments.map(\.line).max() ?? 0
10851 guard coveredLine <= lineCount else {
1091 throw CoverageSiteError.sourceChanged(
1101 path: file.path, sourceLines: lineCount, coveredLine: coveredLine)
11150 }
11250 let lines = LineCoverage.lines(from: file.segments, lineCount: lineCount)
11350 let page = SourcePage(
11450 relativePath: String(file.path.dropFirst(root.count)), siteTitle: title,
11550 hasTestsPage: hasTestsPage, source: source, coverage: lines,
11650 regions: file.regionCount, run: run)
11750 return (page, lines)
11852 }
119}
120
121public enum CoverageSiteError: Error, Equatable {
122 /// The export has no file to report: the sources folder is wrong, or nothing in it
123 /// was measured.
124 case noFilesInSources(folder: String)
125 /// The source has fewer lines than the export's segments reach, so it changed after
126 /// the tests ran and the marks would land on the wrong lines. A file that grew can't
127 /// be caught this way; the export and the checkout must come from the same build.
128 case sourceChanged(path: String, sourceLines: Int, coveredLine: Int)
129}