Files
OBLITERATUS/tests/test_release_coverage_fragments.py

220 lines
9.5 KiB
Python

"""Zero-execution child fragments remain evidence without qualifying a partition."""
from contextlib import closing
import json
import os
from pathlib import Path
import sqlite3
import subprocess
import sys
from xml.etree import ElementTree as ET
from coverage import CoverageData
import pytest
from scripts import release_source_identity
from scripts.check_release_partitions import validate
from scripts.combine_release_evidence import combine
from scripts.run_release_partition import digest
from tests.release_partition_cases import BINDING, POLICY_IDENTITY, VERSION, evidence, refresh, write_coverage
CONTEXT = "release:" + BINDING["target_sha"]
@pytest.fixture
def bundle(tmp_path, monkeypatch):
# The fixtures' synthetic package has no installed startup hook. Real owned
# environment behavior is covered by test_release_bytecode_policy.py.
monkeypatch.setattr(release_source_identity, "bytecode_identity", lambda: POLICY_IDENTITY)
return evidence(tmp_path)
def core(bundle):
return bundle[1]["artifacts"] / f"release-core-py{VERSION}-head-0"
def unexecuted(path, source, contexts=()):
path.unlink(missing_ok=True)
data = CoverageData(basename=str(path))
data.add_arcs({})
data.touch_file(str(source))
for context in contexts:
data.set_context(context)
data.add_arcs({str(source): []})
data.write()
@pytest.mark.parametrize("contexts", [(), ("",), (CONTEXT,), ("", CONTEXT)])
def test_unexecuted_sidecars_retain_digests_and_combined_coverage(bundle, tmp_path, contexts):
root, options = bundle
before = combine(report_root=root, workspace_root=root, prefix=tmp_path / "before",
junit=tmp_path / "before-junit.xml", **options)
child = core(bundle) / "coverage.data.child.123"
unexecuted(child, root / "obliteratus/example.py", contexts)
refresh(core(bundle))
bound = {str(path): digest(path) for path in [child, *map(Path, before["coverage"])]}
result = combine(report_root=root, workspace_root=root, prefix=tmp_path / "after",
junit=tmp_path / "after-junit.xml", **options)
assert set(result["coverage"]) == set(before["coverage"]) | {str(child)}
assert result["unexecuted_sidecars"] == [{"path": str(child), "sha256": bound[str(child)]}]
assert json.loads((tmp_path / "before.json").read_text())["files"] == json.loads(
(tmp_path / "after.json").read_text())["files"]
assert {name: digest(Path(name)) for name in bound} == bound
@pytest.mark.parametrize("change", [
"corrupt", "zero-byte", "no-files", "line-mode", "unknown-source", "other-context",
"mixed-context", "blank-and-other-context", "mixed-line-data", "orphan-arc", "missing-table",
"unbound", "missing-hash", "wrong-hash", "missing-file", "changed-file",
])
def test_invalid_sidecars_cannot_use_unexecuted_exception(bundle, change):
root, options = bundle
directory = core(bundle)
child = directory / "coverage.data.child.123"
source = root / "obliteratus/example.py"
contexts = {"other-context": ("release:" + "b" * 40,),
"mixed-context": (CONTEXT, "release:" + "b" * 40),
"blank-and-other-context": ("", "release:" + "b" * 40)}.get(change, ())
unexecuted(child, root / "foreign.py" if change == "unknown-source" else source, contexts)
if change in {"corrupt", "zero-byte"}:
child.write_bytes(b"not sqlite" if change == "corrupt" else b"")
elif change in {"no-files", "line-mode"}:
child.unlink()
data = CoverageData(basename=str(child))
if change == "no-files":
data.add_arcs({})
else:
data.add_lines({})
data.touch_file(str(source))
data.write()
elif change in {"mixed-line-data", "orphan-arc", "missing-table"}:
with closing(sqlite3.connect(child)) as db, db:
if change == "missing-table":
db.execute("DROP TABLE arc")
else:
db.execute("INSERT INTO context(id, context) VALUES (1, ?)", (CONTEXT,))
if change == "mixed-line-data":
db.execute("INSERT INTO line_bits VALUES (1, 1, ?)", (b"\x02",))
else:
db.execute("INSERT INTO arc VALUES (999, 1, -1, 1)")
if change != "unbound":
refresh(directory)
if change in {"missing-hash", "wrong-hash"}:
status_path = directory / "status.json"
status = json.loads(status_path.read_text())
status["files"][child.name] = None if change == "missing-hash" else "0" * 64
status_path.write_text(json.dumps(status))
elif change == "missing-file":
child.unlink()
elif change == "changed-file":
child.write_bytes(b"altered after binding")
with pytest.raises(ValueError):
validate(**options)
@pytest.mark.parametrize("context", ["", "release:" + "b" * 40])
def test_executed_sidecar_still_requires_exact_context(bundle, context):
root, options = bundle
child = core(bundle) / "coverage.data.child.123"
data = CoverageData(basename=str(child))
data.set_context(context)
data.add_arcs({str(root / "obliteratus/example.py"): [(-1, 1), (1, -1)]})
data.write()
refresh(core(bundle))
with pytest.raises(ValueError, match="context"):
validate(**options)
@pytest.mark.parametrize("child_kind", ["absent", "unexecuted", "positive"])
def test_empty_primary_cannot_qualify_even_with_positive_sidecar(bundle, child_kind):
root, options = bundle
directory = core(bundle)
source = root / "obliteratus/example.py"
unexecuted(directory / "coverage.data", source)
child = directory / "coverage.data.child.123"
if child_kind == "positive":
write_coverage(child, source)
elif child_kind == "unexecuted":
unexecuted(child, source)
refresh(directory)
with pytest.raises(ValueError, match="coverage.data: coverage must contain measured branches"):
validate(**options)
def test_skipped_partition_keeps_existing_exception_but_not_all_skipped_target(bundle):
root, options = bundle
directory = core(bundle)
tree = ET.parse(directory / "junit.xml")
ET.SubElement(tree.find("testcase"), "skipped", {"message": "unavailable"})
tree.write(directory / "junit.xml")
unexecuted(directory / "coverage.data", root / "obliteratus/example.py")
refresh(directory)
assert validate(**options)["outcomes"]["passed"] == 1
gloo = options["artifacts"] / f"release-gloo-py{VERSION}-head-0"
tree = ET.parse(gloo / "junit.xml")
ET.SubElement(tree.find("testcase"), "skipped", {"message": "unavailable"})
tree.write(gloo / "junit.xml")
unexecuted(gloo / "coverage.data", root / "obliteratus/example.py")
refresh(gloo)
with pytest.raises(ValueError, match="all selected testcases were skipped"):
validate(**options)
def test_real_resource_tracker_fragment_survives_validation_and_combination(bundle, tmp_path):
root, options = bundle
directory = core(bundle)
primary = directory / "coverage.data"
primary.unlink()
config = tmp_path / "subprocess-coverage.ini"
config.write_text("[run]\nbranch = true\npatch = subprocess\n")
script = """
import json, runpy, sys
from multiprocessing import resource_tracker
from coverage import Coverage
cov = Coverage(config_file=sys.argv[1], source=[sys.argv[2]], context=sys.argv[3],
data_file=sys.argv[4], data_suffix=False)
cov.start()
module = runpy.run_path(sys.argv[2] + '/example.py')
assert module['choose'](True) == 1
resource_tracker.ensure_running()
print(json.dumps({'tracker_pid': resource_tracker._resource_tracker._pid}), flush=True)
cov.stop()
cov.save()
"""
env = {key: value for key, value in os.environ.items()
if not key.startswith("COVERAGE_") and key not in {"PYTHONPATH", "PYTHONHOME"}}
result = subprocess.run(
[sys.executable, "-B", "-c", script, str(config), str(root / "obliteratus"), CONTEXT, str(primary)],
cwd=tmp_path, env=env, capture_output=True, text=True, timeout=30,
)
assert result.returncode == 0, result.stdout + result.stderr
# communicate() also waits for the tracker's inherited stdout to close.
tracker_pid = json.loads(result.stdout)["tracker_pid"]
children = list(directory.glob("coverage.data.*"))
assert len(children) == 1
child = children[0]
assert f".pid{tracker_pid}." in child.name
data = CoverageData(basename=str(child))
data.read()
assert data.has_arcs() and data.measured_files() == {str(root / "obliteratus/example.py")}
assert data.measured_contexts() == set()
assert all(data.arcs(name) == [] for name in data.measured_files())
parent = CoverageData(basename=str(primary))
parent.read()
assert parent.measured_contexts() == {CONTEXT}
assert any(parent.arcs(name) for name in parent.measured_files())
refresh(directory)
hashes = {str(path): digest(path) for path in [primary, child]}
accepted = combine(report_root=root, workspace_root=root, prefix=tmp_path / "combined",
junit=tmp_path / "combined-junit.xml", **options)
assert accepted["unexecuted_sidecars"] == [{"path": str(child), "sha256": hashes[str(child)]}]
assert {str(primary), str(child)} <= set(accepted["coverage"])
assert {path: digest(Path(path)) for path in hashes} == hashes
report = json.loads((tmp_path / "combined.json").read_text())
assert set(report["files"]) == {"obliteratus/example.py"}
summary = report["files"]["obliteratus/example.py"]["summary"]
assert summary["num_statements"] == 4
assert summary["covered_branches"] == 1
assert summary["num_branches"] == 2