mirror of
https://github.com/unclebob/crapper.git
synced 2026-11-26 18:18:49 +00:00
Merge pull request #47 from unclebob/cursor/swift-function-filenames-b371
Resolve Swift coverage regions through each function's filenames Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
+28
-8
@@ -449,6 +449,8 @@ def _regions_of(values, reader) -> list[tuple[int, int, int]]:
|
||||
|
||||
|
||||
def _region_file(region: list) -> int:
|
||||
"""File id. An index into this function's filenames, not the unit file list."""
|
||||
|
||||
if len(region) <= 5:
|
||||
return 0
|
||||
try:
|
||||
@@ -457,33 +459,51 @@ def _region_file(region: list) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def _add_function_region(found: dict[int, list[tuple[int, int, int]]], region) -> None:
|
||||
def _filename_at(names, index: int) -> str | None:
|
||||
"""The function filename `index` names, when that slot is a real path."""
|
||||
|
||||
if isinstance(names, list) and 0 <= index < len(names):
|
||||
name = names[index]
|
||||
if isinstance(name, str) and name:
|
||||
return name
|
||||
return None
|
||||
|
||||
|
||||
def _add_function_region(found: dict[str, list[tuple[int, int, int]]], names, region) -> None:
|
||||
parsed = _code_region(region)
|
||||
if parsed is None or not isinstance(region, list):
|
||||
return
|
||||
found.setdefault(_region_file(region), []).append(parsed)
|
||||
name = _filename_at(names, _region_file(region))
|
||||
if name is not None:
|
||||
found.setdefault(name, []).append(parsed)
|
||||
|
||||
|
||||
def _llvm_function_regions(unit: dict) -> dict[int, list[tuple[int, int, int]]]:
|
||||
found: dict[int, list[tuple[int, int, int]]] = {}
|
||||
def _llvm_function_regions(unit: dict) -> dict[str, list[tuple[int, int, int]]]:
|
||||
found: dict[str, list[tuple[int, int, int]]] = {}
|
||||
for function in unit.get("functions") or []:
|
||||
if not isinstance(function, dict):
|
||||
continue
|
||||
names = function.get("filenames")
|
||||
for region in function.get("regions") or []:
|
||||
_add_function_region(found, region)
|
||||
_add_function_region(found, names, region)
|
||||
return found
|
||||
|
||||
|
||||
def _llvm_unit(found: dict[str, SwiftFile], unit: dict) -> None:
|
||||
regions = _llvm_function_regions(unit)
|
||||
for index, file in enumerate(unit.get("files") or []):
|
||||
seen: set[str] = set()
|
||||
for file in unit.get("files") or []:
|
||||
if not isinstance(file, dict):
|
||||
continue
|
||||
name = file.get("filename")
|
||||
if not isinstance(name, str) or not name:
|
||||
continue
|
||||
chosen = regions.get(index) or _regions_of(file.get("segments"), _segment_region)
|
||||
seen.add(name)
|
||||
chosen = regions.get(name) or _regions_of(file.get("segments"), _segment_region)
|
||||
found.setdefault(name, SwiftFile()).regions.extend(chosen)
|
||||
for name, parsed in regions.items():
|
||||
if name not in seen:
|
||||
found.setdefault(name, SwiftFile()).regions.extend(parsed)
|
||||
|
||||
|
||||
def _llvm_files(data: dict) -> dict[str, SwiftFile]:
|
||||
@@ -603,7 +623,7 @@ def _summary_percent(summaries, start: int, end: int) -> float:
|
||||
inside = [item for item in summaries if start <= item[0] <= end]
|
||||
if not inside:
|
||||
return 0.0
|
||||
chosen = min(inside, key=lambda item: (abs(item[0] - start), item[0]))
|
||||
chosen = min(inside, key=lambda item: item[0])
|
||||
return coverage_percent(chosen[1], chosen[2])
|
||||
|
||||
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"type": "llvm.coverage.json.export",
|
||||
"version": "2.0.1",
|
||||
"data": [
|
||||
{
|
||||
"files": [
|
||||
{
|
||||
"filename": "/proj/DerivedSources/test_entry_point.swift",
|
||||
"segments": [
|
||||
[1, 1, 1, true, true, false]
|
||||
]
|
||||
},
|
||||
{
|
||||
"filename": "/proj/Sources/Demo/Place.swift",
|
||||
"segments": [
|
||||
[1, 1, 3, true, true, false],
|
||||
[2, 1, 3, true, true, false],
|
||||
[4, 1, 1, true, true, false],
|
||||
[5, 13, 3, true, false, false]
|
||||
]
|
||||
}
|
||||
],
|
||||
"functions": [
|
||||
{
|
||||
"name": "place(x:)",
|
||||
"count": 1,
|
||||
"filenames": [
|
||||
"/proj/Sources/Demo/Place.swift",
|
||||
"/proj/DerivedSources/test_entry_point.swift"
|
||||
],
|
||||
"regions": [
|
||||
[4, 6, 5, 13, 1, 0, 0, 0],
|
||||
[1, 1, 1, 8, 1, 1, 0, 0]
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -25,6 +25,11 @@ def function(**kwargs):
|
||||
return Function(**defaults)
|
||||
|
||||
|
||||
def test_a_single_covered_form_is_kept():
|
||||
html = '<span title="1 out of 1 forms covered"> 4 </span>'
|
||||
assert parse_form_coverage(html) == {4: (1, 1)}
|
||||
|
||||
|
||||
def test_cloverage_form_counts_and_lcov_lines():
|
||||
html = '<span class="pc" title="1 out of 2 forms covered"> 4 (if x 1)</span>'
|
||||
assert parse_form_coverage(html) == {4: (1, 2)}
|
||||
@@ -58,6 +63,11 @@ def test_lcov_branch_records_override_line_hits():
|
||||
assert bundle.percent_for(scored) == 50.0
|
||||
|
||||
|
||||
def test_an_untaken_branch_is_not_a_hit():
|
||||
text = "SF:src/demo/app.ts\nDA:3,1\nBRDA:3,0,0,0\nend_of_record\n"
|
||||
assert parse_lcov(text)["src/demo/app.ts"].branches[3] == (0, 1)
|
||||
|
||||
|
||||
def test_a_branch_count_that_is_not_a_number_is_ignored():
|
||||
text = "SF:src/demo/app.ts\nDA:3,1\nBRDA:3,0,0,nope\nend_of_record\n"
|
||||
parsed = parse_lcov(text)["src/demo/app.ts"]
|
||||
|
||||
@@ -164,6 +164,23 @@ def test_a_shorter_file_does_not_take_a_longer_files_coverage():
|
||||
assert bundle.percent_for(long) == 100.0
|
||||
|
||||
|
||||
def test_an_unrelated_longer_source_does_not_block_a_suffix_match(tmp_path):
|
||||
body = "package demo\nfunc Place() int { return 1 }\n"
|
||||
covered = tmp_path / "pkg" / "app.go"
|
||||
other = tmp_path / "other" / "long" / "name.go"
|
||||
covered.parent.mkdir(parents=True)
|
||||
other.parent.mkdir(parents=True)
|
||||
covered.write_text(body, encoding="utf-8")
|
||||
other.write_text(body, encoding="utf-8")
|
||||
bundle = CoverageBundle(
|
||||
go_profile=parse_go_profile("mode: set\nproj/src/pkg/app.go:1.1,2.2 1 1\n")
|
||||
)
|
||||
entries = analyze_files([covered, other], tmp_path, bundle)
|
||||
by_path = {entry.path: entry.coverage for entry in entries}
|
||||
assert by_path["pkg/app.go"] == 100.0
|
||||
assert by_path["other/long/name.go"] == 0.0
|
||||
|
||||
|
||||
def test_an_equal_report_key_blocks_a_longer_file():
|
||||
bundle = CoverageBundle(
|
||||
go_profile=parse_go_profile("mode: set\n/data/board.go:1.1,2.2 1 1\n")
|
||||
|
||||
+214
-1
@@ -317,6 +317,13 @@ def _snapshot(tmp_path: Path) -> str:
|
||||
return (tmp_path / ".metrics" / "crap.edn").read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def _xccov_snapshot(tmp_path: Path, report: dict) -> str:
|
||||
_swift_project(tmp_path, "xccov.json")
|
||||
destination = tmp_path / "target" / "coverage" / "swift" / "xccov.json"
|
||||
destination.write_text(json.dumps(report), encoding="utf-8")
|
||||
return _snapshot(tmp_path)
|
||||
|
||||
|
||||
def test_llvm_cov_regions_join_the_swift_snapshot(tmp_path):
|
||||
_swift_project(tmp_path, "llvm-cov.json")
|
||||
text = _snapshot(tmp_path)
|
||||
@@ -331,13 +338,144 @@ def _llvm_regions(filename: str, regions: list) -> str:
|
||||
"data": [
|
||||
{
|
||||
"files": [{"filename": filename}],
|
||||
"functions": [{"regions": regions}],
|
||||
"functions": [{"filenames": [filename], "regions": regions}],
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_a_function_region_follows_that_functions_filenames():
|
||||
text = (_FIXTURES / "function-filenames.json").read_text(encoding="utf-8")
|
||||
files = parse_swift_coverage(text)
|
||||
place = files["/proj/Sources/Demo/Place.swift"]
|
||||
entry = files["/proj/DerivedSources/test_entry_point.swift"]
|
||||
assert place.regions == [(4, 5, 1)]
|
||||
assert entry.regions == [(1, 1, 1)]
|
||||
assert place.line_covered(4)
|
||||
assert place.line_covered(5)
|
||||
assert not place.line_covered(1)
|
||||
assert entry.line_covered(1)
|
||||
assert not entry.line_covered(4)
|
||||
assert not entry.line_covered(5)
|
||||
|
||||
|
||||
def test_a_region_with_no_function_filenames_is_not_placed_by_unit_index():
|
||||
text = json.dumps(
|
||||
{
|
||||
"type": "llvm.coverage.json.export",
|
||||
"data": [
|
||||
{
|
||||
"files": [
|
||||
{"filename": "First.swift"},
|
||||
{"filename": "Second.swift"},
|
||||
],
|
||||
"functions": [{"regions": [[4, 1, 5, 1, 1, 0, 0, 0]]}],
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
files = parse_swift_coverage(text)
|
||||
assert not files["First.swift"].line_covered(4)
|
||||
assert not files["First.swift"].line_covered(5)
|
||||
assert not files["Second.swift"].line_covered(5)
|
||||
|
||||
|
||||
def test_a_region_file_id_outside_the_function_filenames_is_ignored():
|
||||
text = json.dumps(
|
||||
{
|
||||
"type": "llvm.coverage.json.export",
|
||||
"data": [
|
||||
{
|
||||
"files": [
|
||||
{"filename": "Other.swift"},
|
||||
{"filename": "Place.swift"},
|
||||
],
|
||||
"functions": [
|
||||
{
|
||||
"filenames": ["Place.swift"],
|
||||
"regions": [[4, 1, 5, 1, 1, 1, 0, 0]],
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
files = parse_swift_coverage(text)
|
||||
assert not files["Other.swift"].line_covered(5)
|
||||
assert not files["Place.swift"].line_covered(4)
|
||||
assert not files["Place.swift"].line_covered(5)
|
||||
|
||||
|
||||
def test_an_empty_function_filename_does_not_receive_the_region():
|
||||
text = json.dumps(
|
||||
{
|
||||
"type": "llvm.coverage.json.export",
|
||||
"data": [
|
||||
{
|
||||
"files": [{"filename": "Place.swift"}],
|
||||
"functions": [
|
||||
{
|
||||
"filenames": ["", "Place.swift"],
|
||||
"regions": [
|
||||
[4, 1, 5, 1, 1, 0, 0, 0],
|
||||
[2, 1, 2, 1, 1, 1, 0, 0],
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
files = parse_swift_coverage(text)
|
||||
assert "" not in files
|
||||
assert files["Place.swift"].regions == [(2, 2, 1)]
|
||||
assert files["Place.swift"].line_covered(2)
|
||||
assert not files["Place.swift"].line_covered(5)
|
||||
|
||||
|
||||
def test_a_function_filename_absent_from_the_unit_still_keeps_its_region():
|
||||
text = json.dumps(
|
||||
{
|
||||
"type": "llvm.coverage.json.export",
|
||||
"data": [
|
||||
{
|
||||
"files": [{"filename": "Other.swift"}],
|
||||
"functions": [
|
||||
{
|
||||
"filenames": ["Place.swift"],
|
||||
"regions": [[4, 1, 5, 1, 1, 0, 0, 0]],
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
files = parse_swift_coverage(text)
|
||||
assert files["Place.swift"].regions == [(4, 5, 1)]
|
||||
assert files["Place.swift"].line_covered(5)
|
||||
assert not files["Other.swift"].line_covered(5)
|
||||
|
||||
|
||||
def test_a_unit_file_with_an_empty_name_is_skipped():
|
||||
text = json.dumps(
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"files": [
|
||||
{"filename": "", "segments": [[1, 1, 1, True, True, False]]},
|
||||
{"filename": "Place.swift", "segments": [[2, 1, 1, True, True, False]]},
|
||||
],
|
||||
"functions": [],
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
files = parse_swift_coverage(text)
|
||||
assert "" not in files
|
||||
assert files["Place.swift"].line_covered(2)
|
||||
|
||||
|
||||
def test_a_region_records_its_end_line_and_covers_that_span():
|
||||
text = _llvm_regions(
|
||||
"Sources/Demo/Box.swift",
|
||||
@@ -393,6 +531,24 @@ def test_a_segment_region_is_a_single_line():
|
||||
)
|
||||
short = parse_swift_coverage(text)["Short.swift"]
|
||||
assert short.regions == [(2, 2, 1)]
|
||||
text = json.dumps(
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"files": [
|
||||
{
|
||||
"filename": "Gap.swift",
|
||||
"segments": [[2, 5, 1, True, False]],
|
||||
}
|
||||
],
|
||||
"functions": [],
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
gap = parse_swift_coverage(text)["Gap.swift"]
|
||||
assert gap.regions == []
|
||||
assert not gap.line_covered(2)
|
||||
text = (_FIXTURES / "segments.json").read_text(encoding="utf-8")
|
||||
record = parse_swift_coverage(text)["Sources/Demo/Box.swift"]
|
||||
assert (2, 2, 1) in record.regions
|
||||
@@ -407,6 +563,45 @@ def test_an_xccov_summary_does_not_mark_a_line_covered():
|
||||
assert record.line_covered(2) is False
|
||||
|
||||
|
||||
def test_an_xccov_file_with_an_empty_name_is_skipped():
|
||||
text = json.dumps(
|
||||
{
|
||||
"files": [
|
||||
{
|
||||
"name": "",
|
||||
"functions": [{"lineNumber": 1, "coveredLines": 1, "executableLines": 1}],
|
||||
},
|
||||
{
|
||||
"path": "Sources/Demo/Box.swift",
|
||||
"functions": [{"lineNumber": 1, "coveredLines": 1, "executableLines": 2}],
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
files = parse_swift_coverage(text)
|
||||
assert "" not in files
|
||||
assert files["Sources/Demo/Box.swift"].summaries == [(1, 1, 2)]
|
||||
|
||||
|
||||
def test_an_xccov_function_missing_either_count_is_ignored():
|
||||
text = json.dumps(
|
||||
{
|
||||
"files": [
|
||||
{
|
||||
"path": "Place.swift",
|
||||
"functions": [
|
||||
{"lineNumber": 4, "coveredLines": 1},
|
||||
{"executableLines": 1, "coveredLines": 1},
|
||||
{"lineNumber": 2, "executableLines": 1, "coveredLines": 1},
|
||||
],
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
record = parse_swift_coverage(text)["Place.swift"]
|
||||
assert record.summaries == [(2, 1, 1)]
|
||||
|
||||
|
||||
def test_a_spanning_region_still_counts_once_toward_crap(tmp_path):
|
||||
source = tmp_path / "Sources" / "Demo" / "Box.swift"
|
||||
source.parent.mkdir(parents=True)
|
||||
@@ -1090,6 +1285,24 @@ def test_an_xccov_function_without_counts_is_uncovered(tmp_path):
|
||||
assert ':name "choose", :namespace "Demo", :complexity 2, :coverage 0.0, :crap 6.0' in _snapshot(tmp_path)
|
||||
|
||||
|
||||
def test_an_xccov_summary_on_the_last_line_still_scores_the_function(tmp_path):
|
||||
report = json.loads((_FIXTURES / "xccov.json").read_text(encoding="utf-8"))
|
||||
report["targets"][0]["files"][0]["functions"][1]["lineNumber"] = 11
|
||||
text = _xccov_snapshot(tmp_path, report)
|
||||
assert ':name "open", :namespace "Demo.Box", :complexity 1, :coverage 100.0, :crap 1.0' in text
|
||||
|
||||
|
||||
def test_the_earliest_xccov_summary_inside_the_function_scores_it(tmp_path):
|
||||
report = json.loads((_FIXTURES / "xccov.json").read_text(encoding="utf-8"))
|
||||
report["targets"][0]["files"][0]["functions"] = [
|
||||
{"coveredLines": 2, "executableLines": 2, "lineNumber": 1, "name": "choose(x:)"},
|
||||
{"coveredLines": 0, "executableLines": 2, "lineNumber": 4, "name": "later"},
|
||||
{"coveredLines": 1, "executableLines": 1, "lineNumber": 9, "name": "Box.open()"},
|
||||
]
|
||||
text = _xccov_snapshot(tmp_path, report)
|
||||
assert ':name "choose", :namespace "Demo", :complexity 2, :coverage 100.0, :crap 2.0' in text
|
||||
|
||||
|
||||
def test_xccov_report_joins_on_the_function_line(tmp_path):
|
||||
_swift_project(tmp_path, "xccov.json")
|
||||
text = _snapshot(tmp_path)
|
||||
|
||||
Reference in New Issue
Block a user