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
11374 public init(covered: Int, total: Int) {
12374 self.covered = covered
13374 self.total = total
14374 }
15
16326 init(_ summary: LineSummary) {
17326 self.init(covered: summary.covered, total: summary.count)
18326 }
19
20652 init?(_ count: CoverageCount?) {
21652 guard let count else { return nil }
2239 self.init(covered: count.covered, total: count.total)
2339 }
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 public struct TestGroup: Sendable, Equatable, Codable {
44 public let label: String?
45 public let counts: TestCounts
46
479 public init(label: String?, counts: TestCounts) {
489 self.label = label
499 self.counts = counts
509 }
51 }
52
53 /// Counts by test group; absent in older version 1 files and count-only runs.
54 public let testGroups: [TestGroup]?
55
56 /// The file name, beside `index.html`.
57 public static let fileName = "totals.json"
58 /// Raised when a field changes meaning or goes away; adding a field keeps it.
59 public static let currentFormatVersion = 1
60
61 public let formatVersion: Int
62 /// Nil when the run was not given a commit.
63 public let commit: String?
64 /// The commit's first message line; nil when the run was not given one. Added after
65 /// version 1 shipped, so older files lack it.
66 public let subject: String?
67 /// When the report was built, to the second.
68 public let date: Date
69 /// Nil when the run had no test counts.
70 public let tests: TestCounts?
71 /// Whether this run wrote tests.html; absent in older version 1 files.
72 public let hasTestsPage: Bool?
73 public let lines: Lines
74 /// Branch outcomes and MC/DC conditions; nil when the export had none.
75 public let branches: Lines?
76 public let conditions: Lines?
77 /// Ordered by name; each module's files ordered by path.
78 public let modules: [Module]
79 /// Why the run has no coverage; nil for a run with coverage. Then `lines` is zero of
80 /// zero and there are no modules. Added after version 1 shipped.
81 public let unavailable: String?
82
83 public init(
84 commit: String?, subject: String? = nil, date: Date, tests: TestCounts?,
85 files: [FileTotals], unavailable: String? = nil, hasTestsPage: Bool? = nil,
86 testGroups: [TestGroup]? = nil
87107 ) {
88107 let modules = ModuleTotals.grouping(files)
89107 self.formatVersion = Self.currentFormatVersion
90107 self.commit = commit
91107 self.subject = subject
92107 self.date = date
93107 self.testGroups = testGroups
94107 self.tests = tests
95107 self.hasTestsPage = hasTestsPage
96116 self.lines = Lines(LineSummary(adding: files.map(\.lines)))
97116 self.branches = Lines(CoverageCount(addingPresent: files.map(\.branches)))
98116 self.conditions = Lines(CoverageCount(addingPresent: files.map(\.conditions)))
99107 self.unavailable = unavailable
100107 self.modules = modules.map { module in
101103 Module(
102103 name: module.name, lines: Lines(module.lines), branches: Lines(module.branches),
103103 conditions: Lines(module.conditions),
104116 files: module.files.map {
105116 File(
106116 path: $0.relativePath, lines: Lines($0.lines),
107116 branches: Lines($0.branches), conditions: Lines($0.conditions))
108116 })
109103 }
110107 }
111
112 /// A run whose tests failed or whose profile data is missing: it records only when
113 /// it was built and why it has no numbers.
11422 public init(unavailableBecause reason: String, commit: String?, subject: String?, date: Date) {
11522 self.init(
11622 commit: commit, subject: subject, date: date, tests: nil, files: [],
11722 unavailable: reason)
11822 }
119
120 /// For readers that compare with this run.
121 /// - Throws: `RunTotalsError.noCoverage` when the run has no coverage.
1222 public func requireCoverage() throws {
1232 if let unavailable { throw RunTotalsError.noCoverage(reason: unavailable) }
1241 }
125
126 /// The file's text: pretty-printed with sorted keys, so equal runs give equal bytes.
12734 public func json() throws -> Data {
12834 let encoder = JSONEncoder()
12934 encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
13034 encoder.dateEncodingStrategy = .iso8601
13134 return try encoder.encode(self)
13234 }
133
134 /// - Throws: `RunTotalsError.unsupportedVersion` when the file's `formatVersion` is
135 /// not the current one, and `DecodingError` when it is malformed.
13617 public init(json: Data) throws {
13717 let decoder = JSONDecoder()
13817 decoder.dateDecodingStrategy = .iso8601
13917 let totals = try decoder.decode(Self.self, from: json)
14017 guard totals.formatVersion == Self.currentFormatVersion else {
1411 throw RunTotalsError.unsupportedVersion(totals.formatVersion)
14216 }
14316 self = totals
14416 }
145}
146
147public enum RunTotalsError: Error, Equatable {
148 case unsupportedVersion(Int)
149 /// The run has no coverage, so there is nothing to compare with.
150 case noCoverage(reason: String)
151}
152
153extension TestCounts: Codable {
154 private enum CodingKeys: String, CodingKey {
155 case total, failed, skipped
156 }
157
15812 public init(from decoder: any Decoder) throws {
15912 let values = try decoder.container(keyedBy: CodingKeys.self)
16012 self.init(
16112 total: try values.decode(Int.self, forKey: .total),
16212 failed: try values.decode(Int.self, forKey: .failed),
16312 skipped: try values.decode(Int.self, forKey: .skipped))
16412 }
165
16618 public func encode(to encoder: any Encoder) throws {
16718 var values = encoder.container(keyedBy: CodingKeys.self)
16818 try values.encode(total, forKey: .total)
16918 try values.encode(failed, forKey: .failed)
17018 try values.encode(skipped, forKey: .skipped)
17118 }
172}