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] = []
1736 ) {
1836 self.comparison = baseline.map(BaselineComparison.init)
1936 self.modules = ModuleTotals.grouping(files)
2036 self.tests = tests
2136 self.testGroups = testGroups
2236 self.commit = commit
2336 }
24
2536 public var markdown: String {
2657 let total = LineSummary(adding: modules.map(\.lines))
2736 var lines = [commit.map { "### Coverage at \($0)" } ?? "### Coverage", ""]
2836 if let tests {
298 lines.append(
308 "**\(tests.totalText)**: \(groupingDigits(tests.passed)) passed, "
318 + "\(groupingDigits(tests.failed)) failed, \(groupingDigits(tests.skipped)) skipped."
328 )
338 }
3436 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 )
4036 }
4136 let totalChange =
4236 comparison.map { ", \($0.changeOfTotal(now: total).text) since the baseline" } ?? ""
4336 lines.append(
4436 "Line coverage: **\(total.percentText)** "
4536 + "(\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines)"
4636 + totalChange + ".")
4757 let regions = CoverageCount(addingPresent: modules.map(\.regions))
4836 if let regions {
4910 lines += ["", regionLine(regions)]
5010 }
5136 lines.append("")
5236 lines += moduleTable
5336 return lines.joined(separator: "\n") + "\n"
5436 }
55
56 /// Lines and, where the export counts them, regions, each in the same columns.
5736 private var moduleTable: [String] {
5855 let hasRegions = modules.contains { $0.regions != nil }
5936 let columns =
6036 columnTitles(share: "Line coverage", total: "Lines")
6136 + (hasRegions ? columnTitles(share: "Region coverage", total: "Regions") : [])
6236 return [
6336 Self.tableRow(["Module"] + columns),
6436 "| --- |" + String(repeating: " ---: |", count: columns.count),
6536 ]
6657 + modules.map { module in
6757 Self.tableRow(
6857 [module.name] + lineCells(module) + (hasRegions ? regionCells(module) : []))
6957 }
7036 }
71
7248 private func columnTitles(share: String, total: String) -> [String] {
7348 [share] + (comparison == nil ? [] : ["Change"]) + ["Missed", total]
7448 }
75
7657 private func lineCells(_ module: ModuleTotals) -> [String] {
7757 metricCells(
7857 share: module.lines.percentText,
7957 change: comparison?.change(ofModule: module.name, now: module.lines),
8057 covered: module.lines.covered, total: module.lines.count)
8157 }
82
83 /// Dashes where this module has no region data while others do.
8412 private func regionCells(_ module: ModuleTotals) -> [String] {
8512 guard let regions = module.regions else {
866 return columnTitles(share: "", total: "").map { _ in "–" }
8710 }
8810 return metricCells(
8910 share: regions.percentText,
9010 change: comparison?.regionChange(ofModule: module.name, now: regions),
9110 covered: regions.covered, total: regions.total)
9212 }
93
94 private func metricCells(
95 share: String, change: CoverageChange?, covered: Int, total: Int
9667 ) -> [String] {
9767 [share] + (change.map { [$0.text] } ?? [])
9867 + [groupingDigits(total - covered), groupingDigits(total)]
9967 }
100
10193 private static func tableRow(_ cells: [String]) -> String {
10293 "| \(cells.joined(separator: " | ")) |"
10393 }
104
10510 private func regionLine(_ regions: CoverageCount) -> String {
10610 let change =
10710 comparison.map { ", \($0.regionChange(now: regions).text) since the baseline" } ?? ""
10810 return "Region coverage: **\(regions.percentText)** "
10910 + "(\(groupingDigits(regions.covered)) of \(groupingDigits(regions.total)) regions)"
11010 + change + "."
11110 }
112}