1import 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.
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] = []
1733 ) {
1833 self.comparison = baseline.map(BaselineComparison.init)
1933 self.modules = ModuleTotals.grouping(files)
2033 self.tests = tests
2133 self.testGroups = testGroups
2233 self.commit = commit
2333 }
24
2533 public var markdown: String {
2653 let total = LineSummary(adding: modules.map(\.lines))
2733 var lines = [commit.map { "### Coverage at \($0)" } ?? "### Coverage", ""]
2833 if let tests {
298 lines.append(
308 "**\(tests.totalText)**: \(groupingDigits(tests.passed)) passed, "
318 + "\(groupingDigits(tests.failed)) failed, \(groupingDigits(tests.skipped)) skipped."
328 )
338 }
3433 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 )
4033 }
4133 let totalChange =
4233 comparison.map { ", \($0.changeOfTotal(now: total).text) since the baseline" } ?? ""
4333 lines.append(
4433 "Line coverage: **\(total.percentText)** "
4533 + "(\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines)"
4633 + totalChange + ".")
4753 let regions = CoverageCount(addingPresent: modules.map(\.regions))
4833 if let regions {
497 lines += ["", regionLine(regions)]
507 }
5133 let hasRegions = regions != nil
5233 let columns =
5333 ["Line coverage"] + (comparison == nil ? [] : ["Change"]) + ["Missed", "Lines"]
5433 + (hasRegions ? ["Regions"] : [])
5533 lines += [
5633 "",
5733 "| Module | \(columns.joined(separator: " | ")) |",
5833 "| --- |" + String(repeating: " ---: |", count: columns.count),
5933 ]
6053 for module in modules {
6153 let missed = module.lines.count - module.lines.covered
6253 let changeCell =
6353 comparison.map { " \($0.change(ofModule: module.name, now: module.lines).text) |" }
6453 ?? ""
6553 let regionCell = hasRegions ? " \(module.regions?.percentText ?? "–") |" : ""
6653 lines.append(
6753 "| \(module.name) | \(module.lines.percentText) |\(changeCell) "
6853 + "\(groupingDigits(missed)) | \(groupingDigits(module.lines.count)) |"
6953 + regionCell)
7053 }
7133 return lines.joined(separator: "\n") + "\n"
7233 }
73
747 private func regionLine(_ regions: CoverageCount) -> String {
757 let change =
767 comparison.map { ", \($0.regionChange(now: regions).text) since the baseline" } ?? ""
777 return "Region coverage: **\(regions.percentText)** "
787 + "(\(groupingDigits(regions.covered)) of \(groupingDigits(regions.total)) regions)"
797 + change + "."
807 }
81}