| 1 | | import Foundation |
| 2 | | |
| 3 | | /// The Markdown CI appends to the GitHub Actions job summary: test counts, total line |
| 4 | | /// 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 | 24 | ) { |
| 18 | 24 | self.comparison = baseline.map(BaselineComparison.init) |
| 19 | 24 | self.modules = ModuleTotals.grouping(files) |
| 20 | 24 | self.tests = tests |
| 21 | 24 | self.testGroups = testGroups |
| 22 | 24 | self.commit = commit |
| 23 | 24 | } |
| 24 | | |
| 25 | 24 | public var markdown: String { |
| 26 | 43 | let total = LineSummary(adding: modules.map(\.lines)) |
| 27 | 24 | var lines = [commit.map { "### Coverage at \($0)" } ?? "### Coverage", ""] |
| 28 | 24 | 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 | 24 | 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 | 24 | } |
| 41 | 24 | let totalChange = |
| 42 | 24 | comparison.map { ", \($0.changeOfTotal(now: total).text) since the baseline" } ?? "" |
| 43 | 24 | lines.append( |
| 44 | 24 | "Line coverage: **\(total.percentText)** " |
| 45 | 24 | + "(\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines)" |
| 46 | 24 | + totalChange + ".") |
| 47 | 24 | lines += [ |
| 48 | 24 | "", |
| 49 | 24 | "| Module | Line coverage |\(comparison == nil ? "" : " Change |") Missed | Lines |", |
| 50 | 24 | comparison == nil |
| 51 | 24 | ? "| --- | ---: | ---: | ---: |" : "| --- | ---: | ---: | ---: | ---: |", |
| 52 | 24 | ] |
| 53 | 43 | for module in modules { |
| 54 | 43 | let missed = module.lines.count - module.lines.covered |
| 55 | 43 | let changeCell = |
| 56 | 43 | comparison.map { " \($0.change(ofModule: module.name, now: module.lines).text) |" } |
| 57 | 43 | ?? "" |
| 58 | 43 | lines.append( |
| 59 | 43 | "| \(module.name) | \(module.lines.percentText) |\(changeCell) " |
| 60 | 43 | + "\(groupingDigits(missed)) | \(groupingDigits(module.lines.count)) |") |
| 61 | 43 | } |
| 62 | 24 | return lines.joined(separator: "\n") + "\n" |
| 63 | 24 | } |
| 64 | | } |