Add CLI and bin entrypoint: quiet runs, refusals on stderr
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""Shared test fixtures."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class StubRecords(NamedTuple):
|
||||
"""Record paths for the pyenv, pipenv, and just stubs."""
|
||||
|
||||
pyenv: Path
|
||||
pipenv: Path
|
||||
just: Path
|
||||
|
||||
|
||||
def _make_stub(monkeypatch, tmp_path: Path, name: str, exit_code: int = 0) -> Path:
|
||||
"""Create a recording stub *name* on PATH; return its call-record path."""
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stub_cmd(monkeypatch, tmp_path: Path):
|
||||
"""Return a factory that puts a recording *name* stub on PATH."""
|
||||
|
||||
def make(name: str, exit_code: int = 0) -> Path:
|
||||
"""Create the stub and return its call-record path."""
|
||||
return _make_stub(monkeypatch, tmp_path, name, exit_code)
|
||||
|
||||
return make
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls():
|
||||
"""Return a parser turning a stub record into a list of argument lists."""
|
||||
|
||||
def parse(record: Path) -> list[list[str]]:
|
||||
"""Split the record into invocations."""
|
||||
return [c.splitlines() for c in record.read_text().split("----\n") if c.strip()]
|
||||
|
||||
return parse
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def stubs(monkeypatch, tmp_path: Path) -> StubRecords:
|
||||
"""Recording stubs for pyenv, pipenv, and just on PATH."""
|
||||
return StubRecords(
|
||||
_make_stub(monkeypatch, tmp_path, "pyenv"),
|
||||
_make_stub(monkeypatch, tmp_path, "pipenv"),
|
||||
_make_stub(monkeypatch, tmp_path, "just"),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def parent(tmp_path: Path) -> Path:
|
||||
"""A created parent directory for new projects."""
|
||||
projects = tmp_path / "projects"
|
||||
projects.mkdir()
|
||||
return projects
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Tests for the command-line interface."""
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from new_python.cli import main
|
||||
from new_python.render import DEFAULT_PYTHON, TEMPLATE_DIR
|
||||
|
||||
BIN = TEMPLATE_DIR.parent / "bin" / "new-python"
|
||||
|
||||
|
||||
def test_main_scaffolds_and_prints_the_target(stubs, calls, parent, capsys):
|
||||
"""A valid invocation scaffolds the project and prints its path."""
|
||||
exit_code = main(["demo-project", "--dir", str(parent)])
|
||||
|
||||
assert exit_code == 0
|
||||
assert capsys.readouterr().out.strip() == str(parent / "demo-project")
|
||||
assert (parent / "demo-project" / "AGENTS.md").exists()
|
||||
assert calls(stubs.pipenv) == [["install", "--dev", "--python", DEFAULT_PYTHON]]
|
||||
|
||||
|
||||
def test_main_maps_scaffold_error_to_stderr_and_exit_one(stub_cmd, parent, capsys):
|
||||
"""A refusal prints the message to stderr and exits 1, creating nothing."""
|
||||
stub_cmd("pyenv", exit_code=1)
|
||||
stub_cmd("pipenv")
|
||||
stub_cmd("just")
|
||||
|
||||
exit_code = main(["demo-project", "--dir", str(parent)])
|
||||
|
||||
assert exit_code == 1
|
||||
err = capsys.readouterr().err
|
||||
assert "new-python:" in err
|
||||
assert "pyenv install" in err
|
||||
assert not (parent / "demo-project").exists()
|
||||
|
||||
|
||||
def test_main_passes_packages_and_python_pin(stubs, calls, parent):
|
||||
"""--packages and --python reach pipenv and the pyenv check."""
|
||||
exit_code = main(
|
||||
[
|
||||
"demo-project",
|
||||
"--dir",
|
||||
str(parent),
|
||||
"--packages",
|
||||
"mutagen",
|
||||
"requests",
|
||||
"--python",
|
||||
"3.13.1",
|
||||
]
|
||||
)
|
||||
|
||||
assert exit_code == 0
|
||||
assert calls(stubs.pyenv) == [["prefix", "3.13.1"]]
|
||||
assert calls(stubs.pipenv) == [
|
||||
["install", "--dev", "--python", "3.13.1"],
|
||||
["install", "mutagen", "requests"],
|
||||
]
|
||||
|
||||
|
||||
def test_bin_entrypoint_runs_through_a_symlink(stubs, calls, parent, tmp_path: Path):
|
||||
"""bin/new-python works when invoked via a symlink, never via its own location."""
|
||||
link_dir = tmp_path / "links"
|
||||
link_dir.mkdir()
|
||||
link = link_dir / "new-python"
|
||||
link.symlink_to(BIN)
|
||||
|
||||
result = subprocess.run(
|
||||
[str(link), "demo-project", "--dir", str(parent)],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
assert result.stdout.strip() == str(parent / "demo-project")
|
||||
assert (parent / "demo-project" / "justfile").exists()
|
||||
assert calls(stubs.just) == [["install-hooks"]]
|
||||
+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)
|
||||
|
||||
+7
-20
@@ -1,6 +1,5 @@
|
||||
"""Tests for installing runtime packages via pipenv."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -8,32 +7,20 @@ import pytest
|
||||
from new_python.scaffold import ScaffoldError, install_packages
|
||||
|
||||
|
||||
def _pipenv_stub(monkeypatch, tmp_path: Path, exit_code: int) -> Path:
|
||||
"""Put a fake pipenv on PATH that records args and exits *exit_code*."""
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir()
|
||||
record = tmp_path / "pipenv-args.txt"
|
||||
stub = bin_dir / "pipenv"
|
||||
stub.write_text(f'#!/bin/sh\nprintf "%s\\n" "$@" > "{record}"\nexit {exit_code}\n')
|
||||
stub.chmod(0o755)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}:{os.environ['PATH']}")
|
||||
return record
|
||||
|
||||
|
||||
def test_install_packages_invokes_pipenv_install(monkeypatch, tmp_path: Path):
|
||||
def test_install_packages_invokes_pipenv_install(stub_cmd, calls, tmp_path: Path):
|
||||
"""Package values reach one pipenv call with the exact install arguments."""
|
||||
record = _pipenv_stub(monkeypatch, tmp_path, exit_code=0)
|
||||
record = stub_cmd("pipenv")
|
||||
project = tmp_path / "demo-project"
|
||||
project.mkdir()
|
||||
|
||||
install_packages(["mutagen", "requests"], project)
|
||||
|
||||
assert record.read_text().splitlines() == ["install", "mutagen", "requests"]
|
||||
assert calls(record) == [["install", "mutagen", "requests"]]
|
||||
|
||||
|
||||
def test_install_packages_skips_invocation_when_empty(monkeypatch, tmp_path: Path):
|
||||
def test_install_packages_skips_invocation_when_empty(stub_cmd, tmp_path: Path):
|
||||
"""An empty package list never invokes pipenv."""
|
||||
record = _pipenv_stub(monkeypatch, tmp_path, exit_code=0)
|
||||
record = stub_cmd("pipenv")
|
||||
project = tmp_path / "demo-project"
|
||||
project.mkdir()
|
||||
|
||||
@@ -42,9 +29,9 @@ def test_install_packages_skips_invocation_when_empty(monkeypatch, tmp_path: Pat
|
||||
assert not record.exists()
|
||||
|
||||
|
||||
def test_install_packages_wraps_pipenv_failure(monkeypatch, tmp_path: Path):
|
||||
def test_install_packages_wraps_pipenv_failure(stub_cmd, tmp_path: Path):
|
||||
"""A failing pipenv refuses as ScaffoldError naming the command."""
|
||||
_pipenv_stub(monkeypatch, tmp_path, exit_code=1)
|
||||
stub_cmd("pipenv", exit_code=1)
|
||||
project = tmp_path / "demo-project"
|
||||
project.mkdir()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user