1import Foundation
2
3/// The Markdown CI appends to the GitHub Actions job summary: test counts, total line
4/// coverage and a row per module.
5public 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] = []
1724 ) {
1824 self.comparison = baseline.map(BaselineComparison.init)
1924 self.modules = ModuleTotals.grouping(files)
2024 self.tests = tests
2124 self.testGroups = testGroups
2224 self.commit = commit
2324 }
24
2524 public var markdown: String {
2643 let total = LineSummary(adding: modules.map(\.lines))
2724 var lines = [commit.map { "### Coverage at \($0)" } ?? "### Coverage", ""]
2824 if let tests {
298 lines.append(
308 "**\(tests.totalText)**: \(groupingDigits(tests.passed)) passed, "
318 + "\(groupingDigits(tests.failed)) failed, \(groupingDigits(tests.skipped)) skipped."
328 )
338 }
3424 for group in testGroups {
357 let counts = group.counts
367 let label = (group.label ?? "Tests").replacingOccurrences(of: "\n", with: " ")
377 lines.append(
387 "- \(label): \(counts.passed) passed, \(counts.failed) failed, \(counts.skipped) skipped"
397 )
4024 }
4124 let totalChange =
4224 comparison.map { ", \($0.changeOfTotal(now: total).text) since the baseline" } ?? ""
4324 lines.append(
4424 "Line coverage: **\(total.percentText)** "
4524 + "(\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines)"
4624 + totalChange + ".")
4724 lines += [
4824 "",
4924 "| Module | Line coverage |\(comparison == nil ? "" : " Change |") Missed | Lines |",
5024 comparison == nil
5124 ? "| --- | ---: | ---: | ---: |" : "| --- | ---: | ---: | ---: | ---: |",
5224 ]
5343 for module in modules {
5443 let missed = module.lines.count - module.lines.covered
5543 let changeCell =
5643 comparison.map { " \($0.change(ofModule: module.name, now: module.lines).text) |" }
5743 ?? ""
5843 lines.append(
5943 "| \(module.name) | \(module.lines.percentText) |\(changeCell) "
6043 + "\(groupingDigits(missed)) | \(groupingDigits(module.lines.count)) |")
6143 }
6224 return lines.joined(separator: "\n") + "\n"
6324 }
64}