"""Coverage aliases require exact source identity and target-bound contexts.""" import json from pathlib import Path import shutil from coverage import CoverageData import pytest from scripts.check_release_partitions import validate from scripts.combine_release_evidence import combine from scripts.release_source_identity import source_files from scripts import release_source_identity from tests.release_partition_cases import POLICY_IDENTITY from tests.release_partition_cases import BINDING, VERSION, evidence, refresh @pytest.fixture(autouse=True) def policy_identity(monkeypatch): # Synthetic coverage bundles; real startup/ownership is exercised in fresh venvs. monkeypatch.setattr(release_source_identity, "bytecode_identity", lambda: POLICY_IDENTITY) def status_update(directory, **changes): path = directory / "status.json" status = json.loads(path.read_text()) status.update(changes) path.write_text(json.dumps(status)) def write_data(path, source, *, revision=BINDING["target_sha"]): path.unlink(missing_ok=True) data = CoverageData(basename=str(path)) data.set_context("release:" + revision) data.add_arcs({str(source): [(-1, 1), (1, -1), (-1, 2), (2, 4), (4, -1)]}) data.write() def test_installed_and_subprocess_origins_combine_with_checkout_branches(tmp_path): root, options = evidence(tmp_path) installed = tmp_path / "environment/site-packages/obliteratus" shutil.copytree(root / "obliteratus", installed) for directory in [options["expected"], *options["artifacts"].iterdir()]: status_update(directory, source_roots=[str(root / "obliteratus"), str(installed)]) gloo = options["artifacts"] / f"release-gloo-py{VERSION}-head-0" write_data(gloo / "coverage.data", installed / "example.py") write_data(gloo / "coverage.data.child.123", installed / "example.py") refresh(gloo) accepted = combine(report_root=root, workspace_root=root, prefix=tmp_path / "combined", junit=tmp_path / "junit.xml", **options) assert len(accepted["coverage"]) == 3 files = json.loads((tmp_path / "combined.json").read_text())["files"] assert set(files) == {"obliteratus/example.py"} assert files["obliteratus/example.py"]["summary"]["covered_branches"] == 2 @pytest.mark.parametrize("change", ["unknown-origin", "other-target-context", "empty-context"]) def test_readable_digest_matching_database_still_requires_source_identity(tmp_path, change): root, options = evidence(tmp_path) directory = options["artifacts"] / f"release-core-py{VERSION}-head-0" source = tmp_path / "unrelated/obliteratus/example.py" if change == "unknown-origin" else root / "obliteratus/example.py" write_data(directory / "coverage.data", source, revision="b" * 40 if change == "other-target-context" else BINDING["target_sha"]) if change == "empty-context": path = directory / "coverage.data" path.unlink() data = CoverageData(basename=str(path)) data.add_arcs({str(source): [(-1, 1), (1, -1)]}) data.write() refresh(directory) with pytest.raises(ValueError, match="origin|context"): validate(**options) def test_same_filename_with_different_base_contents_cannot_relabel_head_coverage(tmp_path): root, options = evidence(tmp_path) source = root / "obliteratus/example.py" source.write_text(source.read_text().replace("return 1", "return 2")) options.update(target="base", target_sha="b" * 40) for directory in [options["expected"], *options["artifacts"].iterdir()]: status_update(directory, target="base", target_sha="b" * 40, source_files=source_files(root)) if directory != options["expected"]: directory.rename(directory.with_name(directory.name.replace("-head-", "-base-"))) # Every manifest and digest agrees, but the database still carries HEAD's context. with pytest.raises(ValueError, match="context"): validate(**options) @pytest.mark.parametrize("field,value", [ ("project_digest", "b" * 64), ("lock_digest", "b" * 64), ("checkout_tree_digest", "b" * 64), ("coverage_version", "0.0"), ("python_full_version", "3.12.99"), ("source_files", {"obliteratus/example.py": "b" * 64}), ("source_roots", ["/different/obliteratus", "/different/site-packages/obliteratus"]), ]) def test_each_partition_matches_collected_lock_config_tool_and_source(tmp_path, field, value): _, options = evidence(tmp_path) status_update(options["artifacts"] / f"release-core-py{VERSION}-head-0", **{field: value}) with pytest.raises(ValueError, match="binding|origin"): validate(**options) @pytest.mark.parametrize("filename", ["obliteratus/example.py", "pyproject.toml", "uv.lock"]) def test_reporting_modified_source_or_config_discards_stale_success(tmp_path, filename): root, options = evidence(tmp_path) path = root / filename path.write_text(path.read_text() + "\n# changed after collection\n") prefix, junit = tmp_path / "combined", tmp_path / "junit.xml" outputs = [Path(str(prefix) + suffix) for suffix in (".json", ".xml", ".accepted.json")] for output in [*outputs, junit]: output.write_text("stale success") with pytest.raises(ValueError, match="report source"): combine(report_root=root, workspace_root=root, prefix=prefix, junit=junit, **options) assert not any(output.exists() for output in [*outputs, junit]) def test_report_cannot_combine_evidence_from_another_python_patch(tmp_path): root, options = evidence(tmp_path) for directory in [options["expected"], *options["artifacts"].iterdir()]: status_update(directory, python_full_version=VERSION + ".999") # All partitions agree with collection; the report interpreter must also agree. assert validate(**options)["python_full_version"] == VERSION + ".999" with pytest.raises(ValueError, match="Python identity"): combine(report_root=root, workspace_root=root, prefix=tmp_path / "combined", junit=tmp_path / "junit.xml", **options) assert not (tmp_path / "combined.accepted.json").exists() @pytest.mark.parametrize("when", ["before", "after"]) def test_report_policy_drift_cannot_publish_aggregate(tmp_path, monkeypatch, when): root, options = evidence(tmp_path) calls = [] def identity(): calls.append(True) if when == "before" or len(calls) == 2: raise ValueError("altered release bytecode startup policy") return POLICY_IDENTITY monkeypatch.setattr(release_source_identity, "bytecode_identity", identity) prefix, junit = tmp_path / "combined", tmp_path / "junit.xml" with pytest.raises(ValueError, match="altered release bytecode"): combine(report_root=root, workspace_root=root, prefix=prefix, junit=junit, **options) assert len(calls) == (1 if when == "before" else 2) assert not junit.exists() assert not list(tmp_path.glob("combined.*"))