"""Repository contracts for immutable CI dependencies and PR test execution.""" from __future__ import annotations import json import os import re from pathlib import Path import subprocess import sys import pytest import yaml ROOT = Path(__file__).parents[1] WORKFLOW = ROOT / ".github" / "workflows" / "ci.yml" MANIFEST = ROOT / "ci" / "digests.txt" PR_POLICY = ROOT / "ci" / "pr-test-policy.json" ACTION_REF = re.compile( r"^\s*uses:\s*([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)@([0-9a-f]{40})\s+#\s+(\S+)\s*$", ) def _manifest_entries() -> dict[tuple[str, str], tuple[str, str]]: entries: dict[tuple[str, str], tuple[str, str]] = {} for line in MANIFEST.read_text(encoding="utf-8").splitlines(): if not line or line.startswith("#"): continue kind, name, pin, version, _date, _rationale = line.split(maxsplit=5) entries[(kind, name)] = (pin, version) return entries def test_every_external_action_is_sha_pinned_and_manifested(): entries = _manifest_entries() uses_lines = [ line for line in WORKFLOW.read_text(encoding="utf-8").splitlines() if "uses:" in line and "uses: ./" not in line ] assert uses_lines for line in uses_lines: match = ACTION_REF.match(line) assert match is not None, f"external action is not SHA-pinned with a version comment: {line}" name, pin, version = match.groups() assert entries[("action", name)] == (pin, version) def test_tested_source_snapshot_is_created_once_attested_and_reused(): workflow = WORKFLOW.read_text(encoding="utf-8") package = workflow.split(" package:\n", maxsplit=1)[1].split( " lint:\n", maxsplit=1, )[0] supply_chain = workflow.split(" supply-chain:\n", maxsplit=1)[1] assert "artifact-metadata: write" in package assert "attestations: write" in package assert "id-token: write" in package assert "package-evidence/SHA256SUMS" in package assert "package-evidence/obliteratus.cdx.json" in package assert 'git archive \\' in package assert '--format=zip' in package assert 'subject-path: release/*.zip' in package assert "python -m build" not in package assert "*.whl" not in package assert "*.tar.gz" not in package assert package.count("uses: actions/attest@") == 2 assert "subject-checksums: package-evidence/SHA256SUMS" in package assert "sbom-path: package-evidence/obliteratus.cdx.json" in package assert "needs: package" in supply_chain assert "uses: actions/download-artifact@" in supply_chain assert "sha256sum -c package-evidence/SHA256SUMS" in supply_chain assert "tested-source-snapshot-py3.12" in supply_chain assert "find release -maxdepth 1 -type f -name '*.zip'" in supply_chain assert "-m build" not in supply_chain # Nested third-party payloads are verified before the outer source SBOM is # attested, and retained evidence must already contain the bound digest. assert package.index("python scripts/verify_gradio_release.py") < package.index( "python scripts/check_supply_chain_policy.py sbom" ) < package.index("uses: actions/attest@") assert "--output package-evidence/obliteratus.cdx.dependency-bound.json" in package assert "--input package-evidence/obliteratus.cdx.dependency-bound.json" in package assert 'python "$GITHUB_WORKSPACE/scripts/verify_gradio_release.py"' in supply_chain assert "--require-bound" in supply_chain assert '--input "$EVIDENCE/verified-obliteratus.cdx.json"' in supply_chain def test_actionlint_version_and_checksum_match_manifest(): workflow = WORKFLOW.read_text(encoding="utf-8") pin, version = _manifest_entries()[("tool", "rhysd/actionlint")] assert f'ACTIONLINT_VERSION: "{version.removeprefix("v")}"' in workflow assert f'ACTIONLINT_SHA256: "{pin.removeprefix("sha256:")}"' in workflow def test_ci_ruff_f_gate_covers_the_entire_scripts_tree(): workflow = WORKFLOW.read_text(encoding="utf-8") ruff_step = workflow.split(" - name: Enforce Ruff F gate\n", maxsplit=1)[1].split( " - name:", maxsplit=1, )[0] assert "python -m ruff check --select F app.py obliteratus tests scripts" in ruff_step def test_uv_and_gitleaks_pins_match_manifest(): workflow = WORKFLOW.read_text(encoding="utf-8") entries = _manifest_entries() uv_pin, uv_version = entries[("tool", "astral-sh/uv")] gitleaks_pin, gitleaks_version = entries[("tool", "gitleaks/gitleaks")] assert uv_pin == f"pypi:{uv_version.removeprefix('v')}" assert f'UV_VERSION: "{uv_version.removeprefix("v")}"' in workflow assert f'GITLEAKS_VERSION: "{gitleaks_version.removeprefix("v")}"' in workflow assert f'GITLEAKS_SHA256: "{gitleaks_pin.removeprefix("sha256:")}"' in workflow def test_ci_requires_the_committed_lock_and_strict_policy_gate(): workflow = WORKFLOW.read_text(encoding="utf-8") assert "uv lock --check" in workflow assert "uv sync --locked" in workflow assert "scripts/check_supply_chain_policy.py audit" in workflow assert "scripts/check_supply_chain_policy.py secrets" in workflow assert "scripts/check_supply_chain_policy.py licenses" in workflow supply_chain_job = workflow.split(" supply-chain:\n", maxsplit=1)[1] assert "|| true" not in supply_chain_job def test_ci_enforces_exact_base_module_regression_and_risk_mapping(): workflow = WORKFLOW.read_text(encoding="utf-8") assert "Generate exact-base coverage for module regression comparison" in workflow assert 'git worktree add --detach "$BASE_WORKTREE" "$COVERAGE_BASE"' in workflow assert "--touched-module-no-regression" in workflow assert "--base-report test-results/base-coverage-py3.12.json" in workflow assert "scripts/check_test_risk_map.py" in workflow def test_ci_repository_coverage_floors_match_quality_policy(): workflow = WORKFLOW.read_text(encoding="utf-8") policy = json.loads( (ROOT / "ci" / "test-quality-policy.json").read_text(encoding="utf-8"), ) assert f"--min-line {policy['minimums']['repository_statement']:g}" in workflow assert f"--min-branch {policy['minimums']['repository_branch']:g}" in workflow assert f"--min-changed {policy['minimums']['changed_line']:g}" in workflow def test_pull_request_gate_is_fast_risk_mapped_and_uses_shared_floor(): workflow = WORKFLOW.read_text(encoding="utf-8") policy = json.loads(PR_POLICY.read_text(encoding="utf-8")) quality = json.loads( (ROOT / "ci" / "test-quality-policy.json").read_text(encoding="utf-8"), ) pr_core = workflow.split(" pr-core:\n", maxsplit=1)[1].split( " test:\n", maxsplit=1, )[0] assert "name: Pull request core" in pr_core assert "timeout-minutes: 10" in pr_core assert "github.event_name == 'pull_request'" in pr_core assert 'python-version: "3.12"' in pr_core assert "scripts/check_pr_partitions.py" in pr_core assert "--min-line 0" in pr_core assert "--min-branch 0" in pr_core assert f"--min-changed {policy['changed_line_coverage_floor']:g}" in pr_core assert policy["changed_line_coverage_floor"] == 50.0 assert quality["minimums"]["changed_line"] == 50.0 for test in policy["always_tests"] + policy["infrastructure_tests"]: assert (ROOT / test).is_file(), test assert "tests/conditional/" in policy["excluded_test_prefixes"] def _jobs(): return yaml.safe_load(WORKFLOW.read_text())["jobs"] def _step(job, name): return next(step for step in _jobs()[job]["steps"] if step.get("name") == name) def test_required_pr_check_aggregates_successful_jobs_even_after_upstream_failure(): jobs = _jobs() core = jobs["pr-core"] assert core["name"] == "Pull request core" assert core["needs"] == ["pr-select", "pr-core-tests", "pr-gloo-tests"] event = "github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')" assert jobs["pr-select"]["if"] == event assert core["if"] == f"always() && ({event})" from scripts.check_pr_partitions import NEEDS_ENV for job, variable in NEEDS_ENV.items(): assert core["env"][variable] == "${{ needs." + job + ".result }}" guard = _step("pr-core", "Require successful upstream jobs") assert guard["run"] == "python scripts/check_pr_partitions.py --needs-only" names = [step.get("name") for step in core["steps"]] assert names.index(guard["name"]) < names.index("Install locked package and test tools") def test_selector_runs_once_and_single_constant_drives_sharder_and_matrix(): jobs = _jobs() select = jobs["pr-select"] assert select["outputs"] == { "coverage_base": "${{ steps.base.outputs.coverage_base }}", "gloo_shards": "${{ steps.selection.outputs.gloo_shards }}", "core_shards": "${{ steps.selection.outputs.core_shards }}", } run = _step("pr-select", "Select and shard PR tests")["run"] # One constant per kind drives both its output and sharder invocation. counts = re.findall(r"GLOO_SHARD_COUNT=(\d+)", run) assert len(counts) == 1 and int(counts[0]) >= 1 assert 'list(range(int(sys.argv[1])))' in run assert run.count('"$GLOO_SHARD_COUNT"') == 2 assert 'scripts/shard_pr_tests.py --shards "$GLOO_SHARD_COUNT"' in run assert re.findall(r"CORE_SHARD_COUNT=(\d+)", run) == ["6"] assert run.count('"$CORE_SHARD_COUNT"') == 2 assert 'echo "core_shards=$core_shards" >> "$GITHUB_OUTPUT"' in run assert 'scripts/shard_pr_tests.py --kind core --shards "$CORE_SHARD_COUNT"' in run assert '--selected test-results/selected-tests.txt' in run assert run.count("scripts/shard_pr_tests.py --kind core") == 1 assert 'if [ "${#selected_tests[@]}" -eq 0 ]' in run assert WORKFLOW.read_text().count("scripts/select_pr_tests.py") == 1 assert jobs["pr-gloo-tests"]["strategy"] == { "fail-fast": False, "matrix": {"shard": "${{ fromJSON(needs.pr-select.outputs.gloo_shards) }}"}, } assert jobs["pr-gloo-tests"]["name"] == "PR gloo tests (shard ${{ matrix.shard }})" assert jobs["pr-core-tests"]["strategy"] == { "fail-fast": False, "matrix": {"shard": "${{ fromJSON(needs.pr-select.outputs.core_shards) }}"}, } assert jobs["pr-core-tests"]["name"] == "PR core tests (shard ${{ matrix.shard }})" upload = _step("pr-select", "Upload PR test selection")["with"] assert upload["name"] == "pr-test-selection" assert upload["path"] == "test-results/selected-*.txt" assert upload["overwrite"] is True base = _step("pr-select", "Resolve exact comparison base")["run"] assert 'coverage_base="$EVENT_BASE"' in base assert 'coverage_base="$(git rev-parse HEAD^)"' in base assert 'echo "coverage_base=$coverage_base" >> "$GITHUB_OUTPUT"' in base policy = _step("pr-select", "Validate lock and test policy")["run"] for check in ["uv lock --check", "check_quality_policy.py", "check_conditional_policy.py", "check_test_risk_map.py"]: assert check in policy def test_partition_jobs_consume_selection_and_upload_before_enforcing_status(): jobs = _jobs() assert jobs["pr-core-tests"]["outputs"]["exit_code"] == "${{ steps.pytest.outputs.exit_code }}" for job, partition, marker in [ ("pr-core-tests", "core-${{ matrix.shard }}", "not gloo"), ("pr-gloo-tests", "gloo-${{ matrix.shard }}", "gloo"), ]: assert jobs[job]["needs"] == "pr-select" assert jobs[job]["env"]["PARTITION"] == partition download = _step(job, "Download PR test selection") assert download["with"] == {"name": "pr-test-selection", "path": "test-results/"} run = _step(job, "Run marker partition")["run"] assert '-m "$marker_expression and ' + marker + '"' in run assert 'COVERAGE_FILE="test-results/coverage-$PARTITION.data"' in run for option in ["--cov=app", "--cov-branch", "--cov-fail-under=0", "--cov-config=pyproject.toml", "--cov-report=", '-o "addopts=$pytest_addopts"']: assert option in run assert 'pytest_addopts="--strict-config --strict-markers --cov=obliteratus"' in run assert '--basetemp="$RUNNER_TEMP/pytest-pr-$PARTITION"' in run assert '|| exit_code=$?' in run assert '"candidate_sha": os.environ["CANDIDATE_SHA"]' in run assert '"schema_version": 1' in run assert "_pid" not in run and "2>&1 &" not in run upload = _step(job, "Upload partition evidence") assert upload["if"] == "always()" assert upload["with"]["name"] == f"pr-tests-{partition}" assert upload["with"]["overwrite"] is True assert upload["with"]["include-hidden-files"] is False assert "test-results/coverage-*.data" in upload["with"]["path"] names = [step.get("name") for step in jobs[job]["steps"]] assert names.index("Upload partition evidence") < names.index("Enforce partition exit code") assert jobs["pr-core-tests"]["env"]["SELECTED_LIST"] == ( "test-results/selected-core-${{ matrix.shard }}.txt" ) assert jobs["pr-gloo-tests"]["env"]["SELECTED_LIST"] == "test-results/selected-gloo-${{ matrix.shard }}.txt" def test_all_pr_jobs_use_exact_candidate_locked_env_and_ten_minute_timeout(): for job in ["pr-select", "pr-core-tests", "pr-gloo-tests", "pr-core"]: assert _jobs()[job]["timeout-minutes"] == 10 assert _step(job, "Check out exact candidate")["with"] == { "fetch-depth": 0, "ref": "${{ env.CANDIDATE_SHA }}", } assert _step(job, "Set up Python")["with"]["python-version"] == "3.12" assert "uv sync --locked --no-default-groups --extra dev --extra recovery --extra mcp --no-editable" in _step( job, "Install locked package and test tools", )["run"] def test_release_sdk_checks_enable_target_declared_mcp_on_all_supported_python_versions(): installation = _step("test", "Install locked package and test tools")["run"] assert "scripts.release_test_environment" in installation assert '--project "$GITHUB_WORKSPACE/pyproject.toml"' in installation assert '"${extra_args[@]}" --no-editable' in installation assert "matrix.python-version" not in installation from scripts.release_test_environment import release_extras assert release_extras(ROOT / "pyproject.toml") == ["dev", "recovery", "mcp"] assert "--extra mcp" in _step("quality-depth", "Install locked quality tooling")["run"] def test_aggregate_validates_then_combines_in_fresh_directory_before_unchanged_floor(): core = _jobs()["pr-core"] download = _step("pr-core", "Download partition evidence") assert download["with"] == { "pattern": "pr-tests-*", "path": "test-results/partitions/", "merge-multiple": False, } combine = _step("pr-core", "Combine accepted partition coverage")["run"] assert 'mapfile -t coverage_files < test-results/accepted-coverage.txt' in combine assert 'test "${#coverage_files[@]}" -gt 0' in combine assert combine.index('rm -rf test-results/coverage-combined') < combine.index( 'mkdir test-results/coverage-combined', ) < combine.index('export COVERAGE_FILE=test-results/coverage-combined/.coverage') assert '-m coverage combine --keep --rcfile=pyproject.toml "${coverage_files[@]}"' in combine assert combine.index('-m coverage combine') < combine.index('-m coverage xml') < combine.index('-m coverage json') # A partial combine (one input errored) must fail before any report is written. assert 'grep -qiE "errored|couldn\'t|warning" "$combine_log"' in combine assert combine.index('"$combine_log"; then') < combine.index('-m coverage xml') names = [step.get("name") for step in core["steps"]] assert names.index("Validate PR partition evidence") < names.index("Combine accepted partition coverage") < names.index("Enforce pull-request changed-line floor") assert core["env"]["COVERAGE_BASE"] == "${{ needs.pr-select.outputs.coverage_base }}" assert core["env"]["GLOO_SHARDS"] == "${{ needs.pr-select.outputs.gloo_shards }}" assert core["env"]["CORE_SHARDS"] == "${{ needs.pr-select.outputs.core_shards }}" assert _step("pr-core", "Enforce pull-request changed-line floor")["run"] == ( '\n'.join([ '"$TEST_ENV/bin/python" scripts/check_coverage_thresholds.py \\', ' test-results/coverage-pr-core.json \\', ' --min-line 0 \\', ' --min-branch 0 \\', ' --min-changed 50 \\', ' --base-ref "$COVERAGE_BASE"', '', ]) ) evidence = _step("pr-core", "Upload pull-request evidence") assert evidence["if"] == "always()" assert evidence["with"]["name"] == "pr-core-evidence-py3.12" assert evidence["with"]["path"] == "test-results/" assert evidence["with"]["include-hidden-files"] is False def test_gloo_durations_cover_exactly_the_marked_test_files(): from scripts.shard_pr_tests import has_gloo_marker marked = { path.relative_to(ROOT).as_posix() for path in (ROOT / "tests").rglob("test_*.py") if has_gloo_marker(path.read_text()) } assert set(json.loads((ROOT / "ci/gloo-test-durations.json").read_text())["files"]) == marked def test_core_durations_load_and_reference_existing_test_files(): from scripts.shard_pr_tests import load_durations default, durations = load_durations(ROOT / "ci/core-test-durations.json") assert default > 0 assert durations for name in durations: path = ROOT / name assert name.startswith("tests/") and path.is_file() assert path.resolve().is_relative_to((ROOT / "tests").resolve()) def test_gloo_marker_is_registered_and_marks_real_cpu_worker_files(): pyproject = (ROOT / "pyproject.toml").read_text(encoding="utf-8") assert '"gloo: multi-process torch.distributed Gloo tests; run concurrently in pr-core"' in pyproject marked_files = { path.name for path in (ROOT / "tests").glob("test_*.py") if re.search(r"^pytestmark = .*pytest\.mark\.gloo", path.read_text(), re.MULTILINE) } assert marked_files >= { "test_distributed_gloo.py", "test_distributed_model_gloo.py", "test_distributed_probe_gloo.py", } assert not marked_files & { "test_distributed_launcher.py", "test_distributed_model_session.py", "test_distributed_contracts.py", "test_distributed_validation_edges.py", "test_checkpoint_inspection.py", } assert 'pytestmark = [pytest.mark.integration, pytest.mark.gloo]' in ( ROOT / "tests" / "test_distributed_gloo.py" ).read_text() def test_arm64_preflight_proves_portability_without_claiming_jetson_cuda(): workflow = WORKFLOW.read_text(encoding="utf-8") arm = workflow.split(" arm64-preflight:\n", maxsplit=1)[1].split( " pr-select:\n", maxsplit=1, )[0] assert "runs-on: ubuntu-24.04-arm" in arm assert "github.event_name == 'pull_request'" in arm assert "github.event_name == 'push' && github.ref == 'refs/heads/main'" in arm assert "-m build --wheel" in arm assert "import obliteratus" in arm assert "-m obliteratus --help" in arm assert "tests/test_device_boundaries.py" in arm assert "tests/test_jetson_support_tooling.py" in arm assert "jetson-runtime" not in arm def test_release_depth_jobs_only_run_for_tags_or_manual_validation(): workflow = WORKFLOW.read_text(encoding="utf-8") release_condition = ( "if: github.event_name == 'workflow_dispatch' || " "startsWith(github.ref, 'refs/tags/v')" ) assert ' - "v*"' in workflow assert " workflow_dispatch:" in workflow for start, end in ( (" package:\n", " lint:\n"), (" checkpoint-windows:\n", " quality-depth:\n"), (" quality-depth:\n", " supply-chain:\n"), ): job = workflow.split(start, maxsplit=1)[1].split(end, maxsplit=1)[0] assert release_condition in job supply_chain = workflow.split(" supply-chain:\n", maxsplit=1)[1] assert release_condition in supply_chain assert _jobs()["test"]["if"] == "always() && (" + release_condition.removeprefix("if: ") + ")" for name in ("release-select", "release-core-tests", "release-gloo-tests"): assert _jobs()[name]["if"] == release_condition.removeprefix("if: ") def test_ci_enforces_owned_duration_budgets_and_bounded_release_test_lanes(): workflow = WORKFLOW.read_text(encoding="utf-8") test_job = workflow.split(" test:\n", maxsplit=1)[1].split( " quality-depth:\n", maxsplit=1, )[0] jobs = _jobs() # The same limits now bound every core target and its report separately. assert jobs["test"]["timeout-minutes"] == 30 assert jobs["test"]["needs"] == ["release-select", "release-gloo-tests", "release-core-tests"] assert jobs["release-gloo-tests"]["timeout-minutes"] == 10 assert jobs["release-core-tests"]["timeout-minutes"] == 30 assert "scripts.run_release_partition collect" in test_job assert test_job.count("scripts.combine_release_evidence") == 2 assert '--checkout-origin "$GITHUB_WORKSPACE/obliteratus"' in test_job assert "grep -c" not in test_job assert '--evidence "test-results/test-trend-py${{ matrix.python-version }}.json"' in workflow assert "--evidence quality-evidence/quality-trend-py3.12.json" in workflow def test_ci_retains_normalized_test_and_quality_trends_for_ninety_days(): workflow = WORKFLOW.read_text(encoding="utf-8") assert "test-trend-py${{ matrix.python-version }}.json" in workflow assert "quality-trend-py3.12.json" in workflow assert workflow.count("retention-days: 90") >= 2 def test_ci_evidence_names_the_pull_request_head_not_the_synthetic_merge_commit(): workflow = WORKFLOW.read_text(encoding="utf-8") assert "CANDIDATE_SHA: ${{ github.event.pull_request.head.sha || github.sha }}" in workflow assert workflow.count('--head-sha "$CANDIDATE_SHA"') == 2 assert '--head-sha "$GITHUB_SHA"' not in workflow def test_build_backend_requirements_use_lower_bounds_not_exact_pins(): pyproject = (ROOT / "pyproject.toml").read_text(encoding="utf-8") match = re.search( r"(?ms)^\[build-system\]\n(?:#[^\n]*\n)*requires = \[([^\]]+)\]", pyproject, ) assert match is not None requires = re.findall(r'"([^"]+)"', match.group(1)) assert requires for requirement in requires: spec = requirement.split(";", 1)[0] name = re.split(r"[<>=!~]", spec, maxsplit=1)[0].strip() assert name != "wheel", requirement assert "==" not in spec, requirement assert ">=" in spec, requirement lower = re.findall(r">=([^,]+)", spec) upper = re.findall(r"<=([^,]+)", spec) assert not ( lower and upper and lower[0] == upper[0] ), f"collapsed exact pin written as a range: {requirement}" def test_ci_runs_checkpoint_portability_contracts_on_windows(): workflow = WORKFLOW.read_text(encoding="utf-8") checkpoint_job = workflow.split(" checkpoint-windows:\n", maxsplit=1)[1].split( " quality-depth:\n", maxsplit=1, )[0] assert "runs-on: windows-latest" in checkpoint_job assert 'python-version: "3.12"' in checkpoint_job assert "uv sync --locked --no-default-groups --extra dev --extra recovery --no-editable" in checkpoint_job assert "tests/test_checkpoint_atomicity.py" in checkpoint_job assert "tests/test_persistence_contracts.py" in checkpoint_job assert "tests/test_persistence_pipeline.py" in checkpoint_job assert "--no-cov" in checkpoint_job assert "checkpoint-evidence-windows-py3.12" in checkpoint_job assert "if-no-files-found: error" in checkpoint_job # Fast stand-ins execute the actual job Bash under -e, without Gloo sockets. FAKE_PYTHON = r''' import json import os from pathlib import Path import sys args = sys.argv[1:] results = Path("test-results") if args[:2] == ["-m", "pytest"]: partition = os.environ["PARTITION"] (results / "invocation.json").write_text(json.dumps({ "args": args, "coverage_file": os.environ["COVERAGE_FILE"], })) Path(next(arg.split("=", 1)[1] for arg in args if arg.startswith("--junitxml="))).write_text("") Path(os.environ["COVERAGE_FILE"]).write_text(partition) print(partition + " pytest output") sys.exit(int(os.environ["FAKE_STATUS"])) elif args[:2] == ["-m", "coverage"]: assert os.environ["COVERAGE_FILE"] == "test-results/coverage-combined/.coverage" combined = Path(os.environ["COVERAGE_FILE"]) assert sorted(path.name for path in combined.parent.iterdir()) in ([], [".coverage"]) assert "--rcfile=pyproject.toml" in args with (results / "coverage-calls.jsonl").open("a") as stream: stream.write(json.dumps(args) + "\n") if args[2] == "combine": assert "--keep" in args inputs = args[5:] assert inputs and all(Path(path).stat().st_size for path in inputs) combined.write_text("combined") else: assert combined.is_file() Path(args[args.index("-o") + 1]).write_text("report") else: # Run the status writer and evidence checker with real Python. if args and args[0] == "scripts/check_pr_partitions.py": args[0] = str(Path(os.environ["REPOSITORY_ROOT"]) / args[0]) os.execv(sys.executable, [sys.executable, *args]) ''' def _fake_env(tmp_path, **overrides): from scripts.check_pr_partitions import NEEDS_ENV executable = tmp_path / "venv/bin/python" executable.parent.mkdir(parents=True) executable.write_text(f"#!{sys.executable}\n" + FAKE_PYTHON) executable.chmod(0o755) return { **os.environ, **dict.fromkeys(NEEDS_ENV.values(), "success"), "TEST_ENV": str(executable.parents[1]), "REPOSITORY_ROOT": str(ROOT), "CANDIDATE_SHA": "a" * 40, "GLOO_SHARDS": "[0]", "CORE_SHARDS": "[0]", "RUNNER_TEMP": str(tmp_path / "runner-temp"), **overrides, } def _bash(tmp_path, run, env): return subprocess.run( ["bash", "-e", "-o", "pipefail", "-c", run], cwd=tmp_path, env=env, capture_output=True, text=True, timeout=20, ) @pytest.mark.parametrize("job,partition", [("pr-core-tests", "core-0"), ("pr-gloo-tests", "gloo-0")]) @pytest.mark.parametrize("code", [0, 5, 1, 2, 3]) def test_partition_bash_captures_pytest_status_before_enforcement(tmp_path, job, partition, code): results = tmp_path / "test-results" results.mkdir() selected = results / "selected.txt" selected.write_text("tests/test_gloo.py\ntests/test_core.py\n") (results / f"coverage-{partition}.data").write_text("stale") output = tmp_path / "github-output" env = _fake_env(tmp_path, PARTITION=partition, SELECTED_LIST=str(selected), FAKE_STATUS=str(code), GITHUB_OUTPUT=str(output)) process = _bash(tmp_path, _step(job, "Run marker partition")["run"], env) assert process.returncode == 0, process.stdout + process.stderr assert (results / f"status-{partition}.json").read_text() assert json.loads((results / f"status-{partition}.json").read_text()) == { "schema_version": 1, "partition": partition, "exit_code": code, "candidate_sha": "a" * 40, } assert output.read_text() == f"exit_code={code}\n" invocation = json.loads((results / "invocation.json").read_text()) assert invocation["coverage_file"] == f"test-results/coverage-{partition}.data" assert invocation["args"][2:4] == selected.read_text().splitlines() assert f"--basetemp={tmp_path / 'runner-temp' / ('pytest-pr-' + partition)}" in invocation["args"] enforce = _bash(tmp_path, _step(job, "Enforce partition exit code")["run"], {**env, "EXIT_CODE": str(code)}) assert enforce.returncode == (0 if code in {0, 5} else 1) @pytest.mark.parametrize("job,partition", [("pr-core-tests", "core-0"), ("pr-gloo-tests", "gloo-0")]) def test_empty_shard_records_five_without_invoking_pytest(tmp_path, job, partition): results = tmp_path / "test-results" results.mkdir() selected = results / "selected.txt" selected.write_bytes(b"") (results / f"coverage-{partition}.data").write_text("stale") env = _fake_env(tmp_path, PARTITION=partition, SELECTED_LIST=str(selected), FAKE_STATUS="1", GITHUB_OUTPUT=str(tmp_path / "github-output")) process = _bash(tmp_path, _step(job, "Run marker partition")["run"], env) assert process.returncode == 0, process.stdout + process.stderr assert not (results / "invocation.json").exists() assert not (results / f"coverage-{partition}.data").exists() assert json.loads((results / f"status-{partition}.json").read_text())["exit_code"] == 5 assert (results / f"junit-{partition}.xml").read_text().strip() == "" assert "pytest not invoked (exit code 5)" in (results / f"pytest-{partition}.log").read_text() assert (tmp_path / "github-output").read_text() == "exit_code=5\n" @pytest.mark.parametrize("empty", ["core-0", "gloo-0"]) @pytest.mark.parametrize("missing", [False, True]) def test_aggregate_bash_rejects_missing_passing_data_and_excludes_empty_data(tmp_path, empty, missing): from tests.test_check_pr_partitions import write_partition results = tmp_path / "test-results" artifacts = results / "partitions" passing = "gloo-0" if empty == "core-0" else "core-0" write_partition(artifacts, empty, 5) write_partition(artifacts, passing) if missing: (artifacts / f"pr-tests-{passing}/coverage-{passing}.data").unlink() combined = results / "coverage-combined" combined.mkdir() (combined / ".coverage.stale").write_text("stale empty-partition data") env = _fake_env(tmp_path) run = _step("pr-core", "Validate PR partition evidence")["run"] + "\n" + _step( "pr-core", "Combine accepted partition coverage", )["run"] process = _bash(tmp_path, run, env) assert process.returncode == (1 if missing else 0), process.stdout + process.stderr if missing: assert "passed without non-empty coverage" in process.stdout assert not (results / "coverage-calls.jsonl").exists() assert not (results / "coverage-pr-core.json").exists() else: calls = [json.loads(line) for line in (results / "coverage-calls.jsonl").read_text().splitlines()] assert [call[2] for call in calls] == ["combine", "xml", "json"] assert calls[0][5:] == [f"test-results/partitions/pr-tests-{passing}/coverage-{passing}.data"] assert sorted(path.name for path in combined.iterdir()) == [".coverage"] assert (artifacts / f"pr-tests-{empty}/coverage-{empty}.data").is_file() def test_hosted_all_extras_syncs_build_qwen_hybrid_without_cuda(): # causal-conv1d (qwen-hybrid) compiles CUDA kernels by default and hosted # runners have no toolkit. Excluding the extra instead would silently drop # its packages from the release license inventory. steps = [ step for path in (ROOT / ".github" / "workflows").glob("*.yml") for job in yaml.safe_load(path.read_text())["jobs"].values() for step in job.get("steps", []) if "uv sync" in step.get("run", "") and "--all-extras" in step["run"] ] assert len(steps) >= 2 for step in steps: assert "--no-extra" not in step["run"], step["name"] assert step.get("env", {}).get("CAUSAL_CONV1D_SKIP_CUDA_BUILD") == "TRUE", ( step["name"] ) def test_hosted_model_gate_file_has_no_operator_only_skips(): # The gate runner rejects any skip, so an opt-in test in this file fails # every hosted run of the gate. gate = ROOT / "tests" / "conditional" / "test_model_download_runtime.py" source = gate.read_text() assert "pytest.skip(" not in source assert "pytest.mark.gpu" not in source