1import Foundation
2
3/// One source file's line totals and where it sits in the report.
4public struct FileTotals: Sendable, Equatable {
5 /// The file's path below the sources folder, such as `Kit/File.swift`.
6 public let relativePath: String
7 public let lines: LineSummary
8 /// Nil when the export has no branch data for the file.
9 public let branches: CoverageCount?
10 /// Nil when the export has no MC/DC data for the file.
11 public let conditions: CoverageCount?
12
13 public init(
14 relativePath: String, lines: LineSummary, branches: CoverageCount? = nil,
15 conditions: CoverageCount? = nil
1652 ) {
1752 self.relativePath = relativePath
1852 self.lines = lines
1952 self.branches = branches
2052 self.conditions = conditions
2152 }
22
23 /// The first path component: the Swift module the file belongs to.
24247 public var module: String {
251127 String(relativePath.prefix { $0 != "/" })
26247 }
27}
28
29/// A module's files and their combined line totals.
30public struct ModuleTotals: Sendable, Equatable {
31 public let name: String
32 /// Ordered by path.
33 public let files: [FileTotals]
34 public let lines: LineSummary
35 public let branches: CoverageCount?
36 public let conditions: CoverageCount?
37
38 /// The files grouped by module, modules ordered by name.
39125 public static func grouping(_ files: [FileTotals]) -> [ModuleTotals] {
40247 Dictionary(grouping: files, by: \.module)
41177 .map { name, files in
42177 ModuleTotals(
43177 name: name, files: files.sorted { $0.relativePath < $1.relativePath },
44247 lines: LineSummary(adding: files.map(\.lines)),
45247 branches: CoverageCount(addingPresent: files.map(\.branches)),
46247 conditions: CoverageCount(addingPresent: files.map(\.conditions)))
47177 }
48125 .sorted { $0.name < $1.name }
49125 }
50}
51
52/// How a test run ended, from its JUnit results.
53/// What the caller knows about the run's tests: only the counts, or every case, which
54/// also gives the site a test results page.
55public enum TestRun: Sendable, Equatable {
56 case counts(TestCounts)
57 case results(TestResults)
58
5913 public var counts: TestCounts {
6013 switch self {
6113 case .counts(let counts): counts
6213 case .results(let results): results.counts
6313 }
6413 }
65}
66
67public struct TestCounts: Sendable, Equatable {
68 public let total: Int
69 public let failed: Int
70 public let skipped: Int
71
7234 public init(total: Int, failed: Int, skipped: Int) {
7334 self.total = total
7434 self.failed = failed
7534 self.skipped = skipped
7634 }
77
7824 public var passed: Int { total - failed - skipped }
79
80 /// The number of tests with its noun, such as `905 tests` or `1 test`.
8124 var totalText: String {
8224 "\(groupingDigits(total)) \(total == 1 ? "test" : "tests")"
8324 }
84}
85
86extension LineSummary {
87 /// Sums of several summaries, such as a module's files.
88301 init(adding summaries: some Sequence<LineSummary>) {
89301 var count = 0
90301 var covered = 0
91434 for summary in summaries {
92434 count += summary.count
93434 covered += summary.covered
94434 }
95301 self.init(count: count, covered: covered)
96301 }
97
98 /// The covered share as shown everywhere in the report, such as `92.9%`; a summary
99 /// with no code shows 100%, matching its tier.
100524 var percentText: String {
101524 String(format: "%.1f%%", percent)
102524 }
103
104754 var percent: Double {
105754 count == 0 ? 100 : Double(covered) * 100 / Double(count)
106754 }
107
108285 var tier: CoverageTier { CoverageTier(covered: covered, total: count) }
109}
110
111/// `number` with commas between groups of three digits, such as `13,126`. Fixed rather
112/// than locale-dependent, so CI output doesn't change with the runner's settings.
113883func groupingDigits(_ number: Int) -> String {
114883 let digits = Array(String(number.magnitude))
115883 var grouped = ""
1161224 for (index, digit) in digits.enumerated() {
1171224 if index > 0, (digits.count - index) % 3 == 0 {
1184 grouped.append(",")
1194 }
1201224 grouped.append(digit)
1211224 }
122883 return number < 0 ? "-" + grouped : grouped
123883}