Bring 14 files that predated the ruff-format run into line with the configured formatter (line-length 100); provably formatting-only (ruff format of HEAD == working tree, and ruff format is semantics-preserving). Also refresh two lim.image.tor._KEYGEN_SCRIPT doc references in tor_harness.py to lim.image.initramfs.keygen after the module moved. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
import pytest
|
|
|
|
from lim import cli, system
|
|
from lim.cli import Command
|
|
from lim.errors import LimError
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def no_input(monkeypatch):
|
|
monkeypatch.setattr(
|
|
"builtins.input", lambda *args: (_ for _ in ()).throw(AssertionError("unexpected prompt"))
|
|
)
|
|
|
|
|
|
def test_every_command_dispatches_to_its_function(monkeypatch):
|
|
executed = []
|
|
monkeypatch.setitem(
|
|
cli.COMMANDS, "lock", Command(lambda: executed.append("lock"), "d", needs_root=False)
|
|
)
|
|
cli.main(["--type", "lock", "--auto-confirm"])
|
|
assert executed == ["lock"]
|
|
|
|
|
|
def test_needs_root_command_requests_root(monkeypatch):
|
|
escalated = []
|
|
monkeypatch.setattr(system, "ensure_root", lambda: escalated.append(True))
|
|
monkeypatch.setitem(cli.COMMANDS, "backup", Command(lambda: None, "d", needs_root=True))
|
|
cli.main(["--type", "backup", "--auto-confirm"])
|
|
assert escalated == [True]
|
|
|
|
|
|
def test_confirmation_prompt_waits_for_enter(monkeypatch):
|
|
prompts = []
|
|
executed = []
|
|
monkeypatch.setattr("builtins.input", lambda *args: prompts.append(args) or "")
|
|
monkeypatch.setitem(
|
|
cli.COMMANDS, "lock", Command(lambda: executed.append(True), "d", needs_root=False)
|
|
)
|
|
cli.main(["--type", "lock"])
|
|
assert len(prompts) == 1
|
|
assert executed == [True]
|
|
|
|
|
|
def test_lim_error_exits_with_code_1(monkeypatch):
|
|
def failing():
|
|
raise LimError("boom")
|
|
|
|
monkeypatch.setitem(cli.COMMANDS, "lock", Command(failing, "d", needs_root=False))
|
|
with pytest.raises(SystemExit) as excinfo:
|
|
cli.main(["--type", "lock", "--auto-confirm"])
|
|
assert excinfo.value.code == 1
|
|
|
|
|
|
def test_unknown_type_is_rejected_by_argparse():
|
|
with pytest.raises(SystemExit) as excinfo:
|
|
cli.main(["--type", "does-not-exist"])
|
|
assert excinfo.value.code == 2
|
|
|
|
|
|
def test_all_registered_commands_have_descriptions():
|
|
for name, command in cli.COMMANDS.items():
|
|
assert command.description, name
|
|
assert callable(command.func), name
|