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 region data for the file.
9 public let regions: CoverageCount?
10 /// Nil when the export has no branch data for the file.
11 public let branches: CoverageCount?
12 /// Nil when the export has no MC/DC data for the file.
13 public let conditions: CoverageCount?
14
15 public init(
16 relativePath: String, lines: LineSummary, regions: CoverageCount? = nil,
17 branches: CoverageCount? = nil, conditions: CoverageCount? = nil
18305 ) {
19305 self.regions = regions
20305 self.relativePath = relativePath
21305 self.lines = lines
22305 self.branches = branches
23305 self.conditions = conditions
24305 }
25
26 /// The first path component: the Swift module the file belongs to.
27557 public var module: String {
282560 String(relativePath.prefix { $0 != "/" })
29557 }
30}
31
32/// A module's files and their combined line totals.
33public struct ModuleTotals: Sendable, Equatable {
34 public let name: String
35 /// Ordered by path.
36 public let files: [FileTotals]
37 public let lines: LineSummary
38 public let regions: CoverageCount?
39 public let branches: CoverageCount?
40 public let conditions: CoverageCount?
41
42 /// The files grouped by module, modules ordered by name.
43405 public static func grouping(_ files: [FileTotals]) -> [ModuleTotals] {
44557 Dictionary(grouping: files, by: \.module)
45473 .map { name, files in
46473 ModuleTotals(
47473 name: name, files: files.sorted { $0.relativePath < $1.relativePath },
48557 lines: LineSummary(adding: files.map(\.lines)),
49557 regions: CoverageCount(addingPresent: files.map(\.regions)),
50557 branches: CoverageCount(addingPresent: files.map(\.branches)),
51557 conditions: CoverageCount(addingPresent: files.map(\.conditions)))
52473 }
53405 .sorted { $0.name < $1.name }
54405 }
55}
56
57/// How a test run ended, from its JUnit results.
58/// What the caller knows about the run's tests: only the counts, or every case, which
59/// also gives the site a test results page.
60public enum TestRun: Sendable, Equatable {
61 case counts(TestCounts)
62 case results(TestResults)
63
6410 public var counts: TestCounts {
6510 switch self {
6610 case .counts(let counts): counts
6710 case .results(let results): results.counts
6810 }
6910 }
70}
71
72public struct TestCounts: Sendable, Equatable {
73 public let total: Int
74 public let failed: Int
75 public let skipped: Int
76
77109 public init(total: Int, failed: Int, skipped: Int) {
78109 self.total = total
79109 self.failed = failed
80109 self.skipped = skipped
81109 }
82
8367 public var passed: Int { total - failed - skipped }
84
85 /// The number of tests with its noun, such as `905 tests` or `1 test`.
868 var totalText: String {
878 "\(groupingDigits(total)) \(total == 1 ? "test" : "tests")"
888 }
89}
90
91extension LineSummary {
92 /// Sums of several summaries, such as a module's files.
93876 init(adding summaries: some Sequence<LineSummary>) {
94876 var count = 0
95876 var covered = 0
961042 for summary in summaries {
971042 count += summary.count
981042 covered += summary.covered
991042 }
100876 self.init(count: count, covered: covered)
101876 }
102
103 /// The covered share as shown everywhere in the report, such as `92.9%`; a summary
104 /// with no code shows 100%, matching its tier.
1051383 var percentText: String {
1061383 String(format: "%.1f%%", percent)
1071383 }
108
1092290 var percent: Double {
1102290 count == 0 ? 100 : Double(covered) * 100 / Double(count)
1112290 }
112
113669 var tier: CoverageTier { CoverageTier(covered: covered, total: count) }
114}
115
116/// `number` with commas between groups of three digits, such as `13,126`. Fixed rather
117/// than locale-dependent, so CI output doesn't change with the runner's settings.
1181953func groupingDigits(_ number: Int) -> String {
1191953 let digits = Array(String(number.magnitude))
1201953 var grouped = ""
1212733 for (index, digit) in digits.enumerated() {
1222733 if index > 0, (digits.count - index) % 3 == 0 {
1234 grouped.append(",")
1244 }
1252733 grouped.append(digit)
1262733 }
1271953 return number < 0 ? "-" + grouped : grouped
1281953}