|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Run the freshness workflow against local Git history and a mock Pixi.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import pytest |
| 14 | +import yaml |
| 15 | + |
| 16 | +ROOT = Path(__file__).resolve().parents[3] |
| 17 | +WORKFLOW = ROOT / ".github/workflows/ci-pixi-lockfile-freshness-check.yml" |
| 18 | + |
| 19 | + |
| 20 | +def _git(repo, *args): |
| 21 | + return subprocess.run( # noqa: S603 - arguments are passed without a shell. |
| 22 | + ["git", "-C", str(repo), *args], # noqa: S607 |
| 23 | + check=True, |
| 24 | + capture_output=True, |
| 25 | + text=True, |
| 26 | + ).stdout.strip() |
| 27 | + |
| 28 | + |
| 29 | +def _commit(repo): |
| 30 | + _git(repo, "add", ".") |
| 31 | + _git( repo, "-c", "user.name=Test", "-c", "[email protected]", "commit", "-qm", "test") |
| 32 | + return _git(repo, "rev-parse", "HEAD") |
| 33 | + |
| 34 | + |
| 35 | +def _run_check(tmp_path, manifest, changed_manifest, *, stale=True, known_defect=False): |
| 36 | + repo = tmp_path / "repo" |
| 37 | + repo.mkdir() |
| 38 | + _git(repo, "init", "-q") |
| 39 | + _git(repo, "config", "commit.gpgsign", "false") |
| 40 | + tools = repo / "ci/tools" |
| 41 | + tools.mkdir(parents=True) |
| 42 | + for name in ("list_pixi_workspaces.py", "classify_pixi_lockfile_freshness.py"): |
| 43 | + shutil.copyfile(ROOT / "ci/tools" / name, tools / name) |
| 44 | + # The exact defect detector is covered separately; exercise its workflow gate. |
| 45 | + (tools / "check_pixi_samples_source_pruning.py").write_text( |
| 46 | + f"raise SystemExit({0 if known_defect else 1})\n", encoding="utf-8" |
| 47 | + ) |
| 48 | + for workspace in {".", manifest, changed_manifest}: |
| 49 | + directory = repo / workspace |
| 50 | + directory.mkdir(parents=True, exist_ok=True) |
| 51 | + (directory / "pixi.toml").write_text('[workspace]\nchannels = ["conda-forge"]\n', encoding="utf-8") |
| 52 | + content = "stale\n" if stale and workspace == manifest else "fresh\n" |
| 53 | + (directory / "pixi.lock").write_text(content, encoding="utf-8") |
| 54 | + base_sha = _commit(repo) |
| 55 | + with (repo / changed_manifest / "pixi.toml").open("a", encoding="utf-8") as stream: |
| 56 | + stream.write('\n[tasks]\nexample = "echo example"\n') |
| 57 | + _commit(repo) |
| 58 | + |
| 59 | + bin_dir = tmp_path / "bin" |
| 60 | + bin_dir.mkdir() |
| 61 | + pixi = bin_dir / "pixi" |
| 62 | + pixi.write_text( |
| 63 | + "#!/usr/bin/env python3\n" |
| 64 | + "import sys\n" |
| 65 | + "from pathlib import Path\n" |
| 66 | + "lockfile = Path(sys.argv[-1]) / 'pixi.lock'\n" |
| 67 | + "if lockfile.read_text() == 'stale\\n':\n" |
| 68 | + " lockfile.write_text('repaired\\n')\n" |
| 69 | + " sys.exit(1)\n", |
| 70 | + encoding="utf-8", |
| 71 | + ) |
| 72 | + pixi.chmod(0o755) |
| 73 | + summary = tmp_path / "summary.md" |
| 74 | + output = tmp_path / "output.txt" |
| 75 | + workflow = yaml.safe_load(WORKFLOW.read_text(encoding="utf-8")) |
| 76 | + step = next(step for step in workflow["jobs"]["lockfile-fresh"]["steps"] if step.get("id") == "check") |
| 77 | + result = subprocess.run( # noqa: S603 - run trusted workflow code with local fixtures and no network. |
| 78 | + ["bash", "--noprofile", "--norc", "-euo", "pipefail", "-c", step["run"]], # noqa: S607 |
| 79 | + cwd=repo, |
| 80 | + env={ |
| 81 | + **os.environ, |
| 82 | + "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}", |
| 83 | + "BASE_SHA": base_sha, |
| 84 | + "EVENT_NAME": "pull_request", |
| 85 | + "MAINTENANCE_KIND": "workflow", |
| 86 | + "MAINTENANCE_URL": "https://example.com/refresh", |
| 87 | + "PIXI_VERSION": "v0.73.0", |
| 88 | + "WORKSPACE_TIMEOUT": "10s", |
| 89 | + "RUNNER_TEMP": str(tmp_path), |
| 90 | + "GITHUB_STEP_SUMMARY": str(summary), |
| 91 | + "GITHUB_OUTPUT": str(output), |
| 92 | + }, |
| 93 | + capture_output=True, |
| 94 | + text=True, |
| 95 | + timeout=30, |
| 96 | + ) |
| 97 | + return result, summary.read_text(encoding="utf-8") |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("manifest", [".", "nested/workspace", "cuda_core"]) |
| 101 | +@pytest.mark.agent_authored(model="gpt-6") |
| 102 | +def test_manifest_edit_without_lock_update_reports_pr_remediation(tmp_path, manifest): |
| 103 | + result, summary = _run_check(tmp_path, manifest, manifest, known_defect=manifest == "cuda_core") |
| 104 | + |
| 105 | + assert result.returncode == 1, result.stdout + result.stderr |
| 106 | + assert "manifest changed without a lockfile update" in summary |
| 107 | + assert "regenerate and commit the lockfile in this PR" in summary |
| 108 | + assert "Base maintenance:" not in summary |
| 109 | + assert "Do not add this refresh" not in result.stdout |
| 110 | + assert "Known inherited Pixi samples lockfile bug" not in result.stdout |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.agent_authored(model="gpt-6") |
| 114 | +def test_other_workspace_manifest_edit_keeps_base_maintenance_attribution(tmp_path): |
| 115 | + result, summary = _run_check(tmp_path, "nested/workspace", ".") |
| 116 | + |
| 117 | + assert result.returncode == 1, result.stdout + result.stderr |
| 118 | + assert "Base maintenance:" in summary |
| 119 | + assert "PR-induced:" not in summary |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.agent_authored(model="gpt-6") |
| 123 | +def test_fresh_lockfile_allows_task_only_manifest_edit(tmp_path): |
| 124 | + result, summary = _run_check(tmp_path, "nested/workspace", "nested/workspace", stale=False) |
| 125 | + |
| 126 | + assert result.returncode == 0, result.stdout + result.stderr |
| 127 | + assert "| `nested/workspace` | Fresh |" in summary |
0 commit comments