From f005cc7d4cc98bfdb1f3ee9c2f75450db3079a2d Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 9 Nov 2026 13:39:53 -0500 Subject: [PATCH] Add package installation: pipenv install with wrapped failures --- new_python/scaffold.py | 12 ++++++++++ tests/test_install.py | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/test_install.py diff --git a/new_python/scaffold.py b/new_python/scaffold.py index 58642f8..b86a154 100644 --- a/new_python/scaffold.py +++ b/new_python/scaffold.py @@ -1,6 +1,8 @@ """Create new python projects from the versioned template.""" import re +import subprocess +from collections.abc import Sequence from pathlib import Path NAME_PATTERN = re.compile(r"^[a-z]+(-[a-z]+)*$") @@ -34,3 +36,13 @@ def parent_dir(explicit: Path | None) -> Path: if not explicit.is_dir(): raise ScaffoldError(f"--dir is not an existing directory: {explicit}") return explicit + + +def install_packages(packages: Sequence[str], cwd: Path) -> None: + """Install runtime *packages* into the project at *cwd* via pipenv.""" + if not packages: + return + try: + subprocess.run(["pipenv", "install", *packages], cwd=cwd, check=True) + except subprocess.CalledProcessError as exc: + raise ScaffoldError(f"pipenv install failed with exit code {exc.returncode}") from exc diff --git a/tests/test_install.py b/tests/test_install.py new file mode 100644 index 0000000..061b614 --- /dev/null +++ b/tests/test_install.py @@ -0,0 +1,54 @@ +"""Tests for installing runtime packages via pipenv.""" + +import os +from pathlib import Path + +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): + """Package values reach one pipenv call with the exact install arguments.""" + record = _pipenv_stub(monkeypatch, tmp_path, exit_code=0) + project = tmp_path / "demo-project" + project.mkdir() + + install_packages(["mutagen", "requests"], project) + + assert record.read_text().splitlines() == ["install", "mutagen", "requests"] + + +def test_install_packages_skips_invocation_when_empty(monkeypatch, tmp_path: Path): + """An empty package list never invokes pipenv.""" + record = _pipenv_stub(monkeypatch, tmp_path, exit_code=0) + project = tmp_path / "demo-project" + project.mkdir() + + install_packages([], project) + + assert not record.exists() + + +def test_install_packages_wraps_pipenv_failure(monkeypatch, tmp_path: Path): + """A failing pipenv refuses as ScaffoldError naming the command.""" + _pipenv_stub(monkeypatch, tmp_path, exit_code=1) + project = tmp_path / "demo-project" + project.mkdir() + + with pytest.raises(ScaffoldError) as exc: + install_packages(["mutagen"], project) + + assert "pipenv install" in str(exc.value)