| 1 | | import Foundation |
| 2 | | |
| 3 | | /// The numbers of one run, kept as `totals.json` beside `index.html` so a later run can |
| 4 | | /// compare itself with this one (DESIGN.md D5). |
| 5 | | public struct RunTotals: Sendable, Equatable, Codable { |
| 6 | | /// Covered and total lines of a module or file, as written to the file. |
| 7 | | public struct Lines: Sendable, Equatable, Codable { |
| 8 | | public let covered: Int |
| 9 | | public let total: Int |
| 10 | | |
| 11 | 874 | public init(covered: Int, total: Int) { |
| 12 | 874 | self.covered = covered |
| 13 | 874 | self.total = total |
| 14 | 874 | } |
| 15 | | |
| 16 | 776 | init(_ summary: LineSummary) { |
| 17 | 776 | self.init(covered: summary.covered, total: summary.count) |
| 18 | 776 | } |
| 19 | | |
| 20 | 2328 | init?(_ count: CoverageCount?) { |
| 21 | 2328 | guard let count else { return nil } |
| 22 | 86 | self.init(covered: count.covered, total: count.total) |
| 23 | 86 | } |
| 24 | | } |
| 25 | | |
| 26 | | public struct File: Sendable, Equatable, Codable { |
| 27 | | /// Below the sources folder, as in `FileTotals.relativePath`. |
| 28 | | public let path: String |
| 29 | | public let lines: Lines |
| 30 | | /// Nil when the export had no region data; added after version 1 shipped. |
| 31 | | public let regions: Lines? |
| 32 | | /// Nil when the export had no branch data; added after version 1 shipped. |
| 33 | | public let branches: Lines? |
| 34 | | public let conditions: Lines? |
| 35 | | } |
| 36 | | |
| 37 | | public struct Module: Sendable, Equatable, Codable { |
| 38 | | public let name: String |
| 39 | | public let lines: Lines |
| 40 | | public let regions: Lines? |
| 41 | | public let branches: Lines? |
| 42 | | public let conditions: Lines? |
| 43 | | public let files: [File] |
| 44 | | } |
| 45 | | |
| 46 | | public struct TestGroup: Sendable, Equatable, Codable { |
| 47 | | public let label: String? |
| 48 | | public let counts: TestCounts |
| 49 | | |
| 50 | 9 | public init(label: String?, counts: TestCounts) { |
| 51 | 9 | self.label = label |
| 52 | 9 | self.counts = counts |
| 53 | 9 | } |
| 54 | | } |
| 55 | | |
| 56 | | /// Counts by test group; absent in older version 1 files and count-only runs. |
| 57 | | public let testGroups: [TestGroup]? |
| 58 | | |
| 59 | | /// The file name, beside `index.html`. |
| 60 | | public static let fileName = "totals.json" |
| 61 | | /// Raised when a field changes meaning or goes away; adding a field keeps it. |
| 62 | | public static let currentFormatVersion = 1 |
| 63 | | |
| 64 | | public let formatVersion: Int |
| 65 | | /// Nil when the run was not given a commit. |
| 66 | | public let commit: String? |
| 67 | | /// The commit's first message line; nil when the run was not given one. Added after |
| 68 | | /// version 1 shipped, so older files lack it. |
| 69 | | public let subject: String? |
| 70 | | /// When the report was built, to the second. |
| 71 | | public let date: Date |
| 72 | | /// Nil when the run had no test counts. |
| 73 | | public let tests: TestCounts? |
| 74 | | /// Whether this run wrote tests.html; absent in older version 1 files. |
| 75 | | public let hasTestsPage: Bool? |
| 76 | | public let lines: Lines |
| 77 | | /// Regions; nil when the export had none, and in files written before they were |
| 78 | | /// recorded. |
| 79 | | public let regions: Lines? |
| 80 | | /// Branch outcomes and MC/DC conditions; nil when the export had none. |
| 81 | | public let branches: Lines? |
| 82 | | public let conditions: Lines? |
| 83 | | /// Ordered by name; each module's files ordered by path. |
| 84 | | public let modules: [Module] |
| 85 | | /// Why the run has no coverage; nil for a run with coverage. Then `lines` is zero of |
| 86 | | /// zero and there are no modules. Added after version 1 shipped. |
| 87 | | public let unavailable: String? |
| 88 | | |
| 89 | | public init( |
| 90 | | commit: String?, subject: String? = nil, date: Date, tests: TestCounts?, |
| 91 | | files: [FileTotals], unavailable: String? = nil, hasTestsPage: Bool? = nil, |
| 92 | | testGroups: [TestGroup]? = nil |
| 93 | 258 | ) { |
| 94 | 258 | let modules = ModuleTotals.grouping(files) |
| 95 | 258 | self.formatVersion = Self.currentFormatVersion |
| 96 | 258 | self.commit = commit |
| 97 | 258 | self.subject = subject |
| 98 | 258 | self.date = date |
| 99 | 258 | self.testGroups = testGroups |
| 100 | 258 | self.tests = tests |
| 101 | 258 | self.hasTestsPage = hasTestsPage |
| 102 | 267 | self.lines = Lines(LineSummary(adding: files.map(\.lines))) |
| 103 | 267 | self.regions = Lines(CoverageCount(addingPresent: files.map(\.regions))) |
| 104 | 267 | self.branches = Lines(CoverageCount(addingPresent: files.map(\.branches))) |
| 105 | 267 | self.conditions = Lines(CoverageCount(addingPresent: files.map(\.conditions))) |
| 106 | 258 | self.unavailable = unavailable |
| 107 | 258 | self.modules = modules.map { module in |
| 108 | 251 | Module( |
| 109 | 251 | name: module.name, lines: Lines(module.lines), regions: Lines(module.regions), |
| 110 | 251 | branches: Lines(module.branches), |
| 111 | 251 | conditions: Lines(module.conditions), |
| 112 | 267 | files: module.files.map { |
| 113 | 267 | File( |
| 114 | 267 | path: $0.relativePath, lines: Lines($0.lines), regions: Lines($0.regions), |
| 115 | 267 | branches: Lines($0.branches), conditions: Lines($0.conditions)) |
| 116 | 267 | }) |
| 117 | 251 | } |
| 118 | 258 | } |
| 119 | | |
| 120 | | /// A run whose tests failed or whose profile data is missing: it records only when |
| 121 | | /// it was built and why it has no numbers. |
| 122 | 25 | public init(unavailableBecause reason: String, commit: String?, subject: String?, date: Date) { |
| 123 | 25 | self.init( |
| 124 | 25 | commit: commit, subject: subject, date: date, tests: nil, files: [], |
| 125 | 25 | unavailable: reason) |
| 126 | 25 | } |
| 127 | | |
| 128 | | /// For readers that compare with this run. |
| 129 | | /// - Throws: `RunTotalsError.noCoverage` when the run has no coverage. |
| 130 | 2 | public func requireCoverage() throws { |
| 131 | 2 | if let unavailable { throw RunTotalsError.noCoverage(reason: unavailable) } |
| 132 | 1 | } |
| 133 | | |
| 134 | | /// The file's text: pretty-printed with sorted keys, so equal runs give equal bytes. |
| 135 | 39 | public func json() throws -> Data { |
| 136 | 39 | let encoder = JSONEncoder() |
| 137 | 39 | encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] |
| 138 | 39 | encoder.dateEncodingStrategy = .iso8601 |
| 139 | 39 | return try encoder.encode(self) |
| 140 | 39 | } |
| 141 | | |
| 142 | | /// - Throws: `RunTotalsError.unsupportedVersion` when the file's `formatVersion` is |
| 143 | | /// not the current one, and `DecodingError` when it is malformed. |
| 144 | 21 | public init(json: Data) throws { |
| 145 | 21 | let decoder = JSONDecoder() |
| 146 | 21 | decoder.dateDecodingStrategy = .iso8601 |
| 147 | 21 | let totals = try decoder.decode(Self.self, from: json) |
| 148 | 21 | guard totals.formatVersion == Self.currentFormatVersion else { |
| 149 | 1 | throw RunTotalsError.unsupportedVersion(totals.formatVersion) |
| 150 | 20 | } |
| 151 | 20 | self = totals |
| 152 | 20 | } |
| 153 | | } |
| 154 | | |
| 155 | | public enum RunTotalsError: Error, Equatable { |
| 156 | | case unsupportedVersion(Int) |
| 157 | | /// The run has no coverage, so there is nothing to compare with. |
| 158 | | case noCoverage(reason: String) |
| 159 | | } |
| 160 | | |
| 161 | | extension TestCounts: Codable { |
| 162 | | private enum CodingKeys: String, CodingKey { |
| 163 | | case total, failed, skipped |
| 164 | | } |
| 165 | | |
| 166 | 12 | public init(from decoder: any Decoder) throws { |
| 167 | 12 | let values = try decoder.container(keyedBy: CodingKeys.self) |
| 168 | 12 | self.init( |
| 169 | 12 | total: try values.decode(Int.self, forKey: .total), |
| 170 | 12 | failed: try values.decode(Int.self, forKey: .failed), |
| 171 | 12 | skipped: try values.decode(Int.self, forKey: .skipped)) |
| 172 | 12 | } |
| 173 | | |
| 174 | 18 | public func encode(to encoder: any Encoder) throws { |
| 175 | 18 | var values = encoder.container(keyedBy: CodingKeys.self) |
| 176 | 18 | try values.encode(total, forKey: .total) |
| 177 | 18 | try values.encode(failed, forKey: .failed) |
| 178 | 18 | try values.encode(skipped, forKey: .skipped) |
| 179 | 18 | } |
| 180 | | } |