Add CLI and bin entrypoint: quiet runs, refusals on stderr
This commit is contained in:
+12
-52
@@ -1,8 +1,6 @@
|
||||
"""Tests for the full scaffold flow."""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -10,36 +8,8 @@ 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):
|
||||
def test_scaffold_creates_the_full_project(stubs, calls, parent):
|
||||
"""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"
|
||||
@@ -63,34 +33,26 @@ def test_scaffold_creates_the_full_project(monkeypatch, tmp_path: Path):
|
||||
)
|
||||
assert status.stdout == ""
|
||||
|
||||
assert _calls(pyenv) == [["prefix", DEFAULT_PYTHON]]
|
||||
assert _calls(pipenv) == [["install", "--dev", "--python", DEFAULT_PYTHON]]
|
||||
assert _calls(just) == [["install-hooks"]]
|
||||
assert calls(stubs.pyenv) == [["prefix", DEFAULT_PYTHON]]
|
||||
assert calls(stubs.pipenv) == [["install", "--dev", "--python", DEFAULT_PYTHON]]
|
||||
assert calls(stubs.just) == [["install-hooks"]]
|
||||
|
||||
|
||||
def test_scaffold_passes_packages_to_a_second_pipenv_call(monkeypatch, tmp_path: Path):
|
||||
def test_scaffold_passes_packages_to_a_second_pipenv_call(stubs, calls, parent):
|
||||
"""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) == [
|
||||
assert calls(stubs.pipenv) == [
|
||||
["install", "--dev", "--python", DEFAULT_PYTHON],
|
||||
["install", "mutagen"],
|
||||
]
|
||||
|
||||
|
||||
def test_scaffold_refuses_missing_python_before_creating_anything(monkeypatch, tmp_path: Path):
|
||||
def test_scaffold_refuses_missing_python_before_creating_anything(stub_cmd, parent):
|
||||
"""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()
|
||||
stub_cmd("pyenv", exit_code=1)
|
||||
stub_cmd("pipenv")
|
||||
stub_cmd("just")
|
||||
|
||||
with pytest.raises(ScaffoldError) as exc:
|
||||
scaffold("demo-project", parent=parent)
|
||||
@@ -99,11 +61,9 @@ def test_scaffold_refuses_missing_python_before_creating_anything(monkeypatch, t
|
||||
assert not (parent / "demo-project").exists()
|
||||
|
||||
|
||||
def test_scaffold_refuses_invalid_names_first(monkeypatch, tmp_path: Path):
|
||||
def test_scaffold_refuses_invalid_names_first(stub_cmd, parent):
|
||||
"""An invalid name refuses before any other work happens."""
|
||||
pyenv = _stub_cmd(monkeypatch, tmp_path, "pyenv")
|
||||
parent = tmp_path / "projects"
|
||||
parent.mkdir()
|
||||
pyenv = stub_cmd("pyenv")
|
||||
|
||||
with pytest.raises(ScaffoldError):
|
||||
scaffold("Demo-Project", parent=parent)
|
||||
|
||||
Reference in New Issue
Block a user