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
18431 ) {
19431 self.regions = regions
20431 self.relativePath = relativePath
21431 self.lines = lines
22431 self.branches = branches
23431 self.conditions = conditions
24431 }
25
26 /// The first path component: the Swift module the file belongs to.
27660 public var module: String {
283139 String(relativePath.prefix { $0 != "/" })
29660 }
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.
43526 public static func grouping(_ files: [FileTotals]) -> [ModuleTotals] {
44660 Dictionary(grouping: files, by: \.module)
45577 .map { name, files in
46577 ModuleTotals(
47577 name: name, files: files.sorted { $0.relativePath < $1.relativePath },
48660 lines: LineSummary(adding: files.map(\.lines)),
49660 regions: CoverageCount(addingPresent: files.map(\.regions)),
50660 branches: CoverageCount(addingPresent: files.map(\.branches)),
51660 conditions: CoverageCount(addingPresent: files.map(\.conditions)))
52577 }
53526 .sorted { $0.name < $1.name }
54526 }
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.
931100 init(adding summaries: some Sequence<LineSummary>) {
941100 var count = 0
951100 var covered = 0
961249 for summary in summaries {
971249 count += summary.count
981249 covered += summary.covered
991249 }
1001100 self.init(count: count, covered: covered)
1011100 }
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.
1051098 var percentText: String {
1061098 String(format: "%.1f%%", percent)
1071098 }
108
1091464 var percent: Double {
1101464 count == 0 ? 100 : Double(covered) * 100 / Double(count)
1111464 }
112
113345 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.
1181897func groupingDigits(_ number: Int) -> String {
1191897 let digits = Array(String(number.magnitude))
1201897 var grouped = ""
1213169 for (index, digit) in digits.enumerated() {
1223169 if index > 0, (digits.count - index) % 3 == 0 {
1234 grouped.append(",")
1244 }
1253169 grouped.append(digit)
1263169 }
1271897 return number < 0 ? "-" + grouped : grouped
1281897}