| 1 | | import Foundation |
| 2 | | |
| 3 | | /// One source file's line totals and where it sits in the report. |
| 4 | | public 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 |
| 18 | 297 | ) { |
| 19 | 297 | self.regions = regions |
| 20 | 297 | self.relativePath = relativePath |
| 21 | 297 | self.lines = lines |
| 22 | 297 | self.branches = branches |
| 23 | 297 | self.conditions = conditions |
| 24 | 297 | } |
| 25 | | |
| 26 | | /// The first path component: the Swift module the file belongs to. |
| 27 | 529 | public var module: String { |
| 28 | 2437 | String(relativePath.prefix { $0 != "/" }) |
| 29 | 529 | } |
| 30 | | } |
| 31 | | |
| 32 | | /// A module's files and their combined line totals. |
| 33 | | public 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. |
| 43 | 392 | public static func grouping(_ files: [FileTotals]) -> [ModuleTotals] { |
| 44 | 529 | Dictionary(grouping: files, by: \.module) |
| 45 | 445 | .map { name, files in |
| 46 | 445 | ModuleTotals( |
| 47 | 445 | name: name, files: files.sorted { $0.relativePath < $1.relativePath }, |
| 48 | 529 | lines: LineSummary(adding: files.map(\.lines)), |
| 49 | 529 | regions: CoverageCount(addingPresent: files.map(\.regions)), |
| 50 | 529 | branches: CoverageCount(addingPresent: files.map(\.branches)), |
| 51 | 529 | conditions: CoverageCount(addingPresent: files.map(\.conditions))) |
| 52 | 445 | } |
| 53 | 392 | .sorted { $0.name < $1.name } |
| 54 | 392 | } |
| 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. |
| 60 | | public enum TestRun: Sendable, Equatable { |
| 61 | | case counts(TestCounts) |
| 62 | | case results(TestResults) |
| 63 | | |
| 64 | 10 | public var counts: TestCounts { |
| 65 | 10 | switch self { |
| 66 | 10 | case .counts(let counts): counts |
| 67 | 10 | case .results(let results): results.counts |
| 68 | 10 | } |
| 69 | 10 | } |
| 70 | | } |
| 71 | | |
| 72 | | public struct TestCounts: Sendable, Equatable { |
| 73 | | public let total: Int |
| 74 | | public let failed: Int |
| 75 | | public let skipped: Int |
| 76 | | |
| 77 | 109 | public init(total: Int, failed: Int, skipped: Int) { |
| 78 | 109 | self.total = total |
| 79 | 109 | self.failed = failed |
| 80 | 109 | self.skipped = skipped |
| 81 | 109 | } |
| 82 | | |
| 83 | 67 | public var passed: Int { total - failed - skipped } |
| 84 | | |
| 85 | | /// The number of tests with its noun, such as `905 tests` or `1 test`. |
| 86 | 8 | var totalText: String { |
| 87 | 8 | "\(groupingDigits(total)) \(total == 1 ? "test" : "tests")" |
| 88 | 8 | } |
| 89 | | } |
| 90 | | |
| 91 | | extension LineSummary { |
| 92 | | /// Sums of several summaries, such as a module's files. |
| 93 | 835 | init(adding summaries: some Sequence<LineSummary>) { |
| 94 | 835 | var count = 0 |
| 95 | 835 | var covered = 0 |
| 96 | 986 | for summary in summaries { |
| 97 | 986 | count += summary.count |
| 98 | 986 | covered += summary.covered |
| 99 | 986 | } |
| 100 | 835 | self.init(count: count, covered: covered) |
| 101 | 835 | } |
| 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. |
| 105 | 1328 | var percentText: String { |
| 106 | 1328 | String(format: "%.1f%%", percent) |
| 107 | 1328 | } |
| 108 | | |
| 109 | 2231 | var percent: Double { |
| 110 | 2231 | count == 0 ? 100 : Double(covered) * 100 / Double(count) |
| 111 | 2231 | } |
| 112 | | |
| 113 | 622 | 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. |
| 118 | 1860 | func groupingDigits(_ number: Int) -> String { |
| 119 | 1860 | let digits = Array(String(number.magnitude)) |
| 120 | 1860 | var grouped = "" |
| 121 | 2639 | for (index, digit) in digits.enumerated() { |
| 122 | 2639 | if index > 0, (digits.count - index) % 3 == 0 { |
| 123 | 4 | grouped.append(",") |
| 124 | 4 | } |
| 125 | 2639 | grouped.append(digit) |
| 126 | 2639 | } |
| 127 | 1860 | return number < 0 ? "-" + grouped : grouped |
| 128 | 1860 | } |