|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import stat |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +GIT = shutil.which("git") |
| 10 | +LOOKUP_RUN_ID = Path(__file__).resolve().parents[1] / "lookup-run-id" |
| 11 | + |
| 12 | + |
| 13 | +def _run(cmd: list[str], cwd: Path) -> str: |
| 14 | + return subprocess.check_output(cmd, cwd=cwd, text=True).strip() # noqa: S603 |
| 15 | + |
| 16 | + |
| 17 | +def _run_checked(cmd: list[str], cwd: Path) -> None: |
| 18 | + subprocess.run(cmd, cwd=cwd, check=True) # noqa: S603 |
| 19 | + |
| 20 | + |
| 21 | +def _init_repo(tmp_path: Path) -> tuple[Path, str]: |
| 22 | + assert GIT is not None |
| 23 | + repo = tmp_path / "repo" |
| 24 | + repo.mkdir() |
| 25 | + _run_checked([GIT, "init"], cwd=repo) |
| 26 | + _run_checked([GIT, "config", "user.name", "Test User"], cwd=repo) |
| 27 | + _run_checked([GIT, "config", "user.email", "test@example.com"], cwd=repo) |
| 28 | + (repo / "README.md").write_text("hello\n", encoding="utf-8") |
| 29 | + _run_checked([GIT, "add", "README.md"], cwd=repo) |
| 30 | + _run_checked([GIT, "commit", "-m", "init"], cwd=repo) |
| 31 | + commit_sha = _run([GIT, "rev-parse", "HEAD"], cwd=repo) |
| 32 | + _run_checked([GIT, "tag", "-a", "cuda-core-v0.6.0", "-m", "release"], cwd=repo) |
| 33 | + return repo, commit_sha |
| 34 | + |
| 35 | + |
| 36 | +def _write_fake_gh(tmp_path: Path, commit_sha: str, tag_name: str) -> Path: |
| 37 | + fakebin = tmp_path / "fakebin" |
| 38 | + fakebin.mkdir() |
| 39 | + gh = fakebin / "gh" |
| 40 | + gh.write_text( |
| 41 | + f"""#!/usr/bin/env bash |
| 42 | +set -euo pipefail |
| 43 | +
|
| 44 | +if [[ "${{1:-}}" == "run" && "${{2:-}}" == "list" ]]; then |
| 45 | + shift 2 |
| 46 | + commit="" |
| 47 | + while [[ $# -gt 0 ]]; do |
| 48 | + case "$1" in |
| 49 | + --commit) |
| 50 | + commit="$2" |
| 51 | + shift 2 |
| 52 | + ;; |
| 53 | + --repo|--workflow|--status|--json|--limit|-R|-w|-s|-L|-b) |
| 54 | + shift 2 |
| 55 | + ;; |
| 56 | + *) |
| 57 | + shift |
| 58 | + ;; |
| 59 | + esac |
| 60 | + done |
| 61 | +
|
| 62 | + if [[ "$commit" == "{commit_sha}" ]]; then |
| 63 | + cat <<EOF |
| 64 | +[{{"databaseId":123,"workflowName":"CI","status":"completed","conclusion":"success","headSha":"{commit_sha}","headBranch":"{tag_name}","event":"push","createdAt":"2026-01-01T00:00:00Z","url":"https://example.test/runs/123"}}] |
| 65 | +EOF |
| 66 | + else |
| 67 | + echo '[]' |
| 68 | + fi |
| 69 | + exit 0 |
| 70 | +fi |
| 71 | +
|
| 72 | +if [[ "${{1:-}}" == "run" && "${{2:-}}" == "view" ]]; then |
| 73 | + echo '{{"url":"https://example.test/runs/123"}}' |
| 74 | + exit 0 |
| 75 | +fi |
| 76 | +
|
| 77 | +echo "unexpected gh invocation: $*" >&2 |
| 78 | +exit 1 |
| 79 | +""", |
| 80 | + encoding="utf-8", |
| 81 | + ) |
| 82 | + gh.chmod(gh.stat().st_mode | stat.S_IXUSR) |
| 83 | + return fakebin |
| 84 | + |
| 85 | + |
| 86 | +def test_lookup_run_id_should_peel_annotated_tag_to_commit_if_tag_mode(tmp_path): |
| 87 | + tag_name = "cuda-core-v0.6.0" |
| 88 | + repo, commit_sha = _init_repo(tmp_path) |
| 89 | + fakebin = _write_fake_gh(tmp_path, commit_sha, tag_name) |
| 90 | + env = os.environ.copy() |
| 91 | + env["GH_TOKEN"] = "test-token" # noqa: S105 |
| 92 | + env["PATH"] = f"{fakebin}{os.pathsep}{env['PATH']}" |
| 93 | + |
| 94 | + result = subprocess.run( # noqa: S603 |
| 95 | + [str(LOOKUP_RUN_ID), "--tag", tag_name, "NVIDIA/cuda-python"], |
| 96 | + cwd=repo, |
| 97 | + env=env, |
| 98 | + text=True, |
| 99 | + capture_output=True, |
| 100 | + check=False, |
| 101 | + ) |
| 102 | + |
| 103 | + assert result.returncode == 0, result.stderr |
| 104 | + assert result.stdout == "123\n" |
| 105 | + assert f"Resolved tag '{tag_name}' to commit: {commit_sha}" in result.stderr |
0 commit comments