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