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
11874 public init(covered: Int, total: Int) {
12874 self.covered = covered
13874 self.total = total
14874 }
15
16776 init(_ summary: LineSummary) {
17776 self.init(covered: summary.covered, total: summary.count)
18776 }
19
202328 init?(_ count: CoverageCount?) {
212328 guard let count else { return nil }
2286 self.init(covered: count.covered, total: count.total)
2386 }
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
509 public init(label: String?, counts: TestCounts) {
519 self.label = label
529 self.counts = counts
539 }
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
93258 ) {
94258 let modules = ModuleTotals.grouping(files)
95258 self.formatVersion = Self.currentFormatVersion
96258 self.commit = commit
97258 self.subject = subject
98258 self.date = date
99258 self.testGroups = testGroups
100258 self.tests = tests
101258 self.hasTestsPage = hasTestsPage
102267 self.lines = Lines(LineSummary(adding: files.map(\.lines)))
103267 self.regions = Lines(CoverageCount(addingPresent: files.map(\.regions)))
104267 self.branches = Lines(CoverageCount(addingPresent: files.map(\.branches)))
105267 self.conditions = Lines(CoverageCount(addingPresent: files.map(\.conditions)))
106258 self.unavailable = unavailable
107258 self.modules = modules.map { module in
108251 Module(
109251 name: module.name, lines: Lines(module.lines), regions: Lines(module.regions),
110251 branches: Lines(module.branches),
111251 conditions: Lines(module.conditions),
112267 files: module.files.map {
113267 File(
114267 path: $0.relativePath, lines: Lines($0.lines), regions: Lines($0.regions),
115267 branches: Lines($0.branches), conditions: Lines($0.conditions))
116267 })
117251 }
118258 }
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.
12225 public init(unavailableBecause reason: String, commit: String?, subject: String?, date: Date) {
12325 self.init(
12425 commit: commit, subject: subject, date: date, tests: nil, files: [],
12525 unavailable: reason)
12625 }
127
128 /// For readers that compare with this run.
129 /// - Throws: `RunTotalsError.noCoverage` when the run has no coverage.
1302 public func requireCoverage() throws {
1312 if let unavailable { throw RunTotalsError.noCoverage(reason: unavailable) }
1321 }
133
134 /// The file's text: pretty-printed with sorted keys, so equal runs give equal bytes.
13539 public func json() throws -> Data {
13639 let encoder = JSONEncoder()
13739 encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
13839 encoder.dateEncodingStrategy = .iso8601
13939 return try encoder.encode(self)
14039 }
141
142 /// - Throws: `RunTotalsError.unsupportedVersion` when the file's `formatVersion` is
143 /// not the current one, and `DecodingError` when it is malformed.
14421 public init(json: Data) throws {
14521 let decoder = JSONDecoder()
14621 decoder.dateDecodingStrategy = .iso8601
14721 let totals = try decoder.decode(Self.self, from: json)
14821 guard totals.formatVersion == Self.currentFormatVersion else {
1491 throw RunTotalsError.unsupportedVersion(totals.formatVersion)
15020 }
15120 self = totals
15220 }
153}
154
155public 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
161extension TestCounts: Codable {
162 private enum CodingKeys: String, CodingKey {
163 case total, failed, skipped
164 }
165
16612 public init(from decoder: any Decoder) throws {
16712 let values = try decoder.container(keyedBy: CodingKeys.self)
16812 self.init(
16912 total: try values.decode(Int.self, forKey: .total),
17012 failed: try values.decode(Int.self, forKey: .failed),
17112 skipped: try values.decode(Int.self, forKey: .skipped))
17212 }
173
17418 public func encode(to encoder: any Encoder) throws {
17518 var values = encoder.container(keyedBy: CodingKeys.self)
17618 try values.encode(total, forKey: .total)
17718 try values.encode(failed, forKey: .failed)
17818 try values.encode(skipped, forKey: .skipped)
17918 }
180}