Files
fiona-assistant/tests/test_bootstrap_install.py
T
nvaldez dfea2bccfa feat: refuse to run bootstrap when not sourced
Bash bootstrap now exits non-zero when executed instead of sourced
(and when run under a non-bash shell), pointing the user at the
source command, per SPEC 7.21.7.

Also clears every ruff and pycustomlint violation so
run_sanity.py passes: five ruff findings in diff_report.py and
the two inline diff_report imports moved to module top in
__main__.py and git_tool.py.
2026-10-21 18:01:46 +02:00

223 lines
7.6 KiB
Python

"""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 shutil
import subprocess
import pytest
# CURSOR: Linux CI images (GitLab python:) have no PowerShell.
POWERSHELL = shutil.which("powershell") or shutil.which("pwsh")
requires_powershell = pytest.mark.skipif(
not POWERSHELL,
reason="PowerShell not installed (Windows-only bootstrap.ps1 tests)",
)
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."""
assert POWERSHELL is not None
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")
@requires_powershell
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
@requires_powershell
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
class TestBashSourcedGuard:
"""SPEC 7.21.7: executed bootstrap refuses to run."""
def _sandbox_env(self, tmp_path: pathlib.Path) -> dict[str, str]:
"""An environment with no python, pyenv, sudo, or curl."""
empty_bin = tmp_path / "bin"
home = tmp_path / "home"
empty_bin.mkdir()
home.mkdir()
return {"PATH": str(empty_bin), "HOME": str(home)}
def test_executed_bootstrap_exits_with_source_hint(self, tmp_path):
"""Running 'bash bootstrap' exits non-zero and says to source it."""
result = subprocess.run(
["/bin/bash", str(ROOT / "bootstrap")],
capture_output=True,
text=True,
timeout=60,
check=False,
env=self._sandbox_env(tmp_path),
)
combined = result.stdout + result.stderr
assert result.returncode != 0
assert "must be sourced" in combined
assert "source bootstrap" in combined
def test_sourced_bootstrap_is_not_blocked(self, tmp_path):
"""Sourcing still reaches argument parsing; no false positive."""
result = subprocess.run(
[
"/bin/bash",
"-c",
f'source "{ROOT / "bootstrap"}" --not-a-flag',
],
capture_output=True,
text=True,
timeout=60,
check=False,
env=self._sandbox_env(tmp_path),
)
combined = result.stdout + result.stderr
assert "Unknown option: --not-a-flag" in combined
assert "must be sourced" not in combined
def test_sh_run_reports_bash_requirement(self, tmp_path):
"""Running with sh/dash exits non-zero with a bash hint."""
result = subprocess.run(
["/bin/sh", str(ROOT / "bootstrap")],
capture_output=True,
text=True,
timeout=60,
check=False,
env=self._sandbox_env(tmp_path),
)
combined = result.stdout + result.stderr
assert result.returncode != 0
assert "bash" in combined