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; recorded in `totals.json`.
22 /// - date: when the report was built; recorded in `totals.json`.
23 /// - baseline: an earlier run's totals; the index and the job summary then show each change.
24 /// - readSource: the text of the source file at an absolute path, as it was when
25 /// the tests ran.
26 /// - Throws: `CoverageSiteError` when a file lies outside `sourcesRoot` or is shorter
27 /// than its coverage reaches, and whatever `readSource` throws.
28 public init(
29 export: CoverageExport, sourcesRoot: String, title: String, tests: TestRun?,
30 commit: String?, subject: String? = nil, date: Date, baseline: RunTotals? = nil,
31 readSource: (String) throws -> String
3217 ) throws {
3317 let root = sourcesRoot.hasSuffix("/") ? sourcesRoot : sourcesRoot + "/"
3417 var files: [File] = []
3517 let hasTestsPage =
3617 if case .results = tests { true } else { false }
3717 var totals: [FileTotals] = []
3829 for file in export.files {
3929 let (page, lines) = try Self.sourcePage(
4029 of: file, root: root, title: title, hasTestsPage: hasTestsPage,
4129 readSource: readSource)
4226 files.append(File(path: page.sitePath, contents: page.html))
4326 totals.append(
4426 FileTotals(
4526 relativePath: page.relativePath, lines: LineSummary(lines: lines),
4626 branches: file.branchCount, conditions: file.conditionCount))
4726 }
4814 files.append(
4914 File(
5014 path: IndexPage.sitePath,
5114 contents: IndexPage(
5214 title: title, files: totals, tests: tests, commit: commit, baseline: baseline
5314 ).html)
5414 )
5514 let runTotals = RunTotals(
5614 commit: commit, subject: subject, date: date, tests: tests?.counts, files: totals)
5714 files.append(
5814 File(
5914 path: RunTotals.fileName,
6014 contents: String(decoding: try runTotals.json(), as: UTF8.self)))
6114 files.append(File(path: SiteStylesheet.fileName, contents: SiteStylesheet.contents))
6214 if case .results(let results) = tests {
632 files.append(
642 File(
652 path: TestsPage.sitePath,
662 contents: TestsPage(title: title, results: results).html))
672 }
6814 self.files = files
6914 self.jobSummary =
7014 JobSummary(
7114 files: totals, tests: tests?.counts, commit: commit, baseline: baseline
7214 ).markdown
7314 }
74
75 private static func sourcePage(
76 of file: FileCoverage, root: String, title: String, hasTestsPage: Bool,
77 readSource: (String) throws -> String
7829 ) throws -> (SourcePage, [LineCoverage]) {
7929 guard file.path.hasPrefix(root) else {
801 throw CoverageSiteError.outsideSources(path: file.path)
8128 }
8228 let source = try readSource(file.path)
8327 let lineCount = SourcePage.lineCount(of: source)
8454 let coveredLine = file.segments.map(\.line).max() ?? 0
8527 guard coveredLine <= lineCount else {
861 throw CoverageSiteError.sourceChanged(
871 path: file.path, sourceLines: lineCount, coveredLine: coveredLine)
8826 }
8926 let lines = LineCoverage.lines(from: file.segments, lineCount: lineCount)
9026 let page = SourcePage(
9126 relativePath: String(file.path.dropFirst(root.count)), siteTitle: title,
9226 hasTestsPage: hasTestsPage, source: source, coverage: lines)
9326 return (page, lines)
9429 }
95}
96
97public enum CoverageSiteError: Error, Equatable {
98 case outsideSources(path: String)
99 /// The source has fewer lines than the export's segments reach, so it changed after
100 /// the tests ran and the marks would land on the wrong lines. A file that grew can't
101 /// be caught this way; the export and the checkout must come from the same build.
102 case sourceChanged(path: String, sourceLines: Int, coveredLine: Int)
103}