1- """Every command template with a scripts: block must render for --script py.
1+ """Command templates with a py: script line must render for --script py.
22
3- Covers #3283: each ``templates/commands/*.md`` that declares a ``scripts:``
4- frontmatter block carries a ``py:`` line, and ``process_template`` turns it
5- into a valid Python invocation (interpreter-prefixed, path rewritten to the
6- ``.specify`` tree).
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.
711"""
812
913import re
1317
1418from specify_cli .integrations .base import IntegrationBase
1519
16- TEMPLATES_DIR = Path (__file__ ).parent .parent / "templates" / "commands"
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
1729
18- SCRIPTED_TEMPLATES = sorted (
19- p .name for p in TEMPLATES_DIR .glob ("*.md" ) if "\n scripts:\n " in p .read_text (encoding = "utf-8" )
30+
31+ PY_TEMPLATES = sorted (
32+ p .name for p in TEMPLATES_DIR .glob ("*.md" ) if _py_script (p .name )
2033)
2134
2235
@@ -28,22 +41,22 @@ def _pin_interpreter(monkeypatch):
2841 )
2942
3043
31- def test_scripted_templates_discovered ():
32- # Guard: the glob must find the known scripted templates, otherwise the
33- # parametrized tests below would silently pass on an empty set.
34- assert "plan .md" in SCRIPTED_TEMPLATES
35- assert "implement .md" in SCRIPTED_TEMPLATES
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
3649
3750
38- @pytest .mark .parametrize ("name" , SCRIPTED_TEMPLATES )
39- def test_template_declares_py_script (name : str ):
40- content = ( TEMPLATES_DIR / name ). read_text ( encoding = "utf-8" )
41- assert re . search ( r"^\s*py: scripts/python/\S+\.py" , content , re . MULTILINE ), (
42- f" { name } has a scripts: block but no py: line"
43- )
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 } "
4457
4558
46- @pytest .mark .parametrize ("name" , SCRIPTED_TEMPLATES )
59+ @pytest .mark .parametrize ("name" , PY_TEMPLATES )
4760def test_template_renders_python_invocation (name : str ):
4861 content = (TEMPLATES_DIR / name ).read_text (encoding = "utf-8" )
4962 result = IntegrationBase .process_template (content , "agent" , "py" )
@@ -53,7 +66,7 @@ def test_template_renders_python_invocation(name: str):
5366 ), f"{ name } did not render a Python invocation"
5467
5568
56- @pytest .mark .parametrize ("name" , SCRIPTED_TEMPLATES )
69+ @pytest .mark .parametrize ("name" , PY_TEMPLATES )
5770def test_sh_rendering_unchanged (name : str ):
5871 # Negative: adding py: lines must not leak into sh rendering.
5972 content = (TEMPLATES_DIR / name ).read_text (encoding = "utf-8" )
@@ -74,7 +87,7 @@ def test_install_shared_infra_copies_python_scripts(tmp_path):
7487 "py" ,
7588 version = "0.0.0" ,
7689 core_pack = None ,
77- repo_root = Path ( __file__ ). resolve (). parents [ 1 ] ,
90+ repo_root = REPO_ROOT ,
7891 console = Console (quiet = True ),
7992 force = False ,
8093 )
0 commit comments