"""Tests for bootstrap install modes (SPEC 7.21). Generated by AI/LLM: 2026-09-30 - local and no-env install options. """ import importlib.util import pathlib import subprocess import pytest ROOT = pathlib.Path(__file__).resolve().parents[1] def _load_bootstrap_install(): """Load scripts/bootstrap_install.py without installing the package.""" path = ROOT / "scripts" / "bootstrap_install.py" spec = importlib.util.spec_from_file_location("bootstrap_install", path) if spec is None or spec.loader is None: raise RuntimeError(f"cannot load {path}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module @pytest.fixture(scope="module") def bootstrap_install(): """The pip-argument helper both bootstrap scripts call.""" return _load_bootstrap_install() def _script(name: str) -> str: """Return one bootstrap script as text.""" return (ROOT / name).read_text(encoding="utf-8") def _powershell(*args: str) -> subprocess.CompletedProcess[str]: """Run bootstrap.ps1 as a file, not dot-sourced.""" return subprocess.run( [ "powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(ROOT / "bootstrap.ps1"), *args, ], capture_output=True, text=True, timeout=60, check=False, ) class TestPipInstallArgs: """SPEC 7.21.1 through 7.21.5: pip argument matrix.""" def test_local_installs_without_editable_mode(self, bootstrap_install): """--local installs '.' and does not pass -e or the dev extra.""" assert bootstrap_install.pip_install_args(local=True, no_env=False) == [ "install", ".", ] def test_no_env_adds_user_and_keeps_editable_dev(self, bootstrap_install): """--no-env adds --user. Without --local the install stays editable.""" assert bootstrap_install.pip_install_args(local=False, no_env=True) == [ "install", "--user", "-e", ".[dev]", ] def test_local_and_no_env_combine(self, bootstrap_install): """--local --no-env installs '.' with --user and without -e.""" assert bootstrap_install.pip_install_args(local=True, no_env=True) == [ "install", "--user", ".", ] def test_default_is_editable_dev_extra(self, bootstrap_install): """Neither flag keeps pip install -e '.[dev]'.""" assert bootstrap_install.pip_install_args(local=False, no_env=False) == [ "install", "-e", ".[dev]", ] def test_break_system_packages_after_user(self, bootstrap_install): """Bash inserts --break-system-packages after --user.""" args = bootstrap_install.pip_install_args( local=True, no_env=True, break_system_packages=True, ) assert args == ["install", "--user", "--break-system-packages", "."] def test_break_system_packages_without_user(self, bootstrap_install): """Bash inserts --break-system-packages after install when no --user.""" args = bootstrap_install.pip_install_args( local=False, no_env=False, break_system_packages=True, ) assert args == [ "install", "--break-system-packages", "-e", ".[dev]", ] def test_main_prints_one_argument_per_line(self, bootstrap_install, capsys): """The shell reads one pip argument per stdout line.""" assert bootstrap_install.main(["--local", "--no-env"]) == 0 captured = capsys.readouterr() assert captured.out.splitlines() == ["install", "--user", "."] assert captured.err == "" class TestBootstrapScripts: """SPEC 7.21.2 and 7.21.6: both scripts honor the flags.""" def test_both_scripts_call_the_helper(self): """Bash and PowerShell pass --local and --no-env to the helper.""" for name in ("bootstrap", "bootstrap.ps1"): text = _script(name) assert "--local" in text assert "--no-env" in text assert "bootstrap_install.py" in text def test_powershell_skips_venv_when_no_env(self): """PowerShell does not create a virtual environment when $NoEnv is set.""" assert "if (-not $NoEnv)" in _script("bootstrap.ps1") def test_bash_skips_venv_when_no_env(self): """Bash does not create a virtual environment when NO_ENV is true.""" assert '[ "$NO_ENV" = true ]' in _script("bootstrap") def test_powershell_local_without_no_env_must_be_dot_sourced(self): """--local alone is a venv install and must be dot-sourced.""" result = _powershell("--local") combined = result.stdout + result.stderr assert "Dot-source" in combined assert "Unknown option" not in combined def test_powershell_rejects_unknown_option(self): """An unrecognized flag is still reported as unknown.""" result = _powershell("--not-a-flag") combined = result.stdout + result.stderr assert "Unknown option" in combined