| 1 | | import 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. |
| 6 | | public 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 |
| 32 | 17 | ) throws { |
| 33 | 17 | let root = sourcesRoot.hasSuffix("/") ? sourcesRoot : sourcesRoot + "/" |
| 34 | 17 | var files: [File] = [] |
| 35 | 17 | let hasTestsPage = |
| 36 | 17 | if case .results = tests { true } else { false } |
| 37 | 17 | var totals: [FileTotals] = [] |
| 38 | 29 | for file in export.files { |
| 39 | 29 | let (page, lines) = try Self.sourcePage( |
| 40 | 29 | of: file, root: root, title: title, hasTestsPage: hasTestsPage, |
| 41 | 29 | readSource: readSource) |
| 42 | 26 | files.append(File(path: page.sitePath, contents: page.html)) |
| 43 | 26 | totals.append( |
| 44 | 26 | FileTotals( |
| 45 | 26 | relativePath: page.relativePath, lines: LineSummary(lines: lines), |
| 46 | 26 | branches: file.branchCount, conditions: file.conditionCount)) |
| 47 | 26 | } |
| 48 | 14 | files.append( |
| 49 | 14 | File( |
| 50 | 14 | path: IndexPage.sitePath, |
| 51 | 14 | contents: IndexPage( |
| 52 | 14 | title: title, files: totals, tests: tests, commit: commit, baseline: baseline |
| 53 | 14 | ).html) |
| 54 | 14 | ) |
| 55 | 14 | let runTotals = RunTotals( |
| 56 | 14 | commit: commit, subject: subject, date: date, tests: tests?.counts, files: totals) |
| 57 | 14 | files.append( |
| 58 | 14 | File( |
| 59 | 14 | path: RunTotals.fileName, |
| 60 | 14 | contents: String(decoding: try runTotals.json(), as: UTF8.self))) |
| 61 | 14 | files.append(File(path: SiteStylesheet.fileName, contents: SiteStylesheet.contents)) |
| 62 | 14 | if case .results(let results) = tests { |
| 63 | 2 | files.append( |
| 64 | 2 | File( |
| 65 | 2 | path: TestsPage.sitePath, |
| 66 | 2 | contents: TestsPage(title: title, results: results).html)) |
| 67 | 2 | } |
| 68 | 14 | self.files = files |
| 69 | 14 | self.jobSummary = |
| 70 | 14 | JobSummary( |
| 71 | 14 | files: totals, tests: tests?.counts, commit: commit, baseline: baseline |
| 72 | 14 | ).markdown |
| 73 | 14 | } |
| 74 | | |
| 75 | | private static func sourcePage( |
| 76 | | of file: FileCoverage, root: String, title: String, hasTestsPage: Bool, |
| 77 | | readSource: (String) throws -> String |
| 78 | 29 | ) throws -> (SourcePage, [LineCoverage]) { |
| 79 | 29 | guard file.path.hasPrefix(root) else { |
| 80 | 1 | throw CoverageSiteError.outsideSources(path: file.path) |
| 81 | 28 | } |
| 82 | 28 | let source = try readSource(file.path) |
| 83 | 27 | let lineCount = SourcePage.lineCount(of: source) |
| 84 | 54 | let coveredLine = file.segments.map(\.line).max() ?? 0 |
| 85 | 27 | guard coveredLine <= lineCount else { |
| 86 | 1 | throw CoverageSiteError.sourceChanged( |
| 87 | 1 | path: file.path, sourceLines: lineCount, coveredLine: coveredLine) |
| 88 | 26 | } |
| 89 | 26 | let lines = LineCoverage.lines(from: file.segments, lineCount: lineCount) |
| 90 | 26 | let page = SourcePage( |
| 91 | 26 | relativePath: String(file.path.dropFirst(root.count)), siteTitle: title, |
| 92 | 26 | hasTestsPage: hasTestsPage, source: source, coverage: lines) |
| 93 | 26 | return (page, lines) |
| 94 | 29 | } |
| 95 | | } |
| 96 | | |
| 97 | | public 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 | | } |