"""Exercise the candidate runner/plugin against small real pytest subprocesses.""" import importlib.metadata import json from pathlib import Path from types import SimpleNamespace import sys import pytest from scripts import run_release_partition as runner from scripts.check_release_partitions import nodes, validate from scripts.combine_release_evidence import combine from scripts.release_test_environment import release_extras from scripts import release_source_identity from tests.release_partition_cases import POLICY_IDENTITY SHA = "a" * 40 @pytest.fixture def target(tmp_path, monkeypatch): # This fixture exercises runner/pytest contracts in the caller's environment. # Fresh owned venvs and actual startup behavior have separate subprocess tests. monkeypatch.setattr(runner, "bytecode_identity", lambda: POLICY_IDENTITY) monkeypatch.setattr(release_source_identity, "bytecode_identity", lambda: POLICY_IDENTITY) root = tmp_path / "target" (root / "obliteratus").mkdir(parents=True) (root / "tests").mkdir() (root / "obliteratus/__init__.py").write_text("") (root / "obliteratus/example.py").write_text("def choose(value):\n if value:\n return 1\n return 0\n") (root / "obliteratus/unused.py").write_text("def untouched():\n return 17\n") (root / "pyproject.toml").write_text( '[project]\nname="fixture"\n[project.optional-dependencies]\ndev=[]\nmcp=[]\n' '[tool.pytest.ini_options]\nmarkers=["gloo: classified worker test"]\n', ) (root / "uv.lock").write_text("version=1\n") (root / "tests/test_core.py").write_text( 'import pytest\nfrom obliteratus.example import choose\n' 'class TestCore:\n' ' @pytest.mark.parametrize("value", [True, False], ids=["a.b", "x::y"])\n' ' def test_value(self, value):\n assert choose(value) == int(value)\n' '@pytest.mark.skip(reason="optional fixture absent")\n' 'def test_optional():\n raise AssertionError("must remain skipped")\n', ) (root / "tests/test_gloo.py").write_text( 'import pytest\nfrom obliteratus.example import choose\n' '@pytest.mark.gloo\ndef test_value():\n assert choose(False) == 0\n', ) # Git binding and installed-source verification are tested separately below; # these small projects use the real runner, pytest, plugin, JUnit and coverage. monkeypatch.setattr(runner.subprocess, "check_output", lambda *a, **kw: SHA + "\n") monkeypatch.setattr(runner, "verify_checkout", lambda *args: "c" * 64) original = importlib.metadata.distribution monkeypatch.setattr(importlib.metadata, "distribution", lambda name: SimpleNamespace(locate_file=lambda path: root / path) if name == "obliteratus" else original(name)) durations = tmp_path / "durations.json" durations.write_text(json.dumps({"schema_version": 1, "source": "contract fixture", "default_seconds": 1, "files": {}})) return root, durations def execute(target, output, *, command="collect", kind=None, shard=None): root, durations = target return runner.execute(SimpleNamespace( root=root, output=output, command=command, candidate_sha=SHA, target_sha=SHA, target="head", kind=kind, shard=shard, durations=durations, )) def test_real_partitions_retain_exact_nodes_skips_and_unexecuted_denominator(target, tmp_path): expected, artifacts = tmp_path / "expected", tmp_path / "artifacts" version = f"{sys.version_info.major}.{sys.version_info.minor}" assert execute(target, expected) == 0 for kind, count in runner.SHARDS.items(): for index in range(count): output = artifacts / f"release-{kind}-py{version}-head-{index}" assert execute(target, output, command="run", kind=kind, shard=index) == 0 args = dict(artifacts=artifacts, expected=expected, candidate_sha=SHA, target_sha=SHA, target="head", python_version=version, core_result="success", gloo_result="success", checkout_origin=str(target[0] / "obliteratus")) accepted = validate(**args) assert accepted["node_counts"] == {"core": 3, "gloo": 1} assert accepted["outcomes"]["passed"] == 3 assert accepted["outcomes"]["skipped"][0]["message"] == "optional fixture absent" assert "tests/test_core.py::TestCore::test_value[x::y]" in accepted["nodes"]["core"] prefix, junit = tmp_path / "coverage-py3.12", tmp_path / "junit.xml" combine(report_root=target[0], workspace_root=target[0], prefix=prefix, junit=junit, **args) report = json.loads((tmp_path / "coverage-py3.12.json").read_text()) assert report["files"]["obliteratus/example.py"]["summary"]["covered_branches"] == 2 assert report["files"]["obliteratus/unused.py"]["summary"]["covered_lines"] == 0 assert report["files"]["obliteratus/unused.py"]["summary"]["num_statements"] == 2 assert (tmp_path / "coverage-py3.12.accepted.json").is_file() def test_collection_time_skip_is_recorded_and_explicitly_rejected(target, tmp_path): (target[0] / "tests/test_skip.py").write_text('import pytest\npytest.skip("module unavailable", allow_module_level=True)\n') output = tmp_path / "expected" assert execute(target, output) == 0 payload = json.loads((output / "collection.json").read_text()) assert payload["collection_skips"][0]["nodeid"] == "tests/test_skip.py" assert "module unavailable" in payload["collection_skips"][0]["reason"] with pytest.raises(ValueError, match="collection-time skips"): nodes(output / "collection.json") def test_collection_error_is_not_a_successful_empty_run(target, tmp_path): (target[0] / "tests/test_error.py").write_text('raise RuntimeError("broken collection")\n') output = tmp_path / "expected" assert execute(target, output) == 2 assert json.loads((output / "status.json").read_text())["exit_code"] == 2 def test_target_revision_must_match_actual_checkout(target, monkeypatch): monkeypatch.setattr(runner.subprocess, "check_output", lambda *a, **kw: "b" * 40 + "\n") with pytest.raises(ValueError, match="revision"): runner.target_metadata(target[0], SHA, SHA, "head") @pytest.mark.parametrize("extras,expected", [ ({"dev": []}, ["dev"]), ({"dev": [], "mcp": []}, ["dev", "mcp"]), ({"dev": [], "recovery": [], "mcp": []}, ["dev", "recovery", "mcp"]), ]) def test_each_target_selects_only_declared_extras_including_mcp_on_310(tmp_path, extras, expected): project = tmp_path / "pyproject.toml" project.write_text('[project]\nname="fixture"\n[project.optional-dependencies]\n' + "".join(f"{name}=[]\n" for name in extras)) assert release_extras(project) == expected def test_existing_context_is_not_silently_replaced(target, tmp_path): project = target[0] / "pyproject.toml" project.write_text(project.read_text() + '[tool.coverage.run]\ncontext="other-owner"\n') with pytest.raises(ValueError, match="context"): runner.coverage_config(target[0], tmp_path, SHA) def test_post_execution_test_mutation_cannot_publish_status(target, tmp_path, monkeypatch): before = (target[0] / "tests/test_core.py").read_bytes() def verify(root, revision): if (root / "tests/test_core.py").read_bytes() != before: raise ValueError("target tree differs from requested commit") return "c" * 64 monkeypatch.setattr(runner, "verify_checkout", verify) (target[0] / "tests/test_mutation.py").write_text( 'from pathlib import Path\n' 'Path("tests/test_core.py").write_text("def test_replacement(): pass\\n")\n', ) output = tmp_path / "evidence" with pytest.raises(ValueError, match="differs from requested commit"): execute(target, output) assert not (output / "status.json").exists() def test_fixture_scratch_never_enters_evidence_and_is_cleaned_after_failure(target, tmp_path, monkeypatch): runner_temp = tmp_path / "runner-temp" runner_temp.mkdir() monkeypatch.setenv("RUNNER_TEMP", str(runner_temp)) evidence = tmp_path / "evidence" (target[0] / "tests/test_core.py").write_text( 'from pathlib import Path\n' 'def test_fixture(tmp_path):\n' ' (tmp_path / "checkpoint.bin").write_bytes(b"large fixture placeholder")\n' f' Path({str(tmp_path / "scratch-path.txt")!r}).write_text(str(tmp_path))\n' ' assert False, "intentional diagnostic failure"\n', ) # A single core file deterministically occupies shard zero. assert execute(target, evidence, command="run", kind="core", shard=0) == 1 scratch = Path((tmp_path / "scratch-path.txt").read_text()) assert scratch.is_relative_to(runner_temp) assert not scratch.is_relative_to(evidence) assert not scratch.exists() assert not list(runner_temp.iterdir()) assert {"status.json", "collection.json", "junit.xml", "pytest.log", "coverage-config.toml", "coverage.data"} <= { path.name for path in evidence.iterdir() } assert not any(path.is_dir() for path in evidence.iterdir()) @pytest.mark.parametrize("failed_test", [False, True]) def test_post_execution_policy_tampering_retains_raw_diagnostics_without_success(target, tmp_path, monkeypatch, failed_test): calls = [] def identity(): calls.append(True) if len(calls) == 2: raise ValueError("altered release bytecode startup policy") return POLICY_IDENTITY monkeypatch.setattr(runner, "bytecode_identity", identity) (target[0] / "tests/test_core.py").write_text(f"def test_case(): assert {not failed_test!r}\n") output = tmp_path / "evidence" with pytest.raises(ValueError, match="altered release bytecode"): execute(target, output, command="run", kind="core", shard=0) assert len(calls) == 2 assert not (output / "status.json").exists() assert (output / "pytest.log").is_file() assert (output / "junit.xml").is_file() assert (output / "coverage.data").is_file()