|
| 1 | +"""Tests for the SPECIFY_INIT_DIR override in the Python CLI (`specify`). |
| 2 | +
|
| 3 | +PR #2892 taught the shell resolver (`get_repo_root` / `Get-RepoRoot`) to honor |
| 4 | +SPECIFY_INIT_DIR, so the core slash-command scripts can target a member project |
| 5 | +from a monorepo root. This extends the same validation rules to the Python CLI's |
| 6 | +project resolution — `_require_specify_project()` (the chokepoint for every |
| 7 | +project-scoped subcommand) and the `workflow run <file>` standalone-YAML path — |
| 8 | +so those can target a member project without `cd` too. |
| 9 | +
|
| 10 | +The contract mirrors `tests/test_init_dir.py` (the shell side): the value names |
| 11 | +the project root (the directory *containing* `.specify/`), relative paths |
| 12 | +resolve against cwd, and an invalid value hard-errors with no silent fallback to |
| 13 | +cwd. See proposals/monorepo-support and github/spec-kit discussion #2834. |
| 14 | +
|
| 15 | +SPECIFY_* vars are stripped from the environment for every test by the autouse |
| 16 | +`_strip_specify_env` fixture in conftest.py; tests that want an override set it |
| 17 | +explicitly via monkeypatch. |
| 18 | +""" |
| 19 | + |
| 20 | +import pytest |
| 21 | +import yaml |
| 22 | +from typer.testing import CliRunner |
| 23 | + |
| 24 | +from specify_cli import app |
| 25 | + |
| 26 | +runner = CliRunner() |
| 27 | + |
| 28 | + |
| 29 | +def _make_project(root, name): |
| 30 | + """Create <root>/<name>/.specify (the minimal Spec Kit project marker).""" |
| 31 | + proj = root / name |
| 32 | + (proj / ".specify").mkdir(parents=True) |
| 33 | + return proj |
| 34 | + |
| 35 | + |
| 36 | +def _workflow_yaml(wf_id): |
| 37 | + """A minimal valid standalone workflow YAML with a single no-op shell step.""" |
| 38 | + return yaml.dump( |
| 39 | + { |
| 40 | + "schema_version": "1.0", |
| 41 | + "workflow": { |
| 42 | + "id": wf_id, |
| 43 | + "name": wf_id, |
| 44 | + "version": "1.0.0", |
| 45 | + "description": f"standalone workflow {wf_id}", |
| 46 | + }, |
| 47 | + "steps": [{"id": "noop", "type": "shell", "run": "echo done"}], |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +# ── chokepoint: _require_specify_project() via `workflow list` ─────────────── |
| 53 | +# `workflow list` is the lightest subcommand routed through the chokepoint: it |
| 54 | +# resolves the project, then reads <project>/.specify/workflows/. An empty |
| 55 | +# project prints "No workflows installed"; a failed resolution prints the error |
| 56 | +# and exits non-zero. |
| 57 | + |
| 58 | + |
| 59 | +def test_override_redirects_to_sibling_from_nonproject_cwd(tmp_path, monkeypatch): |
| 60 | + """A valid SPECIFY_INIT_DIR resolves the target even when cwd is not itself a |
| 61 | + project — without the override this would error 'Not a spec-kit project'.""" |
| 62 | + elsewhere = tmp_path / "elsewhere" |
| 63 | + elsewhere.mkdir() |
| 64 | + web = _make_project(tmp_path, "web") |
| 65 | + monkeypatch.chdir(elsewhere) |
| 66 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(web)) |
| 67 | + |
| 68 | + result = runner.invoke(app, ["workflow", "list"]) |
| 69 | + assert result.exit_code == 0, result.output |
| 70 | + assert "No workflows installed" in result.output |
| 71 | + |
| 72 | + |
| 73 | +def test_override_relative_path_normalized_against_cwd(tmp_path, monkeypatch): |
| 74 | + web = _make_project(tmp_path, "web") |
| 75 | + monkeypatch.chdir(tmp_path) |
| 76 | + monkeypatch.setenv("SPECIFY_INIT_DIR", "web") |
| 77 | + |
| 78 | + result = runner.invoke(app, ["workflow", "list"]) |
| 79 | + assert result.exit_code == 0, result.output |
| 80 | + assert "No workflows installed" in result.output |
| 81 | + assert web.exists() |
| 82 | + |
| 83 | + |
| 84 | +def test_override_trailing_slash_tolerated(tmp_path, monkeypatch): |
| 85 | + _make_project(tmp_path, "web") |
| 86 | + monkeypatch.chdir(tmp_path) |
| 87 | + monkeypatch.setenv("SPECIFY_INIT_DIR", "web/") |
| 88 | + |
| 89 | + result = runner.invoke(app, ["workflow", "list"]) |
| 90 | + assert result.exit_code == 0, result.output |
| 91 | + assert "No workflows installed" in result.output |
| 92 | + |
| 93 | + |
| 94 | +def test_unset_override_uses_cwd(tmp_path, monkeypatch): |
| 95 | + """With SPECIFY_INIT_DIR unset, the project is the current directory.""" |
| 96 | + cwd_proj = _make_project(tmp_path, "cwd") |
| 97 | + monkeypatch.chdir(cwd_proj) |
| 98 | + |
| 99 | + result = runner.invoke(app, ["workflow", "list"]) |
| 100 | + assert result.exit_code == 0, result.output |
| 101 | + assert "No workflows installed" in result.output |
| 102 | + |
| 103 | + |
| 104 | +def test_empty_override_treated_as_unset(tmp_path, monkeypatch): |
| 105 | + """An empty SPECIFY_INIT_DIR behaves as unset (falls through to cwd), not as |
| 106 | + '.' — which from a deep non-project cwd would otherwise diverge.""" |
| 107 | + cwd_proj = _make_project(tmp_path, "cwd") |
| 108 | + monkeypatch.chdir(cwd_proj) |
| 109 | + monkeypatch.setenv("SPECIFY_INIT_DIR", "") |
| 110 | + |
| 111 | + result = runner.invoke(app, ["workflow", "list"]) |
| 112 | + assert result.exit_code == 0, result.output |
| 113 | + assert "No workflows installed" in result.output |
| 114 | + |
| 115 | + |
| 116 | +def test_override_nonexistent_errors_no_fallback(tmp_path, monkeypatch): |
| 117 | + """A non-existent path hard-errors even from inside a valid project, proving |
| 118 | + there is no silent fallback to the cwd project.""" |
| 119 | + cwd_proj = _make_project(tmp_path, "cwd") |
| 120 | + monkeypatch.chdir(cwd_proj) |
| 121 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(tmp_path / "does_not_exist")) |
| 122 | + |
| 123 | + result = runner.invoke(app, ["workflow", "list"]) |
| 124 | + assert result.exit_code != 0 |
| 125 | + assert "does not point to an existing directory" in result.output |
| 126 | + assert "No workflows installed" not in result.output # no fallback to cwd |
| 127 | + |
| 128 | + |
| 129 | +def test_override_without_specify_errors_no_fallback(tmp_path, monkeypatch): |
| 130 | + """A path that exists but lacks .specify/ hard-errors, no fallback.""" |
| 131 | + cwd_proj = _make_project(tmp_path, "cwd") |
| 132 | + nodot = tmp_path / "nodot" |
| 133 | + nodot.mkdir() |
| 134 | + monkeypatch.chdir(cwd_proj) |
| 135 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(nodot)) |
| 136 | + |
| 137 | + result = runner.invoke(app, ["workflow", "list"]) |
| 138 | + assert result.exit_code != 0 |
| 139 | + assert "not a Spec Kit project" in result.output |
| 140 | + assert "No workflows installed" not in result.output |
| 141 | + |
| 142 | + |
| 143 | +def test_override_file_path_errors_no_fallback(tmp_path, monkeypatch): |
| 144 | + """A path that is a file (not a directory) hard-errors with the |
| 145 | + existing-directory message.""" |
| 146 | + cwd_proj = _make_project(tmp_path, "cwd") |
| 147 | + a_file = tmp_path / "afile" |
| 148 | + a_file.write_text("x") |
| 149 | + monkeypatch.chdir(cwd_proj) |
| 150 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(a_file)) |
| 151 | + |
| 152 | + result = runner.invoke(app, ["workflow", "list"]) |
| 153 | + assert result.exit_code != 0 |
| 154 | + assert "does not point to an existing directory" in result.output |
| 155 | + |
| 156 | + |
| 157 | +# ── bypass: `workflow run <file>` ──────────────────────────────────────────── |
| 158 | + |
| 159 | + |
| 160 | +def test_override_redirects_workflow_run_file(tmp_path, monkeypatch): |
| 161 | + """Running a standalone YAML with SPECIFY_INIT_DIR set uses the target as the |
| 162 | + project root: run artifacts land under the target, not cwd.""" |
| 163 | + web = _make_project(tmp_path, "web") |
| 164 | + elsewhere = tmp_path / "elsewhere" |
| 165 | + elsewhere.mkdir() |
| 166 | + workflow_file = elsewhere / "wf.yml" |
| 167 | + workflow_file.write_text(_workflow_yaml("override-run"), encoding="utf-8") |
| 168 | + monkeypatch.chdir(elsewhere) |
| 169 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(web)) |
| 170 | + |
| 171 | + result = runner.invoke(app, ["workflow", "run", str(workflow_file)], catch_exceptions=False) |
| 172 | + assert result.exit_code == 0, result.output |
| 173 | + assert (web / ".specify" / "workflows" / "runs").is_dir() |
| 174 | + assert not (elsewhere / ".specify").exists() # cwd was not used as the project |
| 175 | + |
| 176 | + |
| 177 | +def test_override_invalid_errors_workflow_run_file(tmp_path, monkeypatch): |
| 178 | + """An invalid SPECIFY_INIT_DIR hard-errors the file path too — no fallback to |
| 179 | + cwd's standalone-YAML behavior.""" |
| 180 | + elsewhere = tmp_path / "elsewhere" |
| 181 | + elsewhere.mkdir() |
| 182 | + workflow_file = elsewhere / "wf.yml" |
| 183 | + workflow_file.write_text(_workflow_yaml("x"), encoding="utf-8") |
| 184 | + monkeypatch.chdir(elsewhere) |
| 185 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(tmp_path / "does_not_exist")) |
| 186 | + |
| 187 | + result = runner.invoke(app, ["workflow", "run", str(workflow_file)]) |
| 188 | + assert result.exit_code != 0 |
| 189 | + assert "does not point to an existing directory" in result.output |
| 190 | + |
| 191 | + |
| 192 | +def test_override_rejects_symlinked_specify(tmp_path, monkeypatch): |
| 193 | + """`workflow run <file>` refuses a symlinked .specify under the override |
| 194 | + target, matching the guard the cwd path applies (the override resolver's |
| 195 | + is_dir() check follows symlinks, so this is re-checked on the override path).""" |
| 196 | + web = tmp_path / "web" |
| 197 | + web.mkdir() |
| 198 | + real = tmp_path / "real-specify" |
| 199 | + real.mkdir() |
| 200 | + try: |
| 201 | + (web / ".specify").symlink_to(real, target_is_directory=True) |
| 202 | + except (OSError, NotImplementedError): |
| 203 | + pytest.skip("Symlinks are not available in this environment") |
| 204 | + elsewhere = tmp_path / "elsewhere" |
| 205 | + elsewhere.mkdir() |
| 206 | + workflow_file = elsewhere / "wf.yml" |
| 207 | + workflow_file.write_text(_workflow_yaml("symlink-run"), encoding="utf-8") |
| 208 | + monkeypatch.chdir(elsewhere) |
| 209 | + monkeypatch.setenv("SPECIFY_INIT_DIR", str(web)) |
| 210 | + |
| 211 | + result = runner.invoke(app, ["workflow", "run", str(workflow_file)]) |
| 212 | + assert result.exit_code != 0 |
| 213 | + assert "Refusing to use symlinked .specify path" in result.output |
0 commit comments