Read Swift coverage from the reports crapper already parses.

llvm-cov JSON and xccov JSON land in target/coverage/swift. crapper
turns those into regions or function summaries. A region with a hit
marks that line covered. An xccov summary has no lines, so the function
line is covered when the summary's covered count is above zero. A file
missing from the report stays uncovered and is not run.

Proved by tests/test_coverage.py, which failed first because a Swift
file with a report still returned no lines, and by a scan that marks
an unhit line uncovered. The reports are the same JSON shapes crapper
reads; mutator does not parse them itself.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 00:00:50 +00:00
co-authored by Robert C. Martin
parent 7c72d9deba
commit 3f6dc5bb7c
3 changed files with 135 additions and 2 deletions
+84
View File
@@ -1,3 +1,6 @@
import json
from pathlib import Path
from mutator.coverage import covered_lines
@@ -112,6 +115,87 @@ def _go_source(path):
return path
def _swift_report(root: Path, payload: dict) -> None:
report = root / "target" / "coverage" / "swift" / "llvm-cov.json"
report.parent.mkdir(parents=True)
report.write_text(json.dumps(payload), encoding="utf-8")
def test_swift_llvm_cov_regions_mark_hit_lines(tmp_path):
source = tmp_path / "Sources" / "Demo" / "Box.swift"
source.parent.mkdir(parents=True)
source.write_text("func choose(x: Int) -> Int { 1 }\n", encoding="utf-8")
_swift_report(
tmp_path,
{
"type": "llvm.coverage.json.export",
"data": [
{
"files": [{"filename": str(source)}],
"functions": [
{
"regions": [
[2, 1, 2, 2, 1, 0, 0, 0],
[4, 1, 4, 2, 0, 0, 0, 0],
[3, 9, 3, 18, 0, 0, 0, 4],
]
}
],
}
],
},
)
assert covered_lines(tmp_path, source, "swift") == {2}
assert covered_lines(tmp_path, tmp_path / "Sources" / "Demo" / "Missing.swift", "swift") is None
def test_swift_xccov_summaries_mark_a_covered_function_line(tmp_path):
source = tmp_path / "Sources" / "Demo" / "Box.swift"
source.parent.mkdir(parents=True)
source.write_text("func choose(x: Int) -> Int { 1 }\n", encoding="utf-8")
_swift_report(
tmp_path,
{
"targets": [
{
"files": [
{
"path": "Sources/Demo/Box.swift",
"functions": [
{"lineNumber": 1, "coveredLines": 1, "executableLines": 2},
{"lineNumber": 9, "coveredLines": 0, "executableLines": 1},
],
}
]
}
]
},
)
assert covered_lines(tmp_path, source, "swift") == {1}
def test_swift_regions_with_no_hits_are_present_and_empty(tmp_path):
source = tmp_path / "Sources" / "Demo" / "Box.swift"
source.parent.mkdir(parents=True)
source.write_text("func choose(x: Int) -> Int { 1 }\n", encoding="utf-8")
_swift_report(
tmp_path,
{
"type": "llvm.coverage.json.export",
"data": [
{
"files": [{"filename": str(source)}],
"functions": [{"regions": [[2, 1, 2, 2, 0, 0, 0, 0]]}],
}
],
},
)
assert covered_lines(tmp_path, source, "swift") == set()
empty = tmp_path / "other"
empty.mkdir()
assert covered_lines(empty, source, "swift") is None
def test_go_profile_keyed_by_import_path_marks_hit_lines(tmp_path):
# `go test -coverprofile` names files by module import path, not by disk path.
_go_module(