Files
github-spec-kit/tests/test_setup_tasks_python_parity.py
Marsel Safin 2a0ada9a6a feat(scripts): port create-new-feature, setup-plan and setup-tasks to Python (#3386)
* feat(scripts): port create-new-feature, setup-plan and setup-tasks to Python

Ports the three core workflow scripts to Python as part of #3280,
following the check-prerequisites PoC pattern from #3302. Adds
resolve_template() to the shared common.py module and parity tests
that run bash and Python side by side.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(tests): treat only None env as unset in parity run helper

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): fall back to directory scan on any registry error, skip hidden preset dirs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* feat(templates): add py: lines for setup_plan and setup_tasks

Ships with the scripts they reference; the remaining templates got
their py: lines in #3403.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: support py variant in skills placeholder resolver

resolve_skill_placeholders only accepted sh/ps, so a py init option
fell into the fallback path and {SCRIPT} rendered without an
interpreter prefix. Accept py and prefix the resolved interpreter,
matching process_template. Also guard ps_cmd against a missing
PowerShell with a clear assert.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* test: pin clean-error behavior for invalid --number

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(scripts): reword unused-arg comment to match implementation

The loop accepts and silently ignores extra positional args (it doesn't
build a collected list); match the wording to what the code and
setup-plan.sh actually do.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: fall back when configured script variant is missing from frontmatter

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): reject signed/whitespace --number values to match bash 10# parity

The bash twin uses $((10#$BRANCH_NUMBER)), which rejects signed and
whitespace-padded values. Python's int() accepted them (e.g. -1),
producing a malformed -01-... prefix that sequential scans ignore.
Restrict --number to unsigned decimal digits before conversion, and
pin the parity with a bash-comparison test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): complete Python port installation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(integrations): fall back for missing script variants

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* test: make Python script checks platform-aware

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix Windows Python command invocation parity

Use PowerShell's call operator for spaced Python interpreter paths and align setup-tasks missing-template errors across script variants.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): preserve cross-platform Python parity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: reject signed PowerShell feature numbers

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): align feature number range

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): reject exhausted feature numbers

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): complete create feature parity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): align create feature outputs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): harden cross-platform parity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): keep truncation JSON clean

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): align setup failure parity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): close parity edge cases

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): propagate PowerShell setup errors

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): harden fallback resolution

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): stabilize PowerShell fallbacks

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): complete setup-plan parity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(cli): require runnable script fallbacks

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(cli): preserve shell fallback without preference

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): restore help and symlink parity

- setup-tasks.ps1: check -Help before unknown-argument validation so
  '-Help --bogus' exits 0 like the Bash/Python variants
- common.py: strip the repo root prefix lexically in persist_feature_json
  instead of resolve(), so a symlinked specs/ still persists the relative
  'specs/NNN-name' path the Bash/PowerShell helpers store

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scripts): align persist-hint quoting with shlex.quote

- create-new-feature.sh: replace printf %q with a shell_quote helper that
  emits shlex.quote-identical output, so the persistence hints stay
  byte-identical between the Bash and Python variants (printf %q output
  also varies between bash versions)
- promote the negative --number test to an all-variants parity test now
  that Bash and PowerShell reject signed values consistently
- add a spaced-repo-path parity test for the persistence hints

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-07-21 11:40:15 -05:00

208 lines
7.1 KiB
Python

