| 1 | | import Foundation |
| 2 | | |
| 3 | | /// The Markdown CI appends to the GitHub Actions job summary: test counts, total line |
| 4 | | /// and region coverage and a row per module. |
| 5 | | public struct JobSummary: Sendable { |
| 6 | | private let modules: [ModuleTotals] |
| 7 | | private let tests: TestCounts? |
| 8 | | private let testGroups: [RunTotals.TestGroup] |
| 9 | | private let commit: String? |
| 10 | | private let comparison: BaselineComparison? |
| 11 | | |
| 12 | | /// - Parameter baseline: an earlier run to compare with; with it, the total line and |
| 13 | | /// the module table show the change since then. |
| 14 | | public init( |
| 15 | | files: [FileTotals], tests: TestCounts?, commit: String?, baseline: RunTotals? = nil, |
| 16 | | testGroups: [RunTotals.TestGroup] = [] |
| 17 | 36 | ) { |
| 18 | 36 | self.comparison = baseline.map(BaselineComparison.init) |
| 19 | 36 | self.modules = ModuleTotals.grouping(files) |
| 20 | 36 | self.tests = tests |
| 21 | 36 | self.testGroups = testGroups |
| 22 | 36 | self.commit = commit |
| 23 | 36 | } |
| 24 | | |
| 25 | 36 | public var markdown: String { |
| 26 | 57 | let total = LineSummary(adding: modules.map(\.lines)) |
| 27 | 36 | var lines = [commit.map { "### Coverage at \($0)" } ?? "### Coverage", ""] |
| 28 | 36 | if let tests { |
| 29 | 8 | lines.append( |
| 30 | 8 | "**\(tests.totalText)**: \(groupingDigits(tests.passed)) passed, " |
| 31 | 8 | + "\(groupingDigits(tests.failed)) failed, \(groupingDigits(tests.skipped)) skipped." |
| 32 | 8 | ) |
| 33 | 8 | } |
| 34 | 36 | for group in testGroups { |
| 35 | 7 | let counts = group.counts |
| 36 | 7 | let label = (group.label ?? "Tests").replacingOccurrences(of: "\n", with: " ") |
| 37 | 7 | lines.append( |
| 38 | 7 | "- \(label): \(counts.passed) passed, \(counts.failed) failed, \(counts.skipped) skipped" |
| 39 | 7 | ) |
| 40 | 36 | } |
| 41 | 36 | let totalChange = |
| 42 | 36 | comparison.map { ", \($0.changeOfTotal(now: total).text) since the baseline" } ?? "" |
| 43 | 36 | lines.append( |
| 44 | 36 | "Line coverage: **\(total.percentText)** " |
| 45 | 36 | + "(\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines)" |
| 46 | 36 | + totalChange + ".") |
| 47 | 57 | let regions = CoverageCount(addingPresent: modules.map(\.regions)) |
| 48 | 36 | if let regions { |
| 49 | 10 | lines += ["", regionLine(regions)] |
| 50 | 10 | } |
| 51 | 36 | lines.append("") |
| 52 | 36 | lines += moduleTable |
| 53 | 36 | return lines.joined(separator: "\n") + "\n" |
| 54 | 36 | } |
| 55 | | |
| 56 | | /// Lines and, where the export counts them, regions, each in the same columns. |
| 57 | 36 | private var moduleTable: [String] { |
| 58 | 55 | let hasRegions = modules.contains { $0.regions != nil } |
| 59 | 36 | let columns = |
| 60 | 36 | columnTitles(share: "Line coverage", total: "Lines") |
| 61 | 36 | + (hasRegions ? columnTitles(share: "Region coverage", total: "Regions") : []) |
| 62 | 36 | return [ |
| 63 | 36 | Self.tableRow(["Module"] + columns), |
| 64 | 36 | "| --- |" + String(repeating: " ---: |", count: columns.count), |
| 65 | 36 | ] |
| 66 | 57 | + modules.map { module in |
| 67 | 57 | Self.tableRow( |
| 68 | 57 | [module.name] + lineCells(module) + (hasRegions ? regionCells(module) : [])) |
| 69 | 57 | } |
| 70 | 36 | } |
| 71 | | |
| 72 | 48 | private func columnTitles(share: String, total: String) -> [String] { |
| 73 | 48 | [share] + (comparison == nil ? [] : ["Change"]) + ["Missed", total] |
| 74 | 48 | } |
| 75 | | |
| 76 | 57 | private func lineCells(_ module: ModuleTotals) -> [String] { |
| 77 | 57 | metricCells( |
| 78 | 57 | share: module.lines.percentText, |
| 79 | 57 | change: comparison?.change(ofModule: module.name, now: module.lines), |
| 80 | 57 | covered: module.lines.covered, total: module.lines.count) |
| 81 | 57 | } |
| 82 | | |
| 83 | | /// Dashes where this module has no region data while others do. |
| 84 | 12 | private func regionCells(_ module: ModuleTotals) -> [String] { |
| 85 | 12 | guard let regions = module.regions else { |
| 86 | 6 | return columnTitles(share: "", total: "").map { _ in "–" } |
| 87 | 10 | } |
| 88 | 10 | return metricCells( |
| 89 | 10 | share: regions.percentText, |
| 90 | 10 | change: comparison?.regionChange(ofModule: module.name, now: regions), |
| 91 | 10 | covered: regions.covered, total: regions.total) |
| 92 | 12 | } |
| 93 | | |
| 94 | | private func metricCells( |
| 95 | | share: String, change: CoverageChange?, covered: Int, total: Int |
| 96 | 67 | ) -> [String] { |
| 97 | 67 | [share] + (change.map { [$0.text] } ?? []) |
| 98 | 67 | + [groupingDigits(total - covered), groupingDigits(total)] |
| 99 | 67 | } |
| 100 | | |
| 101 | 93 | private static func tableRow(_ cells: [String]) -> String { |
| 102 | 93 | "| \(cells.joined(separator: " | ")) |" |
| 103 | 93 | } |
| 104 | | |
| 105 | 10 | private func regionLine(_ regions: CoverageCount) -> String { |
| 106 | 10 | let change = |
| 107 | 10 | comparison.map { ", \($0.regionChange(now: regions).text) since the baseline" } ?? "" |
| 108 | 10 | return "Region coverage: **\(regions.percentText)** " |
| 109 | 10 | + "(\(groupingDigits(regions.covered)) of \(groupingDigits(regions.total)) regions)" |
| 110 | 10 | + change + "." |
| 111 | 10 | } |
| 112 | | } |