Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Oct 18, 2024
1 parent b643574 commit e261785
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion .github/actions/test-summary/make-summary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ for node in failures {
print(messages.joined(separator: ". "))
}

if let coverage {
let table = try makeTable(
name: coverage.lastPathComponent,
coverage: JSONDecoder().decode(Coverage.self, from: Data(contentsOf: coverage))
)
print(table)
}

func makeTable(
total: Int,
failed: Int,
Expand All @@ -51,4 +59,52 @@ func makeTable(
<tr><td>\(junit.lastPathComponent)</td><td>\(total) ran</td><td>\(passed) passed</td><td>\(skipped) skipped</td><td>\(failed) failed</td></tr>
</table>
"""
}
}

func makeTable(
name: String,
coverage: Coverage
) -> String {
"""
<table>
<tr><td></td><td><b>Covered</b></td><td><b>Total</b></td><td><b>Coverage</b> ⏭️</td></tr>
<tr><td>\(name)</td><td>\(coverage.covered)</td><td>\(coverage.count)</td><td>\(coverage.percentString)</td></tr>
</table>
"""
}

struct Coverage: Decodable {
var count: Int
var covered: Int
var percent: Double

init(from decoder: any Decoder) throws {
var unkeyed = try decoder
.container(keyedBy: CodingKeys.self)
.nestedUnkeyedContainer(forKey: .data)
let container = try unkeyed
.nestedContainer(keyedBy: CodingKeys.self)
.nestedContainer(keyedBy: CodingKeys.self, forKey: .totals)
.nestedContainer(keyedBy: CodingKeys.self, forKey: .lines)

self.count = try container.decode(Int.self, forKey: .count)
self.covered = try container.decode(Int.self, forKey: .covered)
self.percent = try container.decode(Double.self, forKey: .percent)
}

enum CodingKeys: String, CodingKey {
case data
case totals
case lines
case count
case covered
case percent
}

var percentString: String {
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 2
return formatter.string(from: (percent / 100) as NSNumber) ?? "asdf"
}
}

0 comments on commit e261785

Please sign in to comment.