|
| 1 | +"""Tests for setup-plan bypassing branch-pattern checks when feature.json is valid.""" |
| 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 | +SETUP_PLAN_SH = PROJECT_ROOT / "scripts" / "bash" / "setup-plan.sh" |
| 16 | +COMMON_PS = PROJECT_ROOT / "scripts" / "powershell" / "common.ps1" |
| 17 | +SETUP_PLAN_PS = PROJECT_ROOT / "scripts" / "powershell" / "setup-plan.ps1" |
| 18 | +PLAN_TEMPLATE = PROJECT_ROOT / "templates" / "plan-template.md" |
| 19 | + |
| 20 | +HAS_PWSH = shutil.which("pwsh") is not None |
| 21 | +_POWERSHELL = shutil.which("powershell.exe") or shutil.which("powershell") |
| 22 | + |
| 23 | + |
| 24 | +def _install_bash_scripts(repo: Path) -> None: |
| 25 | + d = repo / ".specify" / "scripts" / "bash" |
| 26 | + d.mkdir(parents=True, exist_ok=True) |
| 27 | + shutil.copy(COMMON_SH, d / "common.sh") |
| 28 | + shutil.copy(SETUP_PLAN_SH, d / "setup-plan.sh") |
| 29 | + |
| 30 | + |
| 31 | +def _install_ps_scripts(repo: Path) -> None: |
| 32 | + d = repo / ".specify" / "scripts" / "powershell" |
| 33 | + d.mkdir(parents=True, exist_ok=True) |
| 34 | + shutil.copy(COMMON_PS, d / "common.ps1") |
| 35 | + shutil.copy(SETUP_PLAN_PS, d / "setup-plan.ps1") |
| 36 | + |
| 37 | + |
| 38 | +def _minimal_templates(repo: Path) -> None: |
| 39 | + tdir = repo / ".specify" / "templates" |
| 40 | + tdir.mkdir(parents=True, exist_ok=True) |
| 41 | + shutil.copy(PLAN_TEMPLATE, tdir / "plan-template.md") |
| 42 | + |
| 43 | + |
| 44 | +def _clean_env() -> dict[str, str]: |
| 45 | + """Return a copy of the current environment with any SPECIFY_* vars removed. |
| 46 | +
|
| 47 | + setup-plan.{sh,ps1} honors SPECIFY_FEATURE, SPECIFY_FEATURE_DIRECTORY, etc., |
| 48 | + which would otherwise leak from a developer shell or CI runner and make these |
| 49 | + tests flaky. Stripping them forces every case to rely purely on git branch + |
| 50 | + .specify/feature.json state set up by the fixture. |
| 51 | + """ |
| 52 | + env = os.environ.copy() |
| 53 | + for key in list(env): |
| 54 | + if key.startswith("SPECIFY_"): |
| 55 | + env.pop(key) |
| 56 | + return env |
| 57 | + |
| 58 | + |
| 59 | +def _git_init(repo: Path) -> None: |
| 60 | + subprocess.run(["git", "init", "-q"], cwd=repo, check=True) |
| 61 | + subprocess.run( |
| 62 | + ["git", "config", "user.email", "test@example.com"], cwd=repo, check=True |
| 63 | + ) |
| 64 | + subprocess.run(["git", "config", "user.name", "Test User"], cwd=repo, check=True) |
| 65 | + subprocess.run( |
| 66 | + ["git", "commit", "--allow-empty", "-m", "init", "-q"], cwd=repo, check=True |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture |
| 71 | +def plan_repo(tmp_path: Path) -> Path: |
| 72 | + repo = tmp_path / "proj" |
| 73 | + repo.mkdir() |
| 74 | + _git_init(repo) |
| 75 | + (repo / ".specify").mkdir() |
| 76 | + _minimal_templates(repo) |
| 77 | + _install_bash_scripts(repo) |
| 78 | + _install_ps_scripts(repo) |
| 79 | + return repo |
| 80 | + |
| 81 | + |
| 82 | +@requires_bash |
| 83 | +def test_setup_plan_passes_custom_branch_when_feature_json_valid(plan_repo: Path) -> None: |
| 84 | + subprocess.run( |
| 85 | + ["git", "checkout", "-q", "-b", "feature/my-feature-branch"], |
| 86 | + cwd=plan_repo, |
| 87 | + check=True, |
| 88 | + ) |
| 89 | + feat = plan_repo / "specs" / "001-tiny-notes-app" |
| 90 | + feat.mkdir(parents=True) |
| 91 | + (feat / "spec.md").write_text("# spec\n", encoding="utf-8") |
| 92 | + (plan_repo / ".specify" / "feature.json").write_text( |
| 93 | + json.dumps({"feature_directory": "specs/001-tiny-notes-app"}), |
| 94 | + encoding="utf-8", |
| 95 | + ) |
| 96 | + script = plan_repo / ".specify" / "scripts" / "bash" / "setup-plan.sh" |
| 97 | + result = subprocess.run( |
| 98 | + ["bash", str(script)], |
| 99 | + cwd=plan_repo, |
| 100 | + capture_output=True, |
| 101 | + text=True, |
| 102 | + check=False, |
| 103 | + env=_clean_env(), |
| 104 | + ) |
| 105 | + assert result.returncode == 0, result.stderr + result.stdout |
| 106 | + assert (feat / "plan.md").is_file() |
| 107 | + |
| 108 | + |
| 109 | +@requires_bash |
| 110 | +def test_setup_plan_fails_custom_branch_without_feature_json(plan_repo: Path) -> None: |
| 111 | + subprocess.run( |
| 112 | + ["git", "checkout", "-q", "-b", "feature/my-feature-branch"], |
| 113 | + cwd=plan_repo, |
| 114 | + check=True, |
| 115 | + ) |
| 116 | + script = plan_repo / ".specify" / "scripts" / "bash" / "setup-plan.sh" |
| 117 | + result = subprocess.run( |
| 118 | + ["bash", str(script)], |
| 119 | + cwd=plan_repo, |
| 120 | + capture_output=True, |
| 121 | + text=True, |
| 122 | + check=False, |
| 123 | + env=_clean_env(), |
| 124 | + ) |
| 125 | + assert result.returncode != 0 |
| 126 | + assert "Not on a feature branch" in result.stderr |
| 127 | + |
| 128 | + |
| 129 | +@requires_bash |
| 130 | +def test_setup_plan_numbered_branch_unchanged_without_feature_json( |
| 131 | + plan_repo: Path, |
| 132 | +) -> None: |
| 133 | + subprocess.run( |
| 134 | + ["git", "checkout", "-q", "-b", "001-tiny-notes-app"], |
| 135 | + cwd=plan_repo, |
| 136 | + check=True, |
| 137 | + ) |
| 138 | + feat = plan_repo / "specs" / "001-tiny-notes-app" |
| 139 | + feat.mkdir(parents=True) |
| 140 | + (feat / "spec.md").write_text("# spec\n", encoding="utf-8") |
| 141 | + script = plan_repo / ".specify" / "scripts" / "bash" / "setup-plan.sh" |
| 142 | + result = subprocess.run( |
| 143 | + ["bash", str(script)], |
| 144 | + cwd=plan_repo, |
| 145 | + capture_output=True, |
| 146 | + text=True, |
| 147 | + check=False, |
| 148 | + env=_clean_env(), |
| 149 | + ) |
| 150 | + assert result.returncode == 0, result.stderr + result.stdout |
| 151 | + assert (feat / "plan.md").is_file() |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.skipif(not (HAS_PWSH or _POWERSHELL), reason="no PowerShell available") |
| 155 | +def test_setup_plan_ps_passes_custom_branch_when_feature_json_valid(plan_repo: Path) -> None: |
| 156 | + subprocess.run( |
| 157 | + ["git", "checkout", "-q", "-b", "feature/my-feature-branch"], |
| 158 | + cwd=plan_repo, |
| 159 | + check=True, |
| 160 | + ) |
| 161 | + feat = plan_repo / "specs" / "001-tiny-notes-app" |
| 162 | + feat.mkdir(parents=True) |
| 163 | + (feat / "spec.md").write_text("# spec\n", encoding="utf-8") |
| 164 | + (plan_repo / ".specify" / "feature.json").write_text( |
| 165 | + json.dumps({"feature_directory": "specs/001-tiny-notes-app"}), |
| 166 | + encoding="utf-8", |
| 167 | + ) |
| 168 | + script = plan_repo / ".specify" / "scripts" / "powershell" / "setup-plan.ps1" |
| 169 | + exe = "pwsh" if HAS_PWSH else _POWERSHELL |
| 170 | + result = subprocess.run( |
| 171 | + [exe, "-NoProfile", "-File", str(script)], |
| 172 | + cwd=plan_repo, |
| 173 | + capture_output=True, |
| 174 | + text=True, |
| 175 | + check=False, |
| 176 | + env=_clean_env(), |
| 177 | + ) |
| 178 | + assert result.returncode == 0, result.stderr + result.stdout |
| 179 | + assert (feat / "plan.md").is_file() |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.skipif(not (HAS_PWSH or _POWERSHELL), reason="no PowerShell available") |
| 183 | +def test_setup_plan_ps_fails_custom_branch_without_feature_json( |
| 184 | + plan_repo: Path, |
| 185 | +) -> None: |
| 186 | + subprocess.run( |
| 187 | + ["git", "checkout", "-q", "-b", "feature/my-feature-branch"], |
| 188 | + cwd=plan_repo, |
| 189 | + check=True, |
| 190 | + ) |
| 191 | + script = plan_repo / ".specify" / "scripts" / "powershell" / "setup-plan.ps1" |
| 192 | + exe = "pwsh" if HAS_PWSH else _POWERSHELL |
| 193 | + result = subprocess.run( |
| 194 | + [exe, "-NoProfile", "-File", str(script)], |
| 195 | + cwd=plan_repo, |
| 196 | + capture_output=True, |
| 197 | + text=True, |
| 198 | + check=False, |
| 199 | + env=_clean_env(), |
| 200 | + ) |
| 201 | + assert result.returncode != 0 |
| 202 | + assert "Not on a feature branch" in result.stderr |
0 commit comments