cdc209536f
- new script python/src/init/init.py: locates the register-python-argcomplete that belongs to the running interpreter (venv bin, NOT the resolve()d symlink target - proven by a live pipx run), appends the managed hook block to ~/.bashrc between # BEGIN/END daedalus init markers, idempotent; --bashrc, --dry-run; Windows notice; zsh warning - per-script spec python/src/init/spec.md (SPEC 6.11/6.12), tests python/tests/test_init_script.py (13; the plain test_init.py name is taken by daedalus/tests) - registry entry, README row + section, pyproject packaging (daedalus.init), CHANGELOG 0.10.0, version pins Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
168 lines
6.3 KiB
Python
168 lines
6.3 KiB
Python
"""Tests for the init script: argcomplete registration (init spec).
|
|
|
|
No network, no hardware: the register-script path is injectable and
|
|
the bashrc target a temp file (daedalus agent rule 5.6). File is
|
|
named test_init_script.py because daedalus/tests/test_init.py exists
|
|
and pytest needs unique basenames (init spec item 6.1).
|
|
"""
|
|
import pathlib
|
|
|
|
from daedalus.init import init
|
|
|
|
REGISTER = pathlib.Path("/venv/bin/register-python-argcomplete")
|
|
|
|
|
|
# --- block building (init spec item 3.2)
|
|
|
|
|
|
def test_build_block_contains_hook():
|
|
"""The block holds the markers and the eval line with the path."""
|
|
block = init.build_block(REGISTER)
|
|
assert block.startswith("# BEGIN daedalus init: argcomplete\n")
|
|
assert block.endswith("# END daedalus init: argcomplete\n")
|
|
assert f"eval \"$('{REGISTER}' daedalus)\"" in block
|
|
|
|
|
|
# --- register-script resolution (init spec item 3.1)
|
|
|
|
|
|
def test_register_script_path_beside_python(monkeypatch, tmp_path):
|
|
"""The interpreter's folder wins (pipx venv, .venv, system)."""
|
|
fake = tmp_path / "bin" / "register-python-argcomplete"
|
|
fake.parent.mkdir(parents=True)
|
|
fake.write_text("#!/bin/sh\n", encoding="utf-8")
|
|
monkeypatch.setattr(init.sys, "executable",
|
|
str(tmp_path / "bin" / "python"))
|
|
assert init.register_script_path() == fake
|
|
|
|
|
|
def test_register_script_path_prefers_venv_symlink(monkeypatch, tmp_path):
|
|
"""A venv python is a symlink; its own bin wins, not the target's.
|
|
|
|
resolve() on sys.executable would dereference the venv symlink
|
|
and hand the lookup to the system python's folder (init spec
|
|
item 3.1: the *running interpreter's* folder).
|
|
"""
|
|
real = tmp_path / "real" / "bin"
|
|
real.mkdir(parents=True)
|
|
(real / "python3").write_text("#!/bin/sh\n", encoding="utf-8")
|
|
(real / "register-python-argcomplete").write_text(
|
|
"#!/bin/sh\n", encoding="utf-8")
|
|
venv = tmp_path / "venv" / "bin"
|
|
venv.mkdir(parents=True)
|
|
(venv / "python").symlink_to(real / "python3")
|
|
(venv / "register-python-argcomplete").write_text(
|
|
"#!/bin/sh\n", encoding="utf-8")
|
|
monkeypatch.setattr(init.sys, "executable", str(venv / "python"))
|
|
assert init.register_script_path() == \
|
|
venv / "register-python-argcomplete"
|
|
|
|
|
|
def test_register_script_path_falls_back_to_path(monkeypatch):
|
|
"""Without a match beside python, PATH is searched."""
|
|
monkeypatch.setattr(init.sys, "executable", "/nowhere/python")
|
|
monkeypatch.setattr(init.shutil, "which",
|
|
lambda _name: "/usr/local/bin/register-python-argcomplete")
|
|
assert init.register_script_path() == \
|
|
pathlib.Path("/usr/local/bin/register-python-argcomplete")
|
|
|
|
|
|
def test_register_script_path_missing(monkeypatch):
|
|
"""Neither location has it: None (exit 3 upstream)."""
|
|
monkeypatch.setattr(init.sys, "executable", "/nowhere/python")
|
|
monkeypatch.setattr(init.shutil, "which", lambda _name: None)
|
|
assert init.register_script_path() is None
|
|
|
|
|
|
# --- main flow (init spec items 3.3-3.8)
|
|
|
|
|
|
def fake_register(monkeypatch, path):
|
|
"""Point register_script_path at a fixed path."""
|
|
monkeypatch.setattr(init, "register_script_path",
|
|
lambda: pathlib.Path(path))
|
|
|
|
|
|
def test_main_appends_block(tmp_path, monkeypatch, capsys):
|
|
"""A fresh bashrc receives the block, exit 0."""
|
|
target = tmp_path / "bashrc"
|
|
fake_register(monkeypatch, REGISTER)
|
|
assert init.main(["--bashrc", str(target)]) == 0
|
|
content = target.read_text(encoding="utf-8")
|
|
assert "# BEGIN daedalus init: argcomplete" in content
|
|
assert f"'{REGISTER}' daedalus" in content
|
|
out = capsys.readouterr().out
|
|
assert "registered" in out
|
|
assert "exec bash" in out
|
|
|
|
|
|
def test_main_preserves_existing_bashrc(tmp_path, monkeypatch):
|
|
"""Existing lines stay; the block lands after a blank line."""
|
|
target = tmp_path / "bashrc"
|
|
target.write_text("alias ll='ls -l'\n", encoding="utf-8")
|
|
fake_register(monkeypatch, REGISTER)
|
|
assert init.main(["--bashrc", str(target)]) == 0
|
|
content = target.read_text(encoding="utf-8")
|
|
assert content.startswith("alias ll='ls -l'\n\n")
|
|
assert content.endswith("# END daedalus init: argcomplete\n")
|
|
|
|
|
|
def test_main_idempotent(tmp_path, monkeypatch, capsys):
|
|
"""A second run reports already enabled and writes nothing."""
|
|
target = tmp_path / "bashrc"
|
|
fake_register(monkeypatch, REGISTER)
|
|
init.main(["--bashrc", str(target)])
|
|
before = target.read_text(encoding="utf-8")
|
|
assert init.main(["--bashrc", str(target)]) == 0
|
|
assert target.read_text(encoding="utf-8") == before
|
|
assert "already enabled" in capsys.readouterr().out
|
|
|
|
|
|
def test_main_dry_run(tmp_path, monkeypatch, capsys):
|
|
"""Dry run prints the block, creates and appends nothing."""
|
|
target = tmp_path / "bashrc"
|
|
fake_register(monkeypatch, REGISTER)
|
|
assert init.main(["--bashrc", str(target), "--dry-run"]) == 0
|
|
out = capsys.readouterr().out
|
|
assert "# BEGIN daedalus init: argcomplete" in out
|
|
assert "dry run" in out
|
|
assert not target.exists()
|
|
|
|
|
|
def test_main_missing_register(tmp_path, monkeypatch):
|
|
"""No register script anywhere is exit 3, no file written."""
|
|
target = tmp_path / "bashrc"
|
|
monkeypatch.setattr(init, "register_script_path", lambda: None)
|
|
assert init.main(["--bashrc", str(target)]) == 3
|
|
assert not target.exists()
|
|
|
|
|
|
def test_main_non_posix(tmp_path, monkeypatch, capsys):
|
|
"""On Windows nothing applies: notice, exit 0, no write."""
|
|
target = tmp_path / "bashrc"
|
|
fake_register(monkeypatch, REGISTER)
|
|
monkeypatch.setattr(init.os, "name", "nt")
|
|
assert init.main(["--bashrc", str(target)]) == 0
|
|
assert "bash/zsh" in capsys.readouterr().out
|
|
assert not target.exists()
|
|
|
|
|
|
def test_main_zsh_shell_gets_a_note(tmp_path, monkeypatch, caplog):
|
|
"""A zsh SHELL without --bashrc logs the bashcompinit warning."""
|
|
target = tmp_path / "bashrc"
|
|
fake_register(monkeypatch, REGISTER)
|
|
monkeypatch.setenv("SHELL", "/usr/bin/zsh")
|
|
monkeypatch.setattr(init.pathlib.Path, "home",
|
|
staticmethod(lambda: tmp_path))
|
|
assert init.main([]) == 0
|
|
assert "bashcompinit" in caplog.text
|
|
assert (tmp_path / ".bashrc").exists()
|
|
|
|
|
|
def test_cli_help():
|
|
"""--help prints usage."""
|
|
import pytest
|
|
with pytest.raises(SystemExit) as exc:
|
|
init.main(["--help"])
|
|
assert exc.value.code == 0
|