Skip to content

Commit 8f46b3f

Browse files
committed
feat(test): coverage map health check (loud anti-rot)
1 parent 505eca2 commit 8f46b3f

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Fail loudly if the committed coverage map is stale or under-covers. Used by coverage-health.yml."""
2+
import datetime
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "toolchain"))
7+
from mfc.test.coverage import COVERAGE_MAP_PATH, load_map, map_health # noqa: E402
8+
from mfc.test.cases import list_cases # noqa: E402 (returns the current test list)
9+
10+
MAX_AGE_DAYS = 10
11+
MIN_FRACTION = 0.80
12+
13+
entries, meta = load_map(COVERAGE_MAP_PATH)
14+
if entries is None:
15+
sys.exit("Coverage map missing or corrupt.")
16+
current_keys = {b.to_case().coverage_key() for b in list_cases()}
17+
ok, msg = map_health(
18+
meta=meta,
19+
current_keys=current_keys,
20+
mapped_keys=set(entries),
21+
now=datetime.datetime.now(datetime.timezone.utc).isoformat(),
22+
max_age_days=MAX_AGE_DAYS,
23+
min_fraction=MIN_FRACTION,
24+
)
25+
print(msg)
26+
sys.exit(0 if ok else 1)

toolchain/mfc/test/coverage.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,17 @@ def format_summary(*, ran, total, reason, meta, now) -> str:
162162
else:
163163
age = "map age unknown"
164164
return f"Coverage selection: ran {ran}/{total} tests · {age} · {reason}"
165+
166+
167+
def map_health(*, meta, current_keys, mapped_keys, now, max_age_days, min_fraction):
168+
"""Return (ok, message). Loud anti-rot check used by the health workflow."""
169+
if not meta or not meta.get("built_at"):
170+
return False, "Coverage map has no build metadata."
171+
age = (datetime.datetime.fromisoformat(now) - datetime.datetime.fromisoformat(meta["built_at"])).days
172+
if age > max_age_days:
173+
return False, f"Coverage map is STALE: {age}d old (max {max_age_days}d). Refresh workflow may be broken."
174+
if current_keys:
175+
frac = len(current_keys & mapped_keys) / len(current_keys)
176+
if frac < min_fraction:
177+
return False, f"Coverage map under-covers: {frac:.0%} of current tests mapped (min {min_fraction:.0%})."
178+
return True, f"Coverage map healthy: {age}d old."

toolchain/mfc/test/test_coverage_unit.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from unittest.mock import patch
55

6-
from mfc.test.coverage import format_summary, get_changed_files, is_always_run_all, load_map, param_hash, save_map, select_tests
6+
from mfc.test.coverage import format_summary, get_changed_files, is_always_run_all, load_map, map_health, param_hash, save_map, select_tests
77

88

99
def test_param_hash_is_order_independent():
@@ -190,3 +190,39 @@ def test_summary_handles_missing_meta():
190190
now="2026-05-29T00:00:00+00:00",
191191
)
192192
assert "610/610" in s and "map age unknown" in s
193+
194+
195+
def test_health_ok():
196+
ok, msg = map_health(
197+
meta={"built_at": "2026-05-28T00:00:00+00:00", "n_tests": 600},
198+
current_keys=set(str(i) for i in range(600)),
199+
mapped_keys=set(str(i) for i in range(580)),
200+
now="2026-05-29T00:00:00+00:00",
201+
max_age_days=10,
202+
min_fraction=0.8,
203+
)
204+
assert ok, msg
205+
206+
207+
def test_health_stale_fails():
208+
ok, msg = map_health(
209+
meta={"built_at": "2026-05-01T00:00:00+00:00", "n_tests": 600},
210+
current_keys=set(["a"]),
211+
mapped_keys=set(["a"]),
212+
now="2026-05-29T00:00:00+00:00",
213+
max_age_days=10,
214+
min_fraction=0.8,
215+
)
216+
assert not ok and "stale" in msg.lower()
217+
218+
219+
def test_health_undercoverage_fails():
220+
ok, msg = map_health(
221+
meta={"built_at": "2026-05-28T00:00:00+00:00", "n_tests": 10},
222+
current_keys=set(str(i) for i in range(100)),
223+
mapped_keys=set(str(i) for i in range(50)),
224+
now="2026-05-29T00:00:00+00:00",
225+
max_age_days=10,
226+
min_fraction=0.8,
227+
)
228+
assert not ok and "coverage" in msg.lower()

0 commit comments

Comments
 (0)