1import 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).
5public 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
11236 public init(covered: Int, total: Int) {
12236 self.covered = covered
13236 self.total = total
14236 }
15
16215 init(_ summary: LineSummary) {
17215 self.init(covered: summary.covered, total: summary.count)
18215 }
19
20429 init?(_ count: CoverageCount?) {
21429 guard let count else { return nil }
2212 self.init(covered: count.covered, total: count.total)
2312 }
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 branch data; added after version 1 shipped.
31 public let branches: Lines?
32 public let conditions: Lines?
33 }
34
35 public struct Module: Sendable, Equatable, Codable {
36 public let name: String
37 public let lines: Lines
38 public let branches: Lines?
39 public let conditions: Lines?
40 public let files: [File]
41 }
42
43 /// The file name, beside `index.html`.
44 public static let fileName = "totals.json"
45 /// Raised when a field changes meaning or goes away; adding a field keeps it.
46 public static let currentFormatVersion = 1
47
48 public let formatVersion: Int
49 /// Nil when the run was not given a commit.
50 public let commit: String?
51 /// The commit's first message line; nil when the run was not given one. Added after
52 /// version 1 shipped, so older files lack it.
53 public let subject: String?
54 /// When the report was built, to the second.
55 public let date: Date
56 /// Nil when the run had no test counts.
57 public let tests: TestCounts?
58 public let lines: Lines
59 /// Branch outcomes and MC/DC conditions; nil when the export had none.
60 public let branches: Lines?
61 public let conditions: Lines?
62 /// Ordered by name; each module's files ordered by path.
63 public let modules: [Module]
64 /// Why the run has no coverage; nil for a run with coverage. Then `lines` is zero of
65 /// zero and there are no modules. Added after version 1 shipped.
66 public let unavailable: String?
67
68 public init(
69 commit: String?, subject: String? = nil, date: Date, tests: TestCounts?,
70 files: [FileTotals], unavailable: String? = nil
7166 ) {
7266 let modules = ModuleTotals.grouping(files)
7366 self.formatVersion = Self.currentFormatVersion
7466 self.commit = commit
7566 self.subject = subject
7666 self.date = date
7766 self.tests = tests
7881 self.lines = Lines(LineSummary(adding: files.map(\.lines)))
7981 self.branches = Lines(CoverageCount(addingPresent: files.map(\.branches)))
8081 self.conditions = Lines(CoverageCount(addingPresent: files.map(\.conditions)))
8166 self.unavailable = unavailable
8268 self.modules = modules.map { module in
8368 Module(
8468 name: module.name, lines: Lines(module.lines), branches: Lines(module.branches),
8568 conditions: Lines(module.conditions),
8681 files: module.files.map {
8781 File(
8881 path: $0.relativePath, lines: Lines($0.lines),
8981 branches: Lines($0.branches), conditions: Lines($0.conditions))
9081 })
9168 }
9266 }
93
94 /// A run whose tests failed or whose profile data is missing: it records only when
95 /// it was built and why it has no numbers.
9622 public init(unavailableBecause reason: String, commit: String?, subject: String?, date: Date) {
9722 self.init(
9822 commit: commit, subject: subject, date: date, tests: nil, files: [],
9922 unavailable: reason)
10022 }
101
102 /// For readers that compare with this run.
103 /// - Throws: `RunTotalsError.noCoverage` when the run has no coverage.
1042 public func requireCoverage() throws {
1052 if let unavailable { throw RunTotalsError.noCoverage(reason: unavailable) }
1061 }
107
108 /// The file's text: pretty-printed with sorted keys, so equal runs give equal bytes.
10928 public func json() throws -> Data {
11028 let encoder = JSONEncoder()
11128 encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
11228 encoder.dateEncodingStrategy = .iso8601
11328 return try encoder.encode(self)
11428 }
115
116 /// - Throws: `RunTotalsError.unsupportedVersion` when the file's `formatVersion` is
117 /// not the current one, and `DecodingError` when it is malformed.
11812 public init(json: Data) throws {
11912 let decoder = JSONDecoder()
12012 decoder.dateDecodingStrategy = .iso8601
12112 let totals = try decoder.decode(Self.self, from: json)
12212 guard totals.formatVersion == Self.currentFormatVersion else {
1231 throw RunTotalsError.unsupportedVersion(totals.formatVersion)
12411 }
12511 self = totals
12611 }
127}
128
129public enum RunTotalsError: Error, Equatable {
130 case unsupportedVersion(Int)
131 /// The run has no coverage, so there is nothing to compare with.
132 case noCoverage(reason: String)
133}
134
135extension TestCounts: Codable {
136 private enum CodingKeys: String, CodingKey {
137 case total, failed, skipped
138 }
139
1401 public init(from decoder: any Decoder) throws {
1411 let values = try decoder.container(keyedBy: CodingKeys.self)
1421 self.init(
1431 total: try values.decode(Int.self, forKey: .total),
1441 failed: try values.decode(Int.self, forKey: .failed),
1451 skipped: try values.decode(Int.self, forKey: .skipped))
1461 }
147
1484 public func encode(to encoder: any Encoder) throws {
1494 var values = encoder.container(keyedBy: CodingKeys.self)
1504 try values.encode(total, forKey: .total)
1514 try values.encode(failed, forKey: .failed)
1524 try values.encode(skipped, forKey: .skipped)
1534 }
154}