|
| 1 | +"""Tests for check-prerequisites --paths-only skipping branch validation (#2653).""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from tests.conftest import requires_bash |
| 12 | + |
| 13 | +PROJECT_ROOT = Path(__file__).resolve().parent.parent |
| 14 | +COMMON_SH = PROJECT_ROOT / "scripts" / "bash" / "common.sh" |
| 15 | +CHECK_PREREQS_SH = PROJECT_ROOT / "scripts" / "bash" / "check-prerequisites.sh" |
| 16 | +COMMON_PS = PROJECT_ROOT / "scripts" / "powershell" / "common.ps1" |
| 17 | +CHECK_PREREQS_PS = PROJECT_ROOT / "scripts" / "powershell" / "check-prerequisites.ps1" |
| 18 | + |
| 19 | +HAS_PWSH = shutil.which("pwsh") is not None |
| 20 | +_POWERSHELL = shutil.which("powershell.exe") or shutil.which("powershell") |
| 21 | + |
| 22 | + |
| 23 | +def _install_bash_scripts(repo: Path) -> None: |
| 24 | + d = repo / ".specify" / "scripts" / "bash" |
| 25 | + d.mkdir(parents=True, exist_ok=True) |
| 26 | + shutil.copy(COMMON_SH, d / "common.sh") |
| 27 | + shutil.copy(CHECK_PREREQS_SH, d / "check-prerequisites.sh") |
| 28 | + |
| 29 | + |
| 30 | +def _install_ps_scripts(repo: Path) -> None: |
| 31 | + d = repo / ".specify" / "scripts" / "powershell" |
| 32 | + d.mkdir(parents=True, exist_ok=True) |
| 33 | + shutil.copy(COMMON_PS, d / "common.ps1") |
| 34 | + shutil.copy(CHECK_PREREQS_PS, d / "check-prerequisites.ps1") |
| 35 | + |
| 36 | + |
| 37 | +def _clean_env() -> dict[str, str]: |
| 38 | + env = os.environ.copy() |
| 39 | + for key in list(env): |
| 40 | + if key.startswith("SPECIFY_"): |
| 41 | + env.pop(key) |
| 42 | + return env |
| 43 | + |
| 44 | + |
| 45 | +def _git_init(repo: Path) -> None: |
| 46 | + subprocess.run(["git", "init", "-q"], cwd=repo, check=True) |
| 47 | + subprocess.run( |
| 48 | + ["git", "config", "user.email", "test@example.com"], cwd=repo, check=True |
| 49 | + ) |
| 50 | + subprocess.run(["git", "config", "user.name", "Test User"], cwd=repo, check=True) |
| 51 | + subprocess.run( |
| 52 | + ["git", "commit", "--allow-empty", "-m", "init", "-q"], cwd=repo, check=True |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture |
| 57 | +def prereq_repo(tmp_path: Path) -> Path: |
| 58 | + repo = tmp_path / "proj" |
| 59 | + repo.mkdir() |
| 60 | + _git_init(repo) |
| 61 | + (repo / ".specify").mkdir() |
| 62 | + _install_bash_scripts(repo) |
| 63 | + _install_ps_scripts(repo) |
| 64 | + return repo |
| 65 | + |
| 66 | + |
| 67 | +# ── Bash tests ──────────────────────────────────────────────────────────── |
| 68 | + |
| 69 | + |
| 70 | +@requires_bash |
| 71 | +def test_paths_only_succeeds_on_non_spec_branch(prereq_repo: Path) -> None: |
| 72 | + """--paths-only must return paths without branch validation (main branch).""" |
| 73 | + script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh" |
| 74 | + result = subprocess.run( |
| 75 | + ["bash", str(script), "--json", "--paths-only"], |
| 76 | + cwd=prereq_repo, |
| 77 | + capture_output=True, |
| 78 | + text=True, |
| 79 | + check=False, |
| 80 | + env=_clean_env(), |
| 81 | + ) |
| 82 | + assert result.returncode == 0, result.stderr |
| 83 | + data = json.loads(result.stdout) |
| 84 | + assert "REPO_ROOT" in data |
| 85 | + assert "BRANCH" in data |
| 86 | + assert "FEATURE_DIR" in data |
| 87 | + |
| 88 | + |
| 89 | +@requires_bash |
| 90 | +def test_paths_only_succeeds_on_spec_branch(prereq_repo: Path) -> None: |
| 91 | + """--paths-only must also work on a properly named spec branch.""" |
| 92 | + subprocess.run( |
| 93 | + ["git", "checkout", "-q", "-b", "001-my-feature"], |
| 94 | + cwd=prereq_repo, |
| 95 | + check=True, |
| 96 | + ) |
| 97 | + script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh" |
| 98 | + result = subprocess.run( |
| 99 | + ["bash", str(script), "--json", "--paths-only"], |
| 100 | + cwd=prereq_repo, |
| 101 | + capture_output=True, |
| 102 | + text=True, |
| 103 | + check=False, |
| 104 | + env=_clean_env(), |
| 105 | + ) |
| 106 | + assert result.returncode == 0, result.stderr |
| 107 | + data = json.loads(result.stdout) |
| 108 | + assert "FEATURE_DIR" in data |
| 109 | + assert "001-my-feature" in data.get("BRANCH", "") |
| 110 | + |
| 111 | + |
| 112 | +@requires_bash |
| 113 | +def test_paths_only_text_mode_on_non_spec_branch(prereq_repo: Path) -> None: |
| 114 | + """--paths-only without --json must return text paths on a non-spec branch.""" |
| 115 | + script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh" |
| 116 | + result = subprocess.run( |
| 117 | + ["bash", str(script), "--paths-only"], |
| 118 | + cwd=prereq_repo, |
| 119 | + capture_output=True, |
| 120 | + text=True, |
| 121 | + check=False, |
| 122 | + env=_clean_env(), |
| 123 | + ) |
| 124 | + assert result.returncode == 0, result.stderr |
| 125 | + assert "REPO_ROOT:" in result.stdout |
| 126 | + assert "FEATURE_DIR:" in result.stdout |
| 127 | + |
| 128 | + |
| 129 | +@requires_bash |
| 130 | +def test_normal_mode_still_validates_branch(prereq_repo: Path) -> None: |
| 131 | + """Without --paths-only, branch validation must still fail on main.""" |
| 132 | + script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh" |
| 133 | + result = subprocess.run( |
| 134 | + ["bash", str(script), "--json"], |
| 135 | + cwd=prereq_repo, |
| 136 | + capture_output=True, |
| 137 | + text=True, |
| 138 | + check=False, |
| 139 | + env=_clean_env(), |
| 140 | + ) |
| 141 | + assert result.returncode != 0 |
| 142 | + assert "Not on a feature branch" in result.stderr |
| 143 | + |
| 144 | + |
| 145 | +# ── PowerShell tests ────────────────────────────────────────────────────── |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.skipif(not (HAS_PWSH or _POWERSHELL), reason="no PowerShell available") |
| 149 | +def test_ps_paths_only_succeeds_on_non_spec_branch(prereq_repo: Path) -> None: |
| 150 | + """-PathsOnly must return paths without branch validation (main branch).""" |
| 151 | + script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1" |
| 152 | + exe = "pwsh" if HAS_PWSH else _POWERSHELL |
| 153 | + result = subprocess.run( |
| 154 | + [exe, "-NoProfile", "-File", str(script), "-Json", "-PathsOnly"], |
| 155 | + cwd=prereq_repo, |
| 156 | + capture_output=True, |
| 157 | + text=True, |
| 158 | + check=False, |
| 159 | + env=_clean_env(), |
| 160 | + ) |
| 161 | + assert result.returncode == 0, result.stderr |
| 162 | + data = json.loads(result.stdout) |
| 163 | + assert "REPO_ROOT" in data |
| 164 | + assert "BRANCH" in data |
| 165 | + assert "FEATURE_DIR" in data |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.skipif(not (HAS_PWSH or _POWERSHELL), reason="no PowerShell available") |
| 169 | +def test_ps_paths_only_succeeds_on_spec_branch(prereq_repo: Path) -> None: |
| 170 | + """-PathsOnly must also work on a properly named spec branch.""" |
| 171 | + subprocess.run( |
| 172 | + ["git", "checkout", "-q", "-b", "001-my-feature"], |
| 173 | + cwd=prereq_repo, |
| 174 | + check=True, |
| 175 | + ) |
| 176 | + script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1" |
| 177 | + exe = "pwsh" if HAS_PWSH else _POWERSHELL |
| 178 | + result = subprocess.run( |
| 179 | + [exe, "-NoProfile", "-File", str(script), "-Json", "-PathsOnly"], |
| 180 | + cwd=prereq_repo, |
| 181 | + capture_output=True, |
| 182 | + text=True, |
| 183 | + check=False, |
| 184 | + env=_clean_env(), |
| 185 | + ) |
| 186 | + assert result.returncode == 0, result.stderr |
| 187 | + data = json.loads(result.stdout) |
| 188 | + assert "FEATURE_DIR" in data |
| 189 | + |
| 190 | + |
| 191 | +@pytest.mark.skipif(not (HAS_PWSH or _POWERSHELL), reason="no PowerShell available") |
| 192 | +def test_ps_normal_mode_still_validates_branch(prereq_repo: Path) -> None: |
| 193 | + """Without -PathsOnly, branch validation must still fail on main.""" |
| 194 | + script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1" |
| 195 | + exe = "pwsh" if HAS_PWSH else _POWERSHELL |
| 196 | + result = subprocess.run( |
| 197 | + [exe, "-NoProfile", "-File", str(script), "-Json"], |
| 198 | + cwd=prereq_repo, |
| 199 | + capture_output=True, |
| 200 | + text=True, |
| 201 | + check=False, |
| 202 | + env=_clean_env(), |
| 203 | + ) |
| 204 | + assert result.returncode != 0 |
| 205 | + assert "Not on a feature branch" in result.stderr |
0 commit comments