"""Parity tests for the Python setup-tasks port."""
from __future__ import annotations
from pathlib import Path
import pytest
from tests.conftest import requires_bash
from tests.parity_helpers import (
HAS_POWERSHELL,
bash_cmd,
clean_env,
install_scripts,
json_stdout,
make_repo,
normalize_status_text,
ps_cmd,
py_cmd,
run,
write_feature_json,
)
SCRIPT = "setup-tasks"
def _setup_repo(tmp_path: Path) -> Path:
repo = make_repo(tmp_path)
install_scripts(repo, SCRIPT)
write_feature_json(repo)
feature = repo / "specs" / "001-my-feature"
feature.mkdir(parents=True)
(feature / "plan.md").write_text("# plan\n", encoding="utf-8")
(feature / "spec.md").write_text("# spec\n", encoding="utf-8")
templates = repo / ".specify" / "templates"
templates.mkdir(parents=True)
(templates / "tasks-template.md").write_text("# Tasks Template\n", encoding="utf-8")
return repo
@pytest.fixture
def repo(tmp_path: Path) -> Path:
return _setup_repo(tmp_path)
@requires_bash
def test_python_json_output_matches_bash(repo: Path) -> None:
feature = repo / "specs" / "001-my-feature"
(feature / "research.md").write_text("# research\n", encoding="utf-8")
(feature / "data-model.md").write_text("# model\n", encoding="utf-8")
(feature / "quickstart.md").write_text("# quickstart\n", encoding="utf-8")
(feature / "contracts" / "v1").mkdir(parents=True)
bash = run(bash_cmd(repo, SCRIPT, "--json"), repo)
py = run(py_cmd(repo, SCRIPT, "--json"), repo)
assert py.returncode == bash.returncode == 0
assert py.stderr == bash.stderr == ""
assert json_stdout(py) == json_stdout(bash)
@requires_bash
def test_python_text_output_matches_bash(repo: Path) -> None:
feature = repo / "specs" / "001-my-feature"
(feature / "research.md").write_text("# research\n", encoding="utf-8")
(feature / "contracts").mkdir() # present but empty -> reported missing
bash = run(bash_cmd(repo, SCRIPT), repo)
py = run(py_cmd(repo, SCRIPT), repo)
assert py.returncode == bash.returncode == 0
assert py.stderr == bash.stderr == ""
assert normalize_status_text(py.stdout) == normalize_status_text(bash.stdout)
@requires_bash
def test_python_override_template_wins_matches_bash(repo: Path) -> None:
overrides = repo / ".specify" / "templates" / "overrides"
overrides.mkdir(parents=True)
(overrides / "tasks-template.md").write_text("# Override\n", encoding="utf-8")
bash = run(bash_cmd(repo, SCRIPT, "--json"), repo)
py = run(py_cmd(repo, SCRIPT, "--json"), repo)
assert py.returncode == bash.returncode == 0
assert json_stdout(py) == json_stdout(bash)
assert json_stdout(py)["TASKS_TEMPLATE"].endswith("overrides/tasks-template.md")
@requires_bash
@pytest.mark.parametrize(
"missing",
["plan.md", "spec.md", "tasks-template"],
ids=["missing_plan", "missing_spec", "missing_tasks_template"],
)
def test_python_error_output_matches_bash(repo: Path, missing: str) -> None:
if missing == "tasks-template":
(repo / ".specify" / "templates" / "tasks-template.md").unlink()
else:
(repo / "specs" / "001-my-feature" / missing).unlink()
bash = run(bash_cmd(repo, SCRIPT, "--json"), repo)
py = run(py_cmd(repo, SCRIPT, "--json"), repo)
assert py.returncode == bash.returncode == 1
assert py.stdout == bash.stdout == ""
assert py.stderr == bash.stderr
@requires_bash
def test_python_unknown_option_matches_bash(repo: Path) -> None:
bash = run(bash_cmd(repo, SCRIPT, "--bogus"), repo)
py = run(py_cmd(repo, SCRIPT, "--bogus"), repo)
assert py.returncode == bash.returncode == 1
assert py.stdout == bash.stdout == ""
assert py.stderr == bash.stderr
@requires_bash
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_powershell_unknown_option_matches_siblings(repo: Path) -> None:
bash = run(bash_cmd(repo, SCRIPT, "--bogus"), repo)
ps = run(ps_cmd(repo, SCRIPT, "--bogus"), repo)
py = run(py_cmd(repo, SCRIPT, "--bogus"), repo)
assert ps.returncode == bash.returncode == py.returncode == 1
assert ps.stdout == bash.stdout == py.stdout == ""
assert ps.stderr == bash.stderr == py.stderr
@requires_bash
def test_help_beats_unknown_option_matches_bash(repo: Path) -> None:
"""--help must win over a later unknown option and exit 0."""
bash = run(bash_cmd(repo, SCRIPT, "--help", "--bogus"), repo)
py = run(py_cmd(repo, SCRIPT, "--help", "--bogus"), repo)
assert py.returncode == bash.returncode == 0
assert py.stderr == bash.stderr == ""
assert "Usage" in py.stdout and "Usage" in bash.stdout
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_powershell_help_beats_unknown_option(repo: Path) -> None:
"""-Help must win over unknown-argument validation like the siblings."""
ps = run(ps_cmd(repo, SCRIPT, "-Help", "--bogus"), repo)
assert ps.returncode == 0, ps.stderr
assert ps.stderr == ""
assert "Usage" in ps.stdout
@requires_bash
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
@pytest.mark.parametrize(
"context", ["missing", "invalid_json", "invalid_utf8", "invalid_init_dir"]
)
def test_all_variants_feature_context_error_matches(
tmp_path: Path, context: str
) -> None:
repo = make_repo(tmp_path)
install_scripts(repo, SCRIPT)
env = None
if context == "invalid_json":
(repo / ".specify" / "feature.json").write_text(
"{not json", encoding="utf-8"
)
elif context == "invalid_utf8":
(repo / ".specify" / "feature.json").write_bytes(b"\xff")
elif context == "invalid_init_dir":
env = clean_env()
env["SPECIFY_INIT_DIR"] = str(tmp_path / "missing")
bash = run(bash_cmd(repo, SCRIPT, "--json"), repo, env)
ps = run(ps_cmd(repo, SCRIPT, "-Json"), repo, env)
py = run(py_cmd(repo, SCRIPT, "--json"), repo, env)
assert bash.returncode == ps.returncode == py.returncode == 1
assert bash.stdout == ps.stdout == py.stdout == ""
assert bash.stderr == ps.stderr == py.stderr
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_python_json_output_matches_powershell(repo: Path) -> None:
feature = repo / "specs" / "001-my-feature"
(feature / "research.md").write_text("# research\n", encoding="utf-8")
(feature / "contracts" / "v1").mkdir(parents=True)
ps = run(ps_cmd(repo, SCRIPT, "-Json"), repo)
py = run(py_cmd(repo, SCRIPT, "--json"), repo)
assert py.returncode == ps.returncode == 0
assert json_stdout(py) == json_stdout(ps)
@requires_bash
@pytest.mark.skipif(not HAS_POWERSHELL, reason="no PowerShell available")
def test_missing_template_error_matches_all_variants(repo: Path) -> None:
(repo / ".specify" / "templates" / "tasks-template.md").unlink()
bash = run(bash_cmd(repo, SCRIPT, "--json"), repo)
ps = run(ps_cmd(repo, SCRIPT, "-Json"), repo)
py = run(py_cmd(repo, SCRIPT, "--json"), repo)
assert bash.returncode == ps.returncode == py.returncode == 1
assert bash.stdout == ps.stdout == py.stdout == ""
assert bash.stderr == ps.stderr == py.stderr