150 lines
4.5 KiB
Python
150 lines
4.5 KiB
Python
"""Focused coverage-helper tests for titan-iac quality modules."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import textwrap
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from testing.quality_coverage import compute_workspace_line_coverage, run_check
|
|
|
|
|
|
def test_compute_workspace_line_coverage_handles_missing_xml(tmp_path: Path) -> None:
|
|
"""Missing coverage XML should produce a zero workspace coverage score."""
|
|
|
|
contract = {"coverage": {"tracked_files": ["managed.py"]}}
|
|
assert (
|
|
compute_workspace_line_coverage(contract, tmp_path, tmp_path / "missing.xml")
|
|
== 0.0
|
|
)
|
|
|
|
|
|
def test_compute_workspace_line_coverage_averages_present_tracked_files(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Workspace coverage should average only tracked files that appear in the report."""
|
|
|
|
coverage_xml = tmp_path / "coverage.xml"
|
|
coverage_xml.write_text(
|
|
textwrap.dedent(
|
|
"""\
|
|
<coverage>
|
|
<packages>
|
|
<package>
|
|
<classes>
|
|
<class filename="alpha.py" line-rate="1.0" branch-rate="1.0" />
|
|
<class filename="beta.py" line-rate="0.5" branch-rate="1.0" />
|
|
</classes>
|
|
</package>
|
|
</packages>
|
|
</coverage>
|
|
"""
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
contract = {"coverage": {"tracked_files": ["alpha.py", "beta.py", "missing.py"]}}
|
|
assert compute_workspace_line_coverage(contract, tmp_path, coverage_xml) == 75.0
|
|
|
|
|
|
def test_run_check_keeps_relative_names_when_source_roots_do_not_match(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Relative filenames should remain relative when no declared source root contains them."""
|
|
|
|
coverage_xml = tmp_path / "coverage.xml"
|
|
unmatched_root = tmp_path / "other-root"
|
|
unmatched_root.mkdir()
|
|
coverage_xml.write_text(
|
|
textwrap.dedent(
|
|
f"""\
|
|
<coverage>
|
|
<sources>
|
|
<source>{unmatched_root}</source>
|
|
</sources>
|
|
<packages>
|
|
<package>
|
|
<classes>
|
|
<class filename="relative.py" line-rate="0.80" branch-rate="1.0" />
|
|
</classes>
|
|
</package>
|
|
</packages>
|
|
</coverage>
|
|
"""
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
issues = run_check(
|
|
{"coverage": {"minimum_percent": 95.0, "tracked_files": ["relative.py"]}},
|
|
tmp_path,
|
|
coverage_xml,
|
|
)
|
|
|
|
assert issues == ["line coverage below 95.0%: relative.py (80.0%)"]
|
|
|
|
|
|
@pytest.mark.parametrize("branch_rate", [None, "nan", "inf", "-0.1", "1.1", "broken"])
|
|
def test_run_check_rejects_missing_or_invalid_branch_rates(
|
|
tmp_path: Path,
|
|
branch_rate: str | None,
|
|
) -> None:
|
|
"""Tracked files must carry finite Cobertura branch evidence."""
|
|
attribute = "" if branch_rate is None else f' branch-rate="{branch_rate}"'
|
|
coverage_xml = tmp_path / "coverage.xml"
|
|
coverage_xml.write_text(
|
|
f'<coverage><class filename="managed.py" line-rate="1"{attribute}/></coverage>',
|
|
encoding="utf-8",
|
|
)
|
|
|
|
issues = run_check(
|
|
{"coverage": {"tracked_files": ["managed.py"]}},
|
|
tmp_path,
|
|
coverage_xml,
|
|
)
|
|
|
|
assert len(issues) == 1
|
|
assert issues[0].startswith("coverage xml invalid:")
|
|
assert (
|
|
compute_workspace_line_coverage(
|
|
{"coverage": {"tracked_files": ["managed.py"]}},
|
|
tmp_path,
|
|
coverage_xml,
|
|
)
|
|
== 0.0
|
|
)
|
|
|
|
|
|
def test_run_check_enforces_line_and_branch_rates_independently(tmp_path: Path) -> None:
|
|
"""A strong line score must not hide weak branch coverage."""
|
|
coverage_xml = tmp_path / "coverage.xml"
|
|
coverage_xml.write_text(
|
|
'<coverage><class filename="managed.py" line-rate="1" branch-rate="0.5"/></coverage>',
|
|
encoding="utf-8",
|
|
)
|
|
|
|
assert run_check(
|
|
{"coverage": {"minimum_percent": 95, "tracked_files": ["managed.py"]}},
|
|
tmp_path,
|
|
coverage_xml,
|
|
) == ["branch coverage below 95.0%: managed.py (50.0%)"]
|
|
|
|
|
|
def test_run_check_accepts_cobertura_branchless_file_semantics(tmp_path: Path) -> None:
|
|
"""Coverage.py reports branchless files with branch-rate one."""
|
|
coverage_xml = tmp_path / "coverage.xml"
|
|
coverage_xml.write_text(
|
|
'<coverage><class filename="managed.py" line-rate="1" branch-rate="1"/></coverage>',
|
|
encoding="utf-8",
|
|
)
|
|
|
|
assert (
|
|
run_check(
|
|
{"coverage": {"minimum_percent": 95, "tracked_files": ["managed.py"]}},
|
|
tmp_path,
|
|
coverage_xml,
|
|
)
|
|
== []
|
|
)
|