mirror of
https://github.com/unclebob/mutator.git
synced 2026-11-26 18:58:50 +00:00
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:
co-authored by
Robert C. Martin
parent
7c72d9deba
commit
3f6dc5bb7c
+22
-2
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
Java uses JaCoCo source lines. Go uses statement profiles, which name a
|
Java uses JaCoCo source lines. Go uses statement profiles, which name a
|
||||||
file by its module import path, the way crapper names it. Clojure prefers
|
file by its module import path, the way crapper names it. Clojure prefers
|
||||||
Cloverage's per-line form counts, then LCOV. The other languages use LCOV.
|
Cloverage's per-line form counts, then LCOV. Swift uses the regions crapper
|
||||||
A file that does not appear in a report has no coverage data.
|
reads from llvm-cov JSON, or an xccov function summary when the report has
|
||||||
|
no regions. The other languages use LCOV. A file that does not appear in a
|
||||||
|
report has no coverage data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -199,6 +201,22 @@ def _jacoco_index(root: Path) -> dict[str, set[int]]:
|
|||||||
return found
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def _swift_hits(record) -> set[int]:
|
||||||
|
"""Lines crapper counted. A region is one line; an xccov summary is the function line."""
|
||||||
|
|
||||||
|
if record.regions:
|
||||||
|
return {line for line, count in record.regions if count > 0}
|
||||||
|
return {line for line, covered, _executable in record.summaries if covered > 0}
|
||||||
|
|
||||||
|
|
||||||
|
def _swift_lines(bundle, source_path: str) -> set[int] | None:
|
||||||
|
crapper = ensure_crapper()
|
||||||
|
record = crapper.coverage._lookup(bundle.swift, source_path, bundle.source_parts)
|
||||||
|
if record is None:
|
||||||
|
return None
|
||||||
|
return _swift_hits(record)
|
||||||
|
|
||||||
|
|
||||||
def _go_lines(root: Path, profile, source_path: str) -> set[int] | None:
|
def _go_lines(root: Path, profile, source_path: str) -> set[int] | None:
|
||||||
if not profile:
|
if not profile:
|
||||||
return None
|
return None
|
||||||
@@ -226,6 +244,8 @@ def covered_lines(root: Path, source_path: Path, language: str) -> set[int] | No
|
|||||||
bundle = crapper.coverage.load_bundle(root)
|
bundle = crapper.coverage.load_bundle(root)
|
||||||
if language == "go":
|
if language == "go":
|
||||||
return _go_lines(root, bundle.go_profile, path)
|
return _go_lines(root, bundle.go_profile, path)
|
||||||
|
if language == "swift":
|
||||||
|
return _swift_lines(bundle, path)
|
||||||
if language == "clojure":
|
if language == "clojure":
|
||||||
html = _covered_from_pairs(_lookup(bundle.form_html, path))
|
html = _covered_from_pairs(_lookup(bundle.form_html, path))
|
||||||
if html is not None:
|
if html is not None:
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from mutator.coverage import covered_lines
|
from mutator.coverage import covered_lines
|
||||||
|
|
||||||
|
|
||||||
@@ -112,6 +115,87 @@ def _go_source(path):
|
|||||||
return 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):
|
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 test -coverprofile` names files by module import path, not by disk path.
|
||||||
_go_module(
|
_go_module(
|
||||||
|
|||||||
@@ -330,6 +330,35 @@ def test_a_run_invokes_swift_test_and_xcodebuild_with_the_destination(tmp_path,
|
|||||||
assert destination in xcode_text.splitlines()
|
assert destination in xcode_text.splitlines()
|
||||||
|
|
||||||
|
|
||||||
|
def test_scan_marks_a_swift_line_uncovered_when_llvm_cov_did_not_hit_it(tmp_path, capsys):
|
||||||
|
import json
|
||||||
|
|
||||||
|
source = tmp_path / "Sources" / "Demo" / "Box.swift"
|
||||||
|
source.parent.mkdir(parents=True)
|
||||||
|
source.write_text("func place(x: Int) -> Int {\n return x > 0 ? x : 0\n}\n", encoding="utf-8")
|
||||||
|
report = tmp_path / "target" / "coverage" / "swift" / "llvm-cov.json"
|
||||||
|
report.parent.mkdir(parents=True)
|
||||||
|
report.write_text(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"type": "llvm.coverage.json.export",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"files": [{"filename": str(source)}],
|
||||||
|
"functions": [{"regions": [[1, 1, 1, 2, 1, 0, 0, 0]]}],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
),
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
code = run(["--scan", "--use-existing-coverage", "--root", str(tmp_path), str(source)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert code == 0
|
||||||
|
assert "uncovered" in captured.out
|
||||||
|
assert "x > 0 -> !(x > 0)" in captured.out or "> -> >=" in captured.out
|
||||||
|
|
||||||
|
|
||||||
def test_a_swift_run_records_killed_and_survived_on_the_crapper_namespace(tmp_path, capsys):
|
def test_a_swift_run_records_killed_and_survived_on_the_crapper_namespace(tmp_path, capsys):
|
||||||
relative = "Sources/Demo/Place.swift"
|
relative = "Sources/Demo/Place.swift"
|
||||||
path = tmp_path / relative
|
path = tmp_path / relative
|
||||||
|
|||||||
Reference in New Issue
Block a user