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
11355 public init(covered: Int, total: Int) {
12355 self.covered = covered
13355 self.total = total
14355 }
15
16307 init(_ summary: LineSummary) {
17307 self.init(covered: summary.covered, total: summary.count)
18307 }
19
20614 init?(_ count: CoverageCount?) {
21614 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
476 public init(label: String?, counts: TestCounts) {
486 self.label = label
496 self.counts = counts
506 }
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
8798 ) {
8898 let modules = ModuleTotals.grouping(files)
8998 self.formatVersion = Self.currentFormatVersion
9098 self.commit = commit
9198 self.subject = subject
9298 self.date = date
9398 self.testGroups = testGroups
9498 self.tests = tests
9598 self.hasTestsPage = hasTestsPage
96111 self.lines = Lines(LineSummary(adding: files.map(\.lines)))
97111 self.branches = Lines(CoverageCount(addingPresent: files.map(\.branches)))
98111 self.conditions = Lines(CoverageCount(addingPresent: files.map(\.conditions)))
9998 self.unavailable = unavailable
10098 self.modules = modules.map { module in
10198 Module(
10298 name: module.name, lines: Lines(module.lines), branches: Lines(module.branches),
10398 conditions: Lines(module.conditions),
104111 files: module.files.map {
105111 File(
106111 path: $0.relativePath, lines: Lines($0.lines),
107111 branches: Lines($0.branches), conditions: Lines($0.conditions))
108111 })
10998 }
11098 }
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.
11423 public init(unavailableBecause reason: String, commit: String?, subject: String?, date: Date) {
11523 self.init(
11623 commit: commit, subject: subject, date: date, tests: nil, files: [],
11723 unavailable: reason)
11823 }
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.
12732 public func json() throws -> Data {
12832 let encoder = JSONEncoder()
12932 encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
13032 encoder.dateEncodingStrategy = .iso8601
13132 return try encoder.encode(self)
13232 }
133
134 /// - Throws: `RunTotalsError.unsupportedVersion` when the file's `formatVersion` is
135 /// not the current one, and `DecodingError` when it is malformed.
13616 public init(json: Data) throws {
13716 let decoder = JSONDecoder()
13816 decoder.dateDecodingStrategy = .iso8601
13916 let totals = try decoder.decode(Self.self, from: json)
14016 guard totals.formatVersion == Self.currentFormatVersion else {
1411 throw RunTotalsError.unsupportedVersion(totals.formatVersion)
14215 }
14315 self = totals
14415 }
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
1589 public init(from decoder: any Decoder) throws {
1599 let values = try decoder.container(keyedBy: CodingKeys.self)
1609 self.init(
1619 total: try values.decode(Int.self, forKey: .total),
1629 failed: try values.decode(Int.self, forKey: .failed),
1639 skipped: try values.decode(Int.self, forKey: .skipped))
1649 }
165
16613 public func encode(to encoder: any Encoder) throws {
16713 var values = encoder.container(keyedBy: CodingKeys.self)
16813 try values.encode(total, forKey: .total)
16913 try values.encode(failed, forKey: .failed)
17013 try values.encode(skipped, forKey: .skipped)
17113 }
172}