| 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 | | /// 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 |
| 39 | 30 | ) throws { |
| 40 | 30 | let run = ReportHeader(commit: commit, subject: subject, historyPath: historyPath) |
| 41 | 30 | let root = sourcesRoot.hasSuffix("/") ? sourcesRoot : sourcesRoot + "/" |
| 42 | 57 | let reported = export.files.filter { $0.path.hasPrefix(root) } |
| 43 | 30 | guard !reported.isEmpty else { |
| 44 | 1 | throw CoverageSiteError.noFilesInSources(folder: sourcesRoot) |
| 45 | 29 | } |
| 46 | 29 | var files: [File] = [] |
| 47 | 29 | let hasTestsPage = |
| 48 | 29 | if case .results = tests { true } else { false } |
| 49 | 29 | var totals: [FileTotals] = [] |
| 50 | 52 | for file in reported { |
| 51 | 52 | let (page, lines) = try Self.sourcePage( |
| 52 | 52 | of: file, root: root, title: title, hasTestsPage: hasTestsPage, run: run, |
| 53 | 52 | readSource: readSource) |
| 54 | 50 | files.append(File(path: page.sitePath, contents: page.html)) |
| 55 | 50 | totals.append( |
| 56 | 50 | FileTotals( |
| 57 | 50 | relativePath: page.relativePath, lines: LineSummary(lines: lines), |
| 58 | 50 | regions: file.regionCount, branches: file.branchCount, |
| 59 | 50 | conditions: file.conditionCount)) |
| 60 | 50 | } |
| 61 | 27 | let testGroups: [RunTotals.TestGroup]? = |
| 62 | 27 | if case .results(let results) = tests { |
| 63 | 5 | results.groups.map { RunTotals.TestGroup(label: $0.label, counts: $0.counts) } |
| 64 | 23 | } else { nil } |
| 65 | 27 | let runTotals = RunTotals( |
| 66 | 27 | commit: commit, subject: subject, date: date, tests: tests?.counts, files: totals, |
| 67 | 27 | hasTestsPage: hasTestsPage, testGroups: testGroups) |
| 68 | 27 | let previousRuns = publishedRuns.isEmpty ? baseline.map { [$0] } ?? [] : publishedRuns |
| 69 | 27 | let trend = CoverageTrend( |
| 70 | 27 | runs: previousRuns.filter { |
| 71 | 4 | commit == nil || $0.commit != commit |
| 72 | 4 | } + [runTotals]) |
| 73 | 27 | files.append( |
| 74 | 27 | File( |
| 75 | 27 | path: IndexPage.sitePath, |
| 76 | 27 | contents: IndexPage( |
| 77 | 27 | title: title, files: totals, tests: tests, commit: commit, baseline: baseline, |
| 78 | 27 | subject: subject, historyPath: historyPath, trend: trend |
| 79 | 27 | ).html) |
| 80 | 27 | ) |
| 81 | 27 | files.append( |
| 82 | 27 | File( |
| 83 | 27 | path: RunTotals.fileName, |
| 84 | 27 | contents: String(decoding: try runTotals.json(), as: UTF8.self))) |
| 85 | 27 | files.append(File(path: SiteStylesheet.fileName, contents: SiteStylesheet.contents)) |
| 86 | 27 | if case .results(let results) = tests { |
| 87 | 4 | files.append( |
| 88 | 4 | File( |
| 89 | 4 | path: TestsPage.sitePath, |
| 90 | 4 | contents: TestsPage(title: title, results: results, run: run).html)) |
| 91 | 4 | } |
| 92 | 27 | self.files = files |
| 93 | 27 | self.filesOutsideSources = export.files.count - reported.count |
| 94 | 27 | self.jobSummary = |
| 95 | 27 | JobSummary( |
| 96 | 27 | files: totals, tests: tests?.counts, commit: commit, baseline: baseline, |
| 97 | 27 | testGroups: testGroups ?? [] |
| 98 | 27 | ).markdown |
| 99 | 27 | } |
| 100 | | |
| 101 | | private static func sourcePage( |
| 102 | | of file: FileCoverage, root: String, title: String, hasTestsPage: Bool, run: ReportHeader, |
| 103 | | readSource: (String) throws -> String |
| 104 | 52 | ) throws -> (SourcePage, [LineCoverage]) { |
| 105 | 52 | let source = try readSource(file.path) |
| 106 | 51 | let lineCount = SourcePage.lineCount(of: source) |
| 107 | 86 | let coveredLine = file.segments.map(\.line).max() ?? 0 |
| 108 | 51 | guard coveredLine <= lineCount else { |
| 109 | 1 | throw CoverageSiteError.sourceChanged( |
| 110 | 1 | path: file.path, sourceLines: lineCount, coveredLine: coveredLine) |
| 111 | 50 | } |
| 112 | 50 | let lines = LineCoverage.lines(from: file.segments, lineCount: lineCount) |
| 113 | 50 | let page = SourcePage( |
| 114 | 50 | relativePath: String(file.path.dropFirst(root.count)), siteTitle: title, |
| 115 | 50 | hasTestsPage: hasTestsPage, source: source, coverage: lines, |
| 116 | 50 | regions: file.regionCount, run: run) |
| 117 | 50 | return (page, lines) |
| 118 | 52 | } |
| 119 | | } |
| 120 | | |
| 121 | | public 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 | | } |