"""Tests for the separate line/branch coverage policy gate.""" from __future__ import annotations import importlib.util from pathlib import Path SCRIPT = Path(__file__).parents[1] / "scripts" / "check_coverage_thresholds.py" SPEC = importlib.util.spec_from_file_location("check_coverage_thresholds", SCRIPT) assert SPEC is not None and SPEC.loader is not None MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) def _report(line: float = 49.0, branch: float = 36.0) -> dict[str, object]: return { "totals": { "percent_statements_covered": line, "percent_branches_covered": branch, }, } def test_validate_coverage_accepts_exact_floors(): assert MODULE.validate_coverage( _report(), min_line=49.0, min_branch=36.0, ) == [] def test_validate_coverage_reports_each_regression(): failures = MODULE.validate_coverage( _report(line=48.9, branch=35.9), min_line=49.0, min_branch=36.0, ) assert failures == [ "line coverage 48.90% is below the 49.00% floor", "branch coverage 35.90% is below the 36.00% floor", ] def test_validate_coverage_rejects_malformed_totals(): assert MODULE.validate_coverage( {}, min_line=49.0, min_branch=36.0, ) == ["coverage report is missing the totals object"] def test_validate_coverage_rejects_non_numeric_metrics(): report = _report() report["totals"]["percent_statements_covered"] = True report["totals"]["percent_branches_covered"] = float("nan") assert MODULE.validate_coverage( report, min_line=49.0, min_branch=36.0, ) == [ "coverage report is missing numeric percent_statements_covered", "coverage report is missing numeric percent_branches_covered", ]