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
1685 ) {
1785 self.relativePath = relativePath
1885 self.lines = lines
1985 self.branches = branches
2085 self.conditions = conditions
2185 }
22
23 /// The first path component: the Swift module the file belongs to.
24298 public var module: String {
251369 String(relativePath.prefix { $0 != "/" })
26298 }
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.
39166 public static func grouping(_ files: [FileTotals]) -> [ModuleTotals] {
40298 Dictionary(grouping: files, by: \.module)
41225 .map { name, files in
42225 ModuleTotals(
43225 name: name, files: files.sorted { $0.relativePath < $1.relativePath },
44298 lines: LineSummary(adding: files.map(\.lines)),
45298 branches: CoverageCount(addingPresent: files.map(\.branches)),
46298 conditions: CoverageCount(addingPresent: files.map(\.conditions)))
47225 }
48166 .sorted { $0.name < $1.name }
49166 }
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
5910 public var counts: TestCounts {
6010 switch self {
6110 case .counts(let counts): counts
6210 case .results(let results): results.counts
6310 }
6410 }
65}
66
67public struct TestCounts: Sendable, Equatable {
68 public let total: Int
69 public let failed: Int
70 public let skipped: Int
71
7257 public init(total: Int, failed: Int, skipped: Int) {
7357 self.total = total
7457 self.failed = failed
7557 self.skipped = skipped
7657 }
77
7821 public var passed: Int { total - failed - skipped }
79
80 /// The number of tests with its noun, such as `905 tests` or `1 test`.
8121 var totalText: String {
8221 "\(groupingDigits(total)) \(total == 1 ? "test" : "tests")"
8321 }
84}
85
86extension LineSummary {
87 /// Sums of several summaries, such as a module's files.
88389 init(adding summaries: some Sequence<LineSummary>) {
89389 var count = 0
90389 var covered = 0
91532 for summary in summaries {
92532 count += summary.count
93532 covered += summary.covered
94532 }
95389 self.init(count: count, covered: covered)
96389 }
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.
100607 var percentText: String {
101607 String(format: "%.1f%%", percent)
102607 }
103
104902 var percent: Double {
105902 count == 0 ? 100 : Double(covered) * 100 / Double(count)
106902 }
107
108290 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.
113891func groupingDigits(_ number: Int) -> String {
114891 let digits = Array(String(number.magnitude))
115891 var grouped = ""
1161121 for (index, digit) in digits.enumerated() {
1171121 if index > 0, (digits.count - index) % 3 == 0 {
1184 grouped.append(",")
1194 }
1201121 grouped.append(digit)
1211121 }
122891 return number < 0 ? "-" + grouped : grouped
123891}