From 4cb41467dd59860c4e1d701c593da4091a4602c3 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Wed, 7 Oct 2026 03:23:47 +0200 Subject: [PATCH] Add raw LCOV function diagnostics --- scripts/check-changed-scope-coverage.py | 39 +++++++++++++++++++++++++ scripts/test-changed-scope-coverage.py | 27 +++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/scripts/check-changed-scope-coverage.py b/scripts/check-changed-scope-coverage.py index 5e674a10..016d52a7 100644 --- a/scripts/check-changed-scope-coverage.py +++ b/scripts/check-changed-scope-coverage.py @@ -420,6 +420,40 @@ def coverage_report(records: list[dict], scopes: dict[str, tuple[list[dict], set return {"baseline": BASELINE, "files": files, "totals": totals} +def raw_function_diagnostics(records: list[dict], scopes: dict[str, tuple[list[dict], set[int]]]) -> str: + """Render raw LCOV function rows for selected zero-hit entries without changing metrics.""" + by_path = _merge_lcov(records) + rows = ["Raw LCOV FN/FNDA diagnostics for selected zero-hit functions:"] + for path in RUST_FILES: + functions, test_lines = scopes[path] + source_records = by_path.get(path, []) + metrics = _file_metrics(source_records, functions, test_lines) + misses = {(item["line"], item["name"]) + for item in metrics["functions"]["misses"]} + if not misses: + continue + raw_fn = sorted({(line, name) for record in source_records for line, name in record["fn"] + if _inside(line, functions, test_lines)}) + raw_fnda = {} + for record in source_records: + for count, name in record["fnda"]: + raw_fnda.setdefault(name, []).append(count) + rows.append(f"{path}:") + for line, name in sorted(misses): + rows.append(f" zero-hit FN:{line},{name}") + enclosing = [item for item in functions if item["start"] <= line <= item["end"]] + related = [(fn_line, fn_name) for fn_line, fn_name in raw_fn + if any(item["start"] <= fn_line <= item["end"] for item in enclosing)] + for fn_line, fn_name in related: + counts = raw_fnda.get(fn_name, []) + raw_counts = ",".join(str(value) for value in counts) if counts else "" + rows.append(f" raw FN:{fn_line},{fn_name}") + rows.append(f" exact raw FNDA:{raw_counts},{fn_name}") + if len(rows) == 1: + return "" + return "\n".join(rows) + "\n" + + def _run(command: list[str], root: Path, env: dict | None = None) -> subprocess.CompletedProcess: return subprocess.run(command, cwd=root, env=env, text=True, capture_output=True, check=False) @@ -636,6 +670,11 @@ def run_coverage(root: Path, out: Path) -> int: state["failure"] = f"{type(error).__name__}: {error}" _write_summary(out, state) print((out / "summary.txt").read_text(), end="") + if "totals" in state: + diagnostics = raw_function_diagnostics(parse_lcov((out / "coverage.raw.lcov").read_text()), scopes) + if diagnostics: + (out / "function-diagnostics.txt").write_text(diagnostics) + print(diagnostics, end="") return 0 if state["passed"] else 1 diff --git a/scripts/test-changed-scope-coverage.py b/scripts/test-changed-scope-coverage.py index b98d0e9a..76f03821 100644 --- a/scripts/test-changed-scope-coverage.py +++ b/scripts/test-changed-scope-coverage.py @@ -100,6 +100,33 @@ pub fn with_test_only_block() { self.assertFalse(any("a.rs:4" in line for line in COVERAGE.summary_lines( {**report, "passed": True}))) + def test_raw_function_diagnostics_show_fn_and_exact_fnda_without_changing_metrics(self) -> None: + path = "control-plane/src/kar_writer.rs" + lcov = (f"SF:{path}\nFN:10,crate::write_section\nFNDA:1,crate::write_section\n" + "FN:12,crate::write_section::{closure#0}\n" + "FNDA:0,crate::write_section::{closure#0}\nDA:10,1\nDA:12,0\n" + "BRDA:12,0,0,0\nend_of_record\n") + records = [COVERAGE.parse_lcov(lcov)[0]] + records.extend(COVERAGE.parse_lcov(_lcov(other))[0] + for other in COVERAGE.RUST_FILES if other != path) + scopes = {name: ([], set()) for name in COVERAGE.RUST_FILES} + scopes[path] = ([{"name": "write_section", "start": 10, "end": 14}], set()) + before = COVERAGE.coverage_report(records, scopes) + diagnostics = COVERAGE.raw_function_diagnostics(records, scopes) + after = COVERAGE.coverage_report(records, scopes) + self.assertEqual(before, after) + self.assertIn("zero-hit FN:12,crate::write_section::{closure#0}", diagnostics) + self.assertIn("raw FN:12,crate::write_section::{closure#0}", diagnostics) + self.assertIn("exact raw FNDA:0,crate::write_section::{closure#0}", diagnostics) + self.assertNotIn("zero-hit FN:10,crate::write_section", diagnostics) + + def test_raw_function_diagnostics_are_empty_when_all_selected_functions_hit(self) -> None: + path = "control-plane/src/kar_writer.rs" + records = COVERAGE.parse_lcov(_lcov(path))[0:] + scopes = {name: ([], set()) for name in COVERAGE.RUST_FILES} + scopes[path] = ([{"name": "live", "start": 1, "end": 3}], set()) + self.assertEqual(COVERAGE.raw_function_diagnostics(records, scopes), "") + def test_cargo_json_selects_only_the_package_lib_test_executable(self) -> None: with tempfile.TemporaryDirectory() as directory: binary = Path(directory) / "strata_control-test"