"""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"]]