"""Release partition wiring preserves coverage, diagnostic, and resource contracts.""" from itertools import product from collections import Counter import os from pathlib import Path import subprocess import sys import pytest import yaml from scripts.run_release_partition import SHARDS, pytest_command from scripts.shard_pr_tests import has_gloo_marker, load_durations, shard_tests ROOT = Path(__file__).parents[1] WORKFLOW = yaml.safe_load((ROOT / ".github/workflows/ci.yml").read_text()) JOBS = WORKFLOW["jobs"] def step(job, name): return next(item for item in JOBS[job]["steps"] if item["name"] == name) def cells(matrix): axes = {key: value for key, value in matrix.items() if key != "exclude"} result = [] for values in product(*axes.values()): cell = dict(zip(axes, values)) if not any(all(cell[key] == value for key, value in excluded.items()) for excluded in matrix.get("exclude", [])): result.append(cell) return result @pytest.mark.parametrize("kind,count,timeout", [("core", 6, 30), ("gloo", 8, 10)]) def test_release_execution_matrix_covers_each_target_once_per_shard(kind, count, timeout): job = JOBS[f"release-{kind}-tests"] actual = cells(job["strategy"]["matrix"]) expected = [{"python-version": version, "target": target, "shard": shard} for version, target in [("3.11", "head"), ("3.12", "head"), ("3.12", "base")] for shard in range(count)] assert sorted(actual, key=str) == sorted(expected, key=str) assert job["strategy"]["fail-fast"] is False assert job["timeout-minutes"] == timeout assert job["needs"] == "release-select" assert SHARDS[kind] == count def test_release_gloo_isolates_heavy_files_and_assigns_bulk_collection_once(): selected = sorted(path.relative_to(ROOT).as_posix() for path in (ROOT / "tests").rglob("test_*.py")) default, weights = load_durations(ROOT / "ci/gloo-test-durations.json") shards = shard_tests(selected, default, weights, root=ROOT, shards=SHARDS["gloo"], kind="gloo") assert Counter(name for shard in shards for name in shard) == Counter(selected) assert shards[:7] == tuple(["tests/" + name] for name in ( "test_distributed_verification_gloo.py", "test_distributed_surgery_gloo.py", "test_distributed_gloo.py", "test_distributed_rank_local_gloo.py", "test_distributed_export_gloo.py", "test_distributed_probe_gloo.py", "test_distributed_model_gloo.py", )) marked = [[name for name in shard if has_gloo_marker((ROOT / name).read_text())] for shard in shards] assert all(marked) assert set(marked[7]) == {"tests/test_distributed_export_receipts.py", "tests/test_distributed_session_boundaries.py", "tests/test_distributed_rank_local_e4_gloo.py"} assert len(shards[7]) == len(selected) - 7 @pytest.mark.parametrize("kind", ["core", "gloo"]) def test_partition_upload_cannot_include_pytest_fixture_trees(tmp_path, kind): upload = step(f"release-{kind}-tests", "Upload release partition evidence") assert upload["if"] == "always()" assert upload["with"]["retention-days"] == 90 artifact = tmp_path / upload["with"]["path"] command = pytest_command(tmp_path, artifact, scratch=tmp_path / "runner-temp", collect=False, kind=kind) scratch = Path(command[command.index("--basetemp") + 1]) assert not scratch.is_relative_to(artifact) assert Path(command[command.index("--junitxml") + 1]).is_relative_to(artifact) assert Path(command[command.index("--release-nodeids") + 1]).is_relative_to(artifact) assert JOBS[f"release-{kind}-tests"]["env"]["PYTHONDONTWRITEBYTECODE"] == "1" def test_release_jobs_preserve_read_only_permissions_and_diagnostics(): assert WORKFLOW["permissions"] == {"contents": "read"} for name in ("release-select", "release-core-tests", "release-gloo-tests", "test"): assert "permissions" not in JOBS[name] report = JOBS["test"] assert report["if"].startswith("always() &&") assert report["strategy"] == {"fail-fast": False, "matrix": {"python-version": ["3.11", "3.12"]}} assert report["timeout-minutes"] == 30 for target in ("head", "base"): download = step("test", f"Download {target} release partitions") assert download["if"].startswith("always()") assert download["with"]["merge-multiple"] is False upload = next(item for item in report["steps"] if "upload-artifact@" in item.get("uses", "")) assert upload["if"] == "always()" assert upload["with"]["name"] == "test-evidence-py${{ matrix.python-version }}" assert upload["with"]["retention-days"] == 90 def test_base_is_resolved_once_and_propagated_without_downstream_fallback(): selection = step("release-select", "Resolve exact release comparison base once")["run"] assert '${CANDIDATE_SHA}^' in selection for name in ("release-core-tests", "release-gloo-tests", "test"): job = JOBS[name] text = yaml.safe_dump(job) assert "needs.release-select.outputs.coverage_base" in text assert "EVENT_BASE" not in text and "HEAD^" not in text and "${CANDIDATE_SHA}^" not in text assert "--locked --no-default-groups" in text assert "scripts.release_test_environment" in text assert "extra spaces" not in text @pytest.mark.parametrize("job,step_name,variable", [ ("release-core-tests", "Install locked package and test tools", "TEST_ENV"), ("release-gloo-tests", "Install locked package and test tools", "TEST_ENV"), ("test", "Install locked package and test tools", "TEST_ENV"), ("test", "Generate exact-base coverage for module regression comparison", "BASE_TEST_ENV"), ]) def test_disposable_policy_ownership_precedes_sync_and_install_follows_final_sync(job, step_name, variable): script = step(job, step_name)["run"] create = f'python -B "$RELEASE_TOOLS/scripts/release_bytecode_policy.py" create --environment "${variable}"' install = f'"${variable}/bin/python" -B "$RELEASE_TOOLS/scripts/release_bytecode_policy.py" install --environment "${variable}"' verify = f'"${variable}/bin/python" "$RELEASE_TOOLS/scripts/release_bytecode_policy.py" verify --environment "${variable}"' assert script.count(create) == script.count(install) == script.count(verify) == 1 assert script.index(create) < script.index("uv sync") < script.rindex("uv sync") < script.index(install) < script.index(verify) assert 'cp scripts/*release*.py' in step(job, "Preserve candidate release tools")["run"] if variable == "BASE_TEST_ENV": assert script.index(verify) < script.index("scripts.run_release_partition collect") for name, other in JOBS.items(): if name not in {"release-core-tests", "release-gloo-tests", "test"}: assert "release_bytecode_policy" not in yaml.safe_dump(other) @pytest.mark.parametrize("event_valid", [True, False]) def test_real_base_selection_script_pins_candidate_before_resolving(tmp_path, event_valid): candidate, base = "a" * 40, "b" * 40 fake_git = tmp_path / "git" fake_git.write_text( '#!/bin/sh\n' 'printf "%s\\n" "$*" >> "$GIT_CALLS"\n' 'if [ "$*" = "rev-parse HEAD" ]; then echo "$CANDIDATE_SHA";\n' 'elif [ "$*" = "rev-parse --verify event^{commit}" ]; then\n' ' if [ "$VALID_EVENT" = yes ]; then echo "$EXPECTED_BASE"; else exit 1; fi;\n' 'elif [ "$*" = "rev-parse --verify ${CANDIDATE_SHA}^" ]; then echo "$EXPECTED_BASE";\n' 'else exit 99; fi\n', ) fake_git.chmod(0o755) output, calls = tmp_path / "output", tmp_path / "calls" env = {**os.environ, "PATH": str(tmp_path) + os.pathsep + os.environ["PATH"], "CANDIDATE_SHA": candidate, "EVENT_BASE": "event", "EXPECTED_BASE": base, "GITHUB_REF": "refs/heads/main", "MAIN_REF": "origin/main", "VALID_EVENT": "yes" if event_valid else "no", "GITHUB_OUTPUT": str(output), "GIT_CALLS": str(calls)} subprocess.run(["bash", "-euo", "pipefail", "-c", step("release-select", "Resolve exact release comparison base once")["run"]], env=env, check=True, capture_output=True, text=True) assert output.read_text() == f"candidate_sha={candidate}\ncoverage_base={base}\n" assert calls.read_text().splitlines() == ["rev-parse HEAD", "rev-parse --verify event^{commit}"] + ( [] if event_valid else [f"rev-parse --verify {candidate}^"] ) def _git(repo, *args): return subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True, text=True).stdout.strip() @pytest.fixture def synced_history(tmp_path): """main: c0 - m1 - merge(PR); branch: c0 - f1 - sync(f1, m1).""" repo = tmp_path / "repo" repo.mkdir() _git(repo, "init", "-q", "-b", "main") for key, value in (("user.name", "ci"), ("user.email", "ci@example.invalid"), ("commit.gpgsign", "false")): _git(repo, "config", key, value) def commit(name): (repo / name).write_text(name) _git(repo, "add", name) _git(repo, "commit", "-q", "-m", name) return _git(repo, "rev-parse", "HEAD") shas = {"c0": commit("c0")} _git(repo, "checkout", "-q", "-b", "feature") shas["f1"] = commit("f1") _git(repo, "checkout", "-q", "main") shas["m1"] = commit("m1") _git(repo, "checkout", "-q", "feature") _git(repo, "merge", "-q", "--no-ff", "--no-edit", "main") shas["sync"] = _git(repo, "rev-parse", "HEAD") _git(repo, "checkout", "-q", "main") _git(repo, "merge", "-q", "--no-ff", "--no-edit", "feature") shas["pr_merge"] = _git(repo, "rev-parse", "HEAD") _git(repo, "update-ref", "refs/remotes/origin/main", shas["m1"]) return repo, shas @pytest.mark.parametrize("candidate,ref,expected", [ # #679/#686: a synced branch head compares with its merge base with main, so # main's changes brought in by the sync are not counted as the branch's. ("sync", "refs/heads/feature", "m1"), # Ordinary cases keep the first parent. ("f1", "refs/heads/feature", "c0"), ("pr_merge", "refs/heads/main", "m1"), ("pr_merge", "refs/tags/v1.0.0", "m1"), ]) def test_release_base_uses_the_merge_base_only_for_a_synced_branch_head( synced_history, tmp_path, candidate, ref, expected, ): repo, shas = synced_history _git(repo, "checkout", "-q", "--detach", shas[candidate]) if candidate == "pr_merge": _git(repo, "update-ref", "refs/remotes/origin/main", shas["pr_merge"]) output = tmp_path / "output" env = {**os.environ, "CANDIDATE_SHA": shas[candidate], "EVENT_BASE": "", "GITHUB_REF": ref, "MAIN_REF": "origin/main", "GITHUB_OUTPUT": str(output)} subprocess.run(["bash", "-euo", "pipefail", "-c", step("release-select", "Resolve exact release comparison base once")["run"]], cwd=repo, env=env, check=True, capture_output=True, text=True) assert output.read_text() == f"candidate_sha={shas[candidate]}\ncoverage_base={shas[expected]}\n" def test_incomplete_aggregation_discards_only_publications_retains_raw_diagnostics(tmp_path): results = tmp_path / "test-results" raw = results / "partitions/head/release-core-py3.12-head-0" raw.mkdir(parents=True) (raw / "status.json").write_text("raw diagnostic") expected = results / "expected-head" expected.mkdir() (expected / "collection.json").write_text("raw inventory") publications = [results / name for name in ( "coverage-py3.12.json", "coverage-py3.12.accepted.json", "base-coverage-py3.12.xml", "junit-release-py3.12.xml", "junit-base-py3.12.xml", )] for path in publications: path.write_text("partial publication") cleanup = step("test", "Discard incomplete aggregate publications") assert "steps.head.outcome != 'success'" in cleanup["if"] assert "steps.base.outcome != 'success'" in cleanup["if"] # setup-python provides this PATH entry in Actions; use the test interpreter locally. env = {**os.environ, "PATH": str(Path(sys.executable).parent) + os.pathsep + os.environ["PATH"]} subprocess.run(["bash", "-euo", "pipefail", "-c", cleanup["run"]], cwd=tmp_path, env=env, check=True) assert not any(path.exists() for path in publications) assert (raw / "status.json").read_text() == "raw diagnostic" assert (expected / "collection.json").read_text() == "raw inventory"