113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
"""Tests for the full scaffold flow."""
|
|
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from new_python.render import DEFAULT_PYTHON
|
|
from new_python.scaffold import INITIAL_COMMIT_MESSAGE, ScaffoldError, scaffold
|
|
|
|
|
|
def _stub_cmd(monkeypatch, tmp_path: Path, name: str, exit_code: int = 0) -> Path:
|
|
"""Put a recording stub *name* on PATH; each call appends args then a separator."""
|
|
bin_dir = tmp_path / "bin"
|
|
bin_dir.mkdir(exist_ok=True)
|
|
record = tmp_path / f"{name}-calls.txt"
|
|
stub = bin_dir / name
|
|
stub.write_text(
|
|
"#!/bin/sh\n"
|
|
f'printf \'%s\\n\' "$@" >> "{record}"\n'
|
|
f'echo "----" >> "{record}"\n'
|
|
f"exit {exit_code}\n"
|
|
)
|
|
stub.chmod(0o755)
|
|
monkeypatch.setenv("PATH", f"{bin_dir}:{os.environ['PATH']}")
|
|
return record
|
|
|
|
|
|
def _calls(record: Path) -> list[list[str]]:
|
|
"""Return the recorded invocations as lists of arguments."""
|
|
return [c.splitlines() for c in record.read_text().split("----\n") if c.strip()]
|
|
|
|
|
|
def test_scaffold_creates_the_full_project(monkeypatch, tmp_path: Path):
|
|
"""Scaffold writes the template, inits git, pins pipenv, commits, installs hooks."""
|
|
pyenv = _stub_cmd(monkeypatch, tmp_path, "pyenv")
|
|
pipenv = _stub_cmd(monkeypatch, tmp_path, "pipenv")
|
|
just = _stub_cmd(monkeypatch, tmp_path, "just")
|
|
parent = tmp_path / "projects"
|
|
parent.mkdir()
|
|
|
|
target = scaffold("demo-project", parent=parent)
|
|
|
|
assert target == parent / "demo-project"
|
|
assert (target / "AGENTS.md").read_text().startswith("# demo-project\n")
|
|
assert (target / "demo_project" / "__init__.py").exists()
|
|
assert (target / ".git").is_dir()
|
|
assert (target / "hooks" / "pre-commit").stat().st_mode & 0o111
|
|
|
|
subject = subprocess.run(
|
|
["git", "-C", str(target), "log", "--format=%s"],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
)
|
|
assert subject.stdout.strip() == INITIAL_COMMIT_MESSAGE
|
|
status = subprocess.run(
|
|
["git", "-C", str(target), "status", "--porcelain"],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
)
|
|
assert status.stdout == ""
|
|
|
|
assert _calls(pyenv) == [["prefix", DEFAULT_PYTHON]]
|
|
assert _calls(pipenv) == [["install", "--dev", "--python", DEFAULT_PYTHON]]
|
|
assert _calls(just) == [["install-hooks"]]
|
|
|
|
|
|
def test_scaffold_passes_packages_to_a_second_pipenv_call(monkeypatch, tmp_path: Path):
|
|
"""Runtime packages reach pipenv after the dev environment install."""
|
|
_stub_cmd(monkeypatch, tmp_path, "pyenv")
|
|
pipenv = _stub_cmd(monkeypatch, tmp_path, "pipenv")
|
|
_stub_cmd(monkeypatch, tmp_path, "just")
|
|
parent = tmp_path / "projects"
|
|
parent.mkdir()
|
|
|
|
scaffold("demo-project", packages=["mutagen"], parent=parent)
|
|
|
|
assert _calls(pipenv) == [
|
|
["install", "--dev", "--python", DEFAULT_PYTHON],
|
|
["install", "mutagen"],
|
|
]
|
|
|
|
|
|
def test_scaffold_refuses_missing_python_before_creating_anything(monkeypatch, tmp_path: Path):
|
|
"""An unavailable python pin refuses loudly and creates nothing."""
|
|
_stub_cmd(monkeypatch, tmp_path, "pyenv", exit_code=1)
|
|
_stub_cmd(monkeypatch, tmp_path, "pipenv")
|
|
_stub_cmd(monkeypatch, tmp_path, "just")
|
|
parent = tmp_path / "projects"
|
|
parent.mkdir()
|
|
|
|
with pytest.raises(ScaffoldError) as exc:
|
|
scaffold("demo-project", parent=parent)
|
|
|
|
assert f"pyenv install {DEFAULT_PYTHON}" in str(exc.value)
|
|
assert not (parent / "demo-project").exists()
|
|
|
|
|
|
def test_scaffold_refuses_invalid_names_first(monkeypatch, tmp_path: Path):
|
|
"""An invalid name refuses before any other work happens."""
|
|
pyenv = _stub_cmd(monkeypatch, tmp_path, "pyenv")
|
|
parent = tmp_path / "projects"
|
|
parent.mkdir()
|
|
|
|
with pytest.raises(ScaffoldError):
|
|
scaffold("Demo-Project", parent=parent)
|
|
|
|
assert not pyenv.exists()
|
|
assert not (parent / "Demo-Project").exists()
|