|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +VERIFY_RELEASE_REF = Path(__file__).parents[1] / ".github" / "verify_release_ref.py" |
| 10 | + |
| 11 | + |
| 12 | +def _git(repository: Path, *arguments: str) -> str: |
| 13 | + completed = subprocess.run( |
| 14 | + ["git", *arguments], |
| 15 | + cwd=repository, |
| 16 | + check=True, |
| 17 | + stdout=subprocess.PIPE, |
| 18 | + text=True, |
| 19 | + ) |
| 20 | + return completed.stdout.strip() |
| 21 | + |
| 22 | + |
| 23 | +def _repository(tmp_path: Path) -> Path: |
| 24 | + repository = tmp_path / "repository" |
| 25 | + repository.mkdir() |
| 26 | + _git(repository, "init", "--quiet") |
| 27 | + _git(repository, "config", "user.name", "Release Test") |
| 28 | + _git(repository, "config", "user.email", "release@example.invalid") |
| 29 | + (repository / "pyproject.toml").write_text( |
| 30 | + '[project]\nname = "openstatspec"\nversion = "0.2.0"\n', |
| 31 | + encoding="utf-8", |
| 32 | + ) |
| 33 | + _git(repository, "add", "pyproject.toml") |
| 34 | + _git(repository, "commit", "--quiet", "-m", "fixture") |
| 35 | + return repository |
| 36 | + |
| 37 | + |
| 38 | +def _verify(repository: Path, release_tag: str) -> subprocess.CompletedProcess[str]: |
| 39 | + environment = os.environ.copy() |
| 40 | + environment["RELEASE_TAG"] = release_tag |
| 41 | + return subprocess.run( |
| 42 | + [sys.executable, str(VERIFY_RELEASE_REF)], |
| 43 | + cwd=repository, |
| 44 | + env=environment, |
| 45 | + check=False, |
| 46 | + stdout=subprocess.PIPE, |
| 47 | + stderr=subprocess.PIPE, |
| 48 | + text=True, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_release_ref_accepts_annotated_version_tag_at_head(tmp_path: Path) -> None: |
| 53 | + repository = _repository(tmp_path) |
| 54 | + _git(repository, "tag", "-a", "v0.2.0", "-m", "release") |
| 55 | + |
| 56 | + assert _verify(repository, "v0.2.0").returncode == 0 |
| 57 | + |
| 58 | + |
| 59 | +def test_release_ref_rejects_branch_with_version_name(tmp_path: Path) -> None: |
| 60 | + repository = _repository(tmp_path) |
| 61 | + _git(repository, "branch", "v0.2.0") |
| 62 | + |
| 63 | + result = _verify(repository, "v0.2.0") |
| 64 | + |
| 65 | + assert result.returncode == 1 |
| 66 | + assert "does not exist" in result.stderr |
| 67 | + |
| 68 | + |
| 69 | +def test_release_ref_rejects_commit_sha_input(tmp_path: Path) -> None: |
| 70 | + repository = _repository(tmp_path) |
| 71 | + |
| 72 | + result = _verify(repository, _git(repository, "rev-parse", "HEAD")) |
| 73 | + |
| 74 | + assert result.returncode == 1 |
| 75 | + assert "does not match package version" in result.stderr |
| 76 | + |
| 77 | + |
| 78 | +def test_release_ref_rejects_missing_version_tag(tmp_path: Path) -> None: |
| 79 | + repository = _repository(tmp_path) |
| 80 | + |
| 81 | + result = _verify(repository, "v0.2.0") |
| 82 | + |
| 83 | + assert result.returncode == 1 |
| 84 | + assert "does not exist" in result.stderr |
| 85 | + |
| 86 | + |
| 87 | +def test_release_ref_rejects_tag_targeting_another_commit(tmp_path: Path) -> None: |
| 88 | + repository = _repository(tmp_path) |
| 89 | + _git(repository, "tag", "-a", "v0.2.0", "-m", "release") |
| 90 | + (repository / "README.md").write_text("later commit\n", encoding="utf-8") |
| 91 | + _git(repository, "add", "README.md") |
| 92 | + _git(repository, "commit", "--quiet", "-m", "later") |
| 93 | + |
| 94 | + result = _verify(repository, "v0.2.0") |
| 95 | + |
| 96 | + assert result.returncode == 1 |
| 97 | + assert "not HEAD" in result.stderr |
0 commit comments