mirror of
https://github.com/unclebob/mutator.git
synced 2026-11-26 18:58:50 +00:00
Count every line of a Swift coverage region.
crapper now reports a region as (start, end, count). The join still unpacked (line, count), so the Swift coverage tests crashed, and a mutation on the last line of a covered region was left uncovered. line_covered decides each line. A nested region with count zero stays uncovered, and a line in no region stays uncovered. An xccov summary is unchanged. The file-wide mutator run then found six older survivors in the path join. A trailing slash still marks the file, the first of two equal paths keeps its hits, and a shorter prefix beats a shorter suffix. Comparisons that could not change a result are gone. Proved by pytest (125 passed, 1 skipped; swift is not installed). crapper's worst score besides parse_args is _prepare_coverage at 9.4; _swift_hits is 6.0. dryer --threshold 0.8 reports no candidates. mutator on src/mutator/coverage.py killed 49, with 0 survived and 0 uncovered. Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
co-authored by
Robert C. Martin
parent
eb14237127
commit
9a3d4a353f
@@ -125,7 +125,7 @@ The baseline command has to pass before any mutant runs. A mutant's timeout is t
|
|||||||
./mutator --xcode-scheme App --xcode-destination 'platform=iOS Simulator,name=iPhone 16'
|
./mutator --xcode-scheme App --xcode-destination 'platform=iOS Simulator,name=iPhone 16'
|
||||||
```
|
```
|
||||||
|
|
||||||
Coverage is generated with crapper's commands unless `--use-existing-coverage` or `--no-coverage` is set. A file missing from the report is treated as uncovered, and the run says so. Swift coverage is the JSON crapper writes under `target/coverage/swift/`: llvm-cov export after `swift test --enable-code-coverage`, or `xcrun xccov view --report --json` after `xcodebuild test -enableCodeCoverage YES`. A hit region marks that line. An xccov summary has no regions, so the function's line is covered when that function has covered lines.
|
Coverage is generated with crapper's commands unless `--use-existing-coverage` or `--no-coverage` is set. A file missing from the report is treated as uncovered, and the run says so. Swift coverage is the JSON crapper writes under `target/coverage/swift/`: llvm-cov export after `swift test --enable-code-coverage`, or `xcrun xccov view --report --json` after `xcodebuild test -enableCodeCoverage YES`. A hit region marks every line from its start through its end. A nested region with count zero leaves those lines uncovered, and a line in no region is uncovered. An xccov summary has no regions, so the function's line is covered when that function has covered lines.
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
|
|||||||
+26
-14
@@ -47,17 +47,17 @@ def _as_suffix(longer: tuple[str, ...], shorter: tuple[str, ...]) -> int | None:
|
|||||||
def _suffix_rank(key_parts: tuple[str, ...], source: tuple[str, ...]) -> tuple[int, int] | None:
|
def _suffix_rank(key_parts: tuple[str, ...], source: tuple[str, ...]) -> tuple[int, int] | None:
|
||||||
"""Rank `(0, extra)` is a key that ends with the source. `(1, missing)` is the reverse.
|
"""Rank `(0, extra)` is a key that ends with the source. `(1, missing)` is the reverse.
|
||||||
|
|
||||||
An equal path is rank `(0, 0)`. A shorter key matches only when the source
|
An equal path is rank `(0, 0)`. A shorter key matches only as a suffix of
|
||||||
is strictly longer, so an equal path is not also a missing-part match.
|
a longer source.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not source or not key_parts:
|
if not source:
|
||||||
|
return None
|
||||||
|
if not key_parts:
|
||||||
return None
|
return None
|
||||||
extra = _as_suffix(key_parts, source)
|
extra = _as_suffix(key_parts, source)
|
||||||
if extra is not None:
|
if extra is not None:
|
||||||
return (0, extra)
|
return (0, extra)
|
||||||
if len(source) <= len(key_parts):
|
|
||||||
return None
|
|
||||||
missing = _as_suffix(source, key_parts)
|
missing = _as_suffix(source, key_parts)
|
||||||
if missing is None:
|
if missing is None:
|
||||||
return None
|
return None
|
||||||
@@ -71,21 +71,21 @@ def _rank(key_parts: tuple[str, ...], sources: list[tuple[str, ...]]) -> tuple[i
|
|||||||
same rank are ambiguous.
|
same rank are ambiguous.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
best: tuple[int, int] | None = None
|
found: list[tuple[int, int]] = []
|
||||||
for source in sources:
|
for source in sources:
|
||||||
rank = _suffix_rank(key_parts, source)
|
rank = _suffix_rank(key_parts, source)
|
||||||
if rank is None:
|
if rank is not None:
|
||||||
continue
|
found.append(rank)
|
||||||
if best is None or rank < best:
|
if not found:
|
||||||
best = rank
|
return None
|
||||||
return best
|
return min(found)
|
||||||
|
|
||||||
|
|
||||||
def _source_parts(candidates: list[str]) -> list[tuple[str, ...]]:
|
def _source_parts(candidates: list[str]) -> list[tuple[str, ...]]:
|
||||||
sources: list[tuple[str, ...]] = []
|
sources: list[tuple[str, ...]] = []
|
||||||
for candidate in candidates:
|
for candidate in candidates:
|
||||||
parts = _parts(candidate)
|
parts = _parts(candidate)
|
||||||
if parts and parts not in sources:
|
if parts:
|
||||||
sources.append(parts)
|
sources.append(parts)
|
||||||
return sources
|
return sources
|
||||||
|
|
||||||
@@ -202,10 +202,22 @@ def _jacoco_index(root: Path) -> dict[str, set[int]]:
|
|||||||
|
|
||||||
|
|
||||||
def _swift_hits(record) -> set[int]:
|
def _swift_hits(record) -> set[int]:
|
||||||
"""Lines crapper counted. A region is one line; an xccov summary is the function line."""
|
"""Lines the tests hit.
|
||||||
|
|
||||||
|
A region covers every line from its start through its end. `line_covered`
|
||||||
|
asks the innermost region, so a nested region with count zero leaves those
|
||||||
|
lines uncovered, and a line in no region stays uncovered. An xccov summary
|
||||||
|
has no regions, so the function line is covered when that function has
|
||||||
|
covered lines.
|
||||||
|
"""
|
||||||
|
|
||||||
if record.regions:
|
if record.regions:
|
||||||
return {line for line, count in record.regions if count > 0}
|
covered: set[int] = set()
|
||||||
|
for start, end, _count in record.regions:
|
||||||
|
for line in range(start, end + 1):
|
||||||
|
if record.line_covered(line):
|
||||||
|
covered.add(line)
|
||||||
|
return covered
|
||||||
return {line for line, covered, _executable in record.summaries if covered > 0}
|
return {line for line, covered, _executable in record.summaries if covered > 0}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from mutator.cli import run
|
||||||
from mutator.coverage import covered_lines
|
from mutator.coverage import covered_lines
|
||||||
|
|
||||||
|
|
||||||
@@ -78,6 +79,43 @@ def test_lcov_does_not_let_a_shorter_path_steal_hits(tmp_path):
|
|||||||
assert covered_lines(tmp_path, short, "python") == {2}
|
assert covered_lines(tmp_path, short, "python") == {2}
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_prefixed_report_path_beats_a_shorter_suffix(tmp_path):
|
||||||
|
source = _py_source(tmp_path)
|
||||||
|
parts = [part for part in source.resolve().as_posix().split("/") if part]
|
||||||
|
prefixed = "x/y/" + "/".join(parts)
|
||||||
|
shorter = "/".join(parts[1:])
|
||||||
|
_lcov(tmp_path, [(prefixed, 2), (shorter, 9)])
|
||||||
|
assert covered_lines(tmp_path, source, "python") == {2}
|
||||||
|
|
||||||
|
|
||||||
|
def _lcov(root: Path, records: list[tuple[str, int]]) -> None:
|
||||||
|
report = root / "target" / "coverage" / "python" / "lcov.info"
|
||||||
|
report.parent.mkdir(parents=True)
|
||||||
|
chunks = [f"SF:{name}\nDA:{line},1\nend_of_record\n" for name, line in records]
|
||||||
|
report.write_text("".join(chunks), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def _py_source(tmp_path: Path) -> Path:
|
||||||
|
source = tmp_path / "src" / "app.py"
|
||||||
|
source.parent.mkdir(parents=True)
|
||||||
|
source.write_text("def place():\n return 1\n", encoding="utf-8")
|
||||||
|
return source
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_trailing_slash_on_the_report_path_still_marks_the_file(tmp_path):
|
||||||
|
source = _py_source(tmp_path)
|
||||||
|
_lcov(tmp_path, [(source.resolve().as_posix() + "/", 2)])
|
||||||
|
assert covered_lines(tmp_path, source, "python") == {2}
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_first_of_two_equal_paths_keeps_its_hits(tmp_path):
|
||||||
|
source = _py_source(tmp_path)
|
||||||
|
parts = [part for part in source.resolve().as_posix().split("/") if part]
|
||||||
|
first = "pre/" + "/".join(parts)
|
||||||
|
_lcov(tmp_path, [(first, 2), (first + "/", 9)])
|
||||||
|
assert covered_lines(tmp_path, source, "python") == {2}
|
||||||
|
|
||||||
|
|
||||||
def test_go_profile_does_not_cross_files(tmp_path):
|
def test_go_profile_does_not_cross_files(tmp_path):
|
||||||
profile = tmp_path / "target" / "coverage" / "go" / "coverage.out"
|
profile = tmp_path / "target" / "coverage" / "go" / "coverage.out"
|
||||||
profile.parent.mkdir(parents=True)
|
profile.parent.mkdir(parents=True)
|
||||||
@@ -196,6 +234,85 @@ def test_swift_regions_with_no_hits_are_present_and_empty(tmp_path):
|
|||||||
assert covered_lines(empty, source, "swift") is None
|
assert covered_lines(empty, source, "swift") is None
|
||||||
|
|
||||||
|
|
||||||
|
# Line 5 is the end of a region that starts on line 4. Line 2 is in no region.
|
||||||
|
# Lines 6 and 7 are a nested region whose count is zero.
|
||||||
|
_PLACE = """func place(x: Int) -> Int {
|
||||||
|
let ready = true
|
||||||
|
let extra = 1
|
||||||
|
if x > 0 {
|
||||||
|
return x + 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _place(tmp_path: Path) -> Path:
|
||||||
|
source = tmp_path / "Sources" / "Demo" / "Place.swift"
|
||||||
|
source.parent.mkdir(parents=True)
|
||||||
|
source.write_text(_PLACE, encoding="utf-8")
|
||||||
|
return source
|
||||||
|
|
||||||
|
|
||||||
|
def _llvm_regions(root: Path, source: Path, regions: list) -> None:
|
||||||
|
_swift_report(
|
||||||
|
root,
|
||||||
|
{
|
||||||
|
"type": "llvm.coverage.json.export",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"files": [{"filename": str(source)}],
|
||||||
|
"functions": [{"regions": regions}],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _scan_line(output: str, fragment: str) -> str:
|
||||||
|
matches = [line for line in output.splitlines() if fragment in line]
|
||||||
|
assert len(matches) == 1
|
||||||
|
return matches[0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_multiline_covered_region_covers_the_mutation_on_its_end_line(tmp_path, capsys):
|
||||||
|
source = _place(tmp_path)
|
||||||
|
_llvm_regions(tmp_path, source, [[4, 1, 5, 20, 1, 0, 0, 0]])
|
||||||
|
assert covered_lines(tmp_path, source, "swift") == {4, 5}
|
||||||
|
code = run(["--scan", "--use-existing-coverage", "--root", str(tmp_path), str(source)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert code == 0
|
||||||
|
assert "uncovered" not in _scan_line(captured.out, ":5 + -> -")
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_nested_zero_count_region_leaves_its_lines_uncovered(tmp_path, capsys):
|
||||||
|
source = _place(tmp_path)
|
||||||
|
_llvm_regions(
|
||||||
|
tmp_path,
|
||||||
|
source,
|
||||||
|
[
|
||||||
|
[4, 1, 7, 14, 3, 0, 0, 0],
|
||||||
|
[6, 1, 7, 14, 0, 0, 0, 0],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert covered_lines(tmp_path, source, "swift") == {4, 5}
|
||||||
|
code = run(["--scan", "--use-existing-coverage", "--root", str(tmp_path), str(source)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert code == 0
|
||||||
|
assert "uncovered" not in _scan_line(captured.out, ":5 + -> -")
|
||||||
|
assert "uncovered" in _scan_line(captured.out, ":7 0 -> 1")
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_line_outside_every_swift_region_is_uncovered(tmp_path, capsys):
|
||||||
|
source = _place(tmp_path)
|
||||||
|
_llvm_regions(tmp_path, source, [[4, 1, 5, 20, 1, 0, 0, 0]])
|
||||||
|
assert covered_lines(tmp_path, source, "swift") == {4, 5}
|
||||||
|
code = run(["--scan", "--use-existing-coverage", "--root", str(tmp_path), str(source)])
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert code == 0
|
||||||
|
assert "uncovered" in _scan_line(captured.out, ":2 true -> false")
|
||||||
|
|
||||||
|
|
||||||
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user