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
1690 ) {
1790 self.relativePath = relativePath
1890 self.lines = lines
1990 self.branches = branches
2090 self.conditions = conditions
2190 }
22
23 /// The first path component: the Swift module the file belongs to.
24303 public var module: String {
251391 String(relativePath.prefix { $0 != "/" })
26303 }
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.
39176 public static func grouping(_ files: [FileTotals]) -> [ModuleTotals] {
40303 Dictionary(grouping: files, by: \.module)
41230 .map { name, files in
42230 ModuleTotals(
43230 name: name, files: files.sorted { $0.relativePath < $1.relativePath },
44303 lines: LineSummary(adding: files.map(\.lines)),
45303 branches: CoverageCount(addingPresent: files.map(\.branches)),
46303 conditions: CoverageCount(addingPresent: files.map(\.conditions)))
47230 }
48176 .sorted { $0.name < $1.name }
49176 }
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
72133 public init(total: Int, failed: Int, skipped: Int) {
73133 self.total = total
74133 self.failed = failed
75133 self.skipped = skipped
76133 }
77
7891 public var passed: Int { total - failed - skipped }
79
80 /// The number of tests with its noun, such as `905 tests` or `1 test`.
8132 var totalText: String {
8232 "\(groupingDigits(total)) \(total == 1 ? "test" : "tests")"
8332 }
84}
85
86extension LineSummary {
87 /// Sums of several summaries, such as a module's files.
88404 init(adding summaries: some Sequence<LineSummary>) {
89404 var count = 0
90404 var covered = 0
91542 for summary in summaries {
92542 count += summary.count
93542 covered += summary.covered
94542 }
95404 self.init(count: count, covered: covered)
96404 }
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.
100611 var percentText: String {
101611 String(format: "%.1f%%", percent)
102611 }
103
104909 var percent: Double {
105909 count == 0 ? 100 : Double(covered) * 100 / Double(count)
106909 }
107
108293 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.
113991func groupingDigits(_ number: Int) -> String {
114991 let digits = Array(String(number.magnitude))
115991 var grouped = ""
1161220 for (index, digit) in digits.enumerated() {
1171220 if index > 0, (digits.count - index) % 3 == 0 {
1184 grouped.append(",")
1194 }
1201220 grouped.append(digit)
1211220 }
122991 return number < 0 ? "-" + grouped : grouped
123991}