Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): derive coverage-gap uncovered lines from the percentage (#2259)

JikSau-D1774178dc708
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py
index 8fce6d53a..8addc7b31 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/coverage_gap.py
@@ -37,9 +37,12 @@ class CoverageGapDetector:
cov = ctx.line_coverage_pct
total = ctx.total_coverable_lines
- uncovered = total - len(ctx.covered_lines or ())
- # Some parsers don't emit a per-line set; fall back to math.
- if uncovered <= 0:
+ if ctx.covered_lines:
+ uncovered = max(0, total - len(ctx.covered_lines))
+ else:
+ # Some parsers pin a file with a percentage and a total but no
+ # per-line set; count from the percentage rather than reading the
+ # absent set as "nothing is covered".
uncovered = round(total * (100.0 - cov) / 100.0)
deep_gap = cov < _LINE_COVERAGE_DEEP_GAP and total >= _MIN_FILE_SIZE
diff --git a/tests/unit/health/test_coverage_biomarkers.py b/tests/unit/health/test_coverage_biomarkers.py
index c9c5db26e..6a6cad396 100644
--- a/tests/unit/health/test_coverage_biomarkers.py
+++ b/tests/unit/health/test_coverage_biomarkers.py
@@ -10,6 +10,7 @@ from repowise.core.analysis.health.biomarkers.coverage_gradient import (
from repowise.core.analysis.health.biomarkers.untested_hotspot import (
UntestedHotspotDetector,
)
+from repowise.core.analysis.health.coverage import parse_repowise_json
def _ctx(
@@ -142,6 +143,41 @@ def test_coverage_gap_skips_well_covered() -> None:
assert CoverageGapDetector().detect(ctx) == []
+def test_coverage_gap_counts_from_the_percentage_when_no_hit_set_is_reported() -> None:
+ # A parser may pin a file with a percentage and a total and no per-line set.
+ # Reading the absent set as "nothing is covered" made 20 real uncovered
+ # lines report as 40/40 and clear the 25-line gate.
+ ctx = _ctx(line_cov=50.0, total_lines=40)
+ assert CoverageGapDetector().detect(ctx) == []
+
+
+def test_coverage_gap_severity_follows_the_percentage_count_not_the_total() -> None:
+ ctx = _ctx(line_cov=25.0, total_lines=120)
+ results = CoverageGapDetector().detect(ctx)
+ assert len(results) == 1
+ assert results[0].details["uncovered_lines"] == 90
+ assert results[0].severity == "medium"
+
+
+def test_coverage_gap_ranks_the_hit_set_ahead_of_the_percentage() -> None:
+ # The set is exact evidence; the percentage is the fallback, and rounded.
+ ctx = _ctx(line_cov=40.0, total_lines=100, covered_lines=set(range(1, 62)))
+ results = CoverageGapDetector().detect(ctx)
+ assert len(results) == 1
+ assert results[0].details["uncovered_lines"] == 39
+
+
+def test_coverage_gap_reads_a_repowise_json_entry_that_omits_the_hit_set() -> None:
+ report = parse_repowise_json(
+ '{"format": "repowise-coverage-v1", "files": {"src/example.py": '
+ '{"line_coverage_pct": 50.0, "total_coverable_lines": 40}}}'
+ )
+ fc = report.files[0]
+ assert fc.covered_lines == []
+ ctx = _ctx(line_cov=fc.line_coverage_pct, total_lines=fc.total_coverable_lines)
+ assert CoverageGapDetector().detect(ctx) == []
+
+
# ---------------------------------------------------------------------------
# coverage_gradient
# ---------------------------------------------------------------------------