|
| 1 | +"""Command templates with a py: script line must render for --script py. |
| 2 | +
|
| 3 | +Covers #3283: ``py:`` lines in the ``scripts:`` frontmatter of |
| 4 | +``templates/commands/*.md`` reference Python scripts that exist in the repo, |
| 5 | +and ``process_template`` turns them into a valid Python invocation |
| 6 | +(interpreter-prefixed, path rewritten to the ``.specify`` tree). |
| 7 | +
|
| 8 | +``plan.md`` and ``tasks.md`` gain their ``py:`` lines together with |
| 9 | +``setup_plan.py``/``setup_tasks.py`` in the core-scripts port (#3280); the |
| 10 | +existence check below enforces that ordering. |
| 11 | +""" |
| 12 | + |
| 13 | +import re |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from specify_cli.integrations.base import IntegrationBase |
| 19 | + |
| 20 | +REPO_ROOT = Path(__file__).parent.parent |
| 21 | +TEMPLATES_DIR = REPO_ROOT / "templates" / "commands" |
| 22 | + |
| 23 | +_PY_LINE = re.compile(r"^\s*py: (scripts/python/\S+\.py)", re.MULTILINE) |
| 24 | + |
| 25 | + |
| 26 | +def _py_script(name: str) -> str | None: |
| 27 | + match = _PY_LINE.search((TEMPLATES_DIR / name).read_text(encoding="utf-8")) |
| 28 | + return match.group(1) if match else None |
| 29 | + |
| 30 | + |
| 31 | +PY_TEMPLATES = sorted( |
| 32 | + p.name for p in TEMPLATES_DIR.glob("*.md") if _py_script(p.name) |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(autouse=True) |
| 37 | +def _pin_interpreter(monkeypatch): |
| 38 | + monkeypatch.setattr( |
| 39 | + "specify_cli.integrations.base.shutil.which", |
| 40 | + lambda name: "/usr/bin/python3" if name == "python3" else None, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_py_templates_discovered(): |
| 45 | + # Guard: the glob must find the known py-scripted templates, otherwise |
| 46 | + # the parametrized tests below would silently pass on an empty set. |
| 47 | + assert "implement.md" in PY_TEMPLATES |
| 48 | + assert "clarify.md" in PY_TEMPLATES |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize("name", PY_TEMPLATES) |
| 52 | +def test_referenced_python_script_exists(name: str): |
| 53 | + # A py: line must never point at a script the repo does not ship — |
| 54 | + # rendering would produce a broken invocation at runtime. |
| 55 | + script = _py_script(name) |
| 56 | + assert (REPO_ROOT / script).is_file(), f"{name} references missing {script}" |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize("name", PY_TEMPLATES) |
| 60 | +def test_template_renders_python_invocation(name: str): |
| 61 | + content = (TEMPLATES_DIR / name).read_text(encoding="utf-8") |
| 62 | + result = IntegrationBase.process_template(content, "agent", "py") |
| 63 | + assert "{SCRIPT}" not in result |
| 64 | + assert re.search( |
| 65 | + r"python3 \.specify/scripts/python/\w+\.py(?: --[\w-]+)*", result |
| 66 | + ), f"{name} did not render a Python invocation" |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize("name", PY_TEMPLATES) |
| 70 | +def test_sh_rendering_unchanged(name: str): |
| 71 | + # Negative: adding py: lines must not leak into sh rendering. |
| 72 | + content = (TEMPLATES_DIR / name).read_text(encoding="utf-8") |
| 73 | + result = IntegrationBase.process_template(content, "agent", "sh") |
| 74 | + assert "{SCRIPT}" not in result |
| 75 | + assert "scripts/python" not in result |
| 76 | + |
| 77 | + |
| 78 | +def test_install_shared_infra_copies_python_scripts(tmp_path): |
| 79 | + # --script py must install scripts/python/ into .specify/scripts/python/ |
| 80 | + # so the rendered invocations point at files that exist. |
| 81 | + from rich.console import Console |
| 82 | + |
| 83 | + from specify_cli.shared_infra import install_shared_infra |
| 84 | + |
| 85 | + install_shared_infra( |
| 86 | + tmp_path, |
| 87 | + "py", |
| 88 | + version="0.0.0", |
| 89 | + core_pack=None, |
| 90 | + repo_root=REPO_ROOT, |
| 91 | + console=Console(quiet=True), |
| 92 | + force=False, |
| 93 | + ) |
| 94 | + dest = tmp_path / ".specify" / "scripts" / "python" |
| 95 | + assert (dest / "check_prerequisites.py").is_file() |
| 96 | + assert not (tmp_path / ".specify" / "scripts" / "powershell").exists() |
0 commit comments