b71a513c36
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
290 lines
12 KiB
Python
290 lines
12 KiB
Python
"""Tests for daedalus.cli and the launchers.
|
|
|
|
Options, listing format, categories, errors, dispatch, and end-to-end runs
|
|
(SPEC.md section 3).
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
from daedalus import LIBRARY_ROOT
|
|
from daedalus import REPO_ROOT
|
|
from daedalus import cli
|
|
from daedalus import registry
|
|
from daedalus import runners
|
|
|
|
FAKE_SCRIPTS = {
|
|
"alpha": registry.Script("python/src/alpha/alpha.py", "First tool"),
|
|
"beta_tool": registry.Script("bash/beta_tool.sh", "Second tool",
|
|
categories=("media", "linux")),
|
|
"gamma": registry.Script("powershell/gamma.ps1", "Third tool", categories="media"),
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_registry(monkeypatch):
|
|
"""Replace SCRIPTS and pin the version so list output is stable."""
|
|
monkeypatch.setattr(registry, "SCRIPTS", dict(FAKE_SCRIPTS))
|
|
monkeypatch.setattr(cli, "version", lambda: "1.2.3")
|
|
|
|
|
|
@pytest.fixture
|
|
def plain(monkeypatch):
|
|
"""Force colors off and a wide, deterministic terminal."""
|
|
monkeypatch.setenv("NO_COLOR", "1")
|
|
monkeypatch.delenv("FORCE_COLOR", raising=False)
|
|
monkeypatch.setenv("COLUMNS", "200")
|
|
|
|
|
|
def row(name: str, script_type: str, categories: str) -> str:
|
|
"""One table row: name 9 wide, type 4 wide, categories 12 wide (SPEC item 3.3.2)."""
|
|
return f"{name:<9} {script_type:<4} {categories:<12}"
|
|
|
|
|
|
@pytest.mark.parametrize("argv", [[], ["-l"], ["--list"]])
|
|
def test_list(fake_registry, plain, capsys, argv):
|
|
"""Listing prints the version line, a table, wrapped descriptions (SPEC item 3.3)."""
|
|
assert cli.main(argv) == 0
|
|
assert capsys.readouterr().out.splitlines() == [
|
|
"Daedalus Version: 1.2.3",
|
|
"",
|
|
row("script", "type", "categories"),
|
|
"--------- ---- ------------",
|
|
row("alpha", "py", "general"),
|
|
" First tool",
|
|
row("beta_tool", "sh", "media, linux"),
|
|
" Second tool",
|
|
row("gamma", "ps1", "media"),
|
|
" Third tool",
|
|
]
|
|
|
|
|
|
def test_list_when_empty(monkeypatch, plain, capsys):
|
|
"""With no scripts, only the version line is printed (SPEC item 3.3.1)."""
|
|
monkeypatch.setattr(registry, "SCRIPTS", {})
|
|
monkeypatch.setattr(cli, "version", lambda: "1.2.3")
|
|
assert cli.main([]) == 0
|
|
assert capsys.readouterr().out == "Daedalus Version: 1.2.3\n"
|
|
|
|
|
|
def test_list_wraps_descriptions(fake_registry, plain, monkeypatch, capsys):
|
|
"""Descriptions wrap to the terminal width and indent by 8 (SPEC item 3.3.4)."""
|
|
monkeypatch.setenv("COLUMNS", "60")
|
|
long_text = "The quick brown fox jumps over the lazy dog again and again " * 3
|
|
monkeypatch.setitem(registry.SCRIPTS, "alpha",
|
|
registry.Script("python/src/alpha/alpha.py", long_text.rstrip()))
|
|
assert cli.main(["--list"]) == 0
|
|
lines = capsys.readouterr().out.splitlines()
|
|
wrapped = [line for line in lines if line.startswith(" ")]
|
|
assert len(wrapped) > 1, "a 170-char description must wrap at 60 columns"
|
|
assert all(len(line) <= 8 + 60 for line in wrapped), lines
|
|
|
|
|
|
def test_list_min_width_is_40(fake_registry, plain, monkeypatch, capsys):
|
|
"""Narrow terminals still wrap to at least 40 columns (SPEC item 3.3.4)."""
|
|
monkeypatch.setenv("COLUMNS", "20")
|
|
assert cli.main(["--list"]) == 0
|
|
lines = capsys.readouterr().out.splitlines()
|
|
wrapped = [line for line in lines if line.startswith(" ")]
|
|
assert all(8 + 40 >= len(line) for line in wrapped), lines
|
|
|
|
|
|
def test_list_no_word_splitting(fake_registry, plain, monkeypatch, capsys):
|
|
"""Long and hyphenated words are never split (SPEC item 3.3.4)."""
|
|
monkeypatch.setenv("COLUMNS", "50")
|
|
text = "short filler words to fill the line " + "supercalifragilistic-expialidocious word"
|
|
monkeypatch.setitem(registry.SCRIPTS, "alpha",
|
|
registry.Script("python/src/alpha/alpha.py", text))
|
|
assert cli.main(["--list"]) == 0
|
|
lines = capsys.readouterr().out.splitlines()
|
|
assert any("supercalifragilistic-expialidocious" in line for line in lines), lines
|
|
fragments = [line for line in lines if "expialidoc" in line
|
|
and "supercalifragilistic" not in line]
|
|
assert fragments == [], lines
|
|
|
|
|
|
def test_list_colors(fake_registry, monkeypatch, capsys):
|
|
"""FORCE_COLOR colors the padded name, type, and categories cells (SPEC item 3.3.5)."""
|
|
monkeypatch.delenv("NO_COLOR", raising=False)
|
|
monkeypatch.setenv("FORCE_COLOR", "1")
|
|
assert cli.main(["--list"]) == 0
|
|
alpha_row = next(line for line in capsys.readouterr().out.splitlines()
|
|
if "alpha" in line and "py" in line)
|
|
assert alpha_row.startswith("\033[32malpha")
|
|
assert "\033[33mpy" in alpha_row
|
|
assert "\033[94mgeneral" in alpha_row
|
|
|
|
|
|
def test_list_no_color_wins(fake_registry, monkeypatch, capsys):
|
|
"""A non-empty NO_COLOR beats FORCE_COLOR (SPEC item 3.3.6)."""
|
|
monkeypatch.setenv("NO_COLOR", "1")
|
|
monkeypatch.setenv("FORCE_COLOR", "1")
|
|
assert cli.main(["--list"]) == 0
|
|
assert "\033[" not in capsys.readouterr().out
|
|
|
|
|
|
@pytest.mark.parametrize("argv", [["-h"], ["--help"]])
|
|
def test_help(fake_registry, plain, capsys, argv):
|
|
"""--help prints the full usage with category forms (SPEC item 3.8)."""
|
|
assert cli.main(argv) == 0
|
|
out = capsys.readouterr().out
|
|
assert "daedalus <name> [args...]" in out
|
|
assert "daedalus <category> [category...]" in out
|
|
|
|
|
|
def test_path(fake_registry, capsys):
|
|
"""--path prints the absolute script path."""
|
|
assert cli.main(["--path", "alpha"]) == 0
|
|
assert capsys.readouterr().out.strip() == str(LIBRARY_ROOT / "python/src/alpha/alpha.py")
|
|
|
|
|
|
@pytest.mark.parametrize("installed, expected", [(True, "daedalus 1.2.3"), (False, "daedalus 0.1.0")])
|
|
def test_version(monkeypatch, capsys, installed, expected):
|
|
"""--version uses metadata or pyproject.toml in a checkout."""
|
|
def fake_version(_name):
|
|
if not installed:
|
|
raise cli.importlib.metadata.PackageNotFoundError(_name)
|
|
return "1.2.3"
|
|
|
|
monkeypatch.setattr(cli.importlib.metadata, "version", fake_version)
|
|
assert cli.main(["--version"]) == 0
|
|
assert capsys.readouterr().out.strip() == expected
|
|
|
|
|
|
@pytest.mark.parametrize("argv, message", [
|
|
(["--path"], "usage: daedalus --path"),
|
|
(["--bogus"], "unknown option '--bogus'; run 'daedalus --help'"),
|
|
(["alpah"], "unknown script 'alpah'; did you mean: alpha"),
|
|
(["zzz"], "unknown script 'zzz'; run 'daedalus --list'"),
|
|
])
|
|
def test_usage_errors_exit_2(fake_registry, plain, capsys, argv, message):
|
|
"""Bad options and unknown names exit 2 with a message on stderr."""
|
|
assert cli.main(argv) == 2
|
|
assert message in capsys.readouterr().err
|
|
|
|
|
|
def test_dispatch_passes_args_through(fake_registry, plain, monkeypatch):
|
|
"""Everything after the name, including --help, goes to the script."""
|
|
calls = []
|
|
monkeypatch.setattr(runners, "run", lambda path, args: calls.append((path, args)) or 7)
|
|
assert cli.main(["alpha", "--help", "x"]) == 7
|
|
assert calls == [(LIBRARY_ROOT / "python/src/alpha/alpha.py", ["--help", "x"])]
|
|
|
|
|
|
def test_runner_error_exits_2(fake_registry, plain, capsys):
|
|
"""A missing script file exits 2 instead of a traceback."""
|
|
assert cli.main(["alpha"]) == 2
|
|
assert "script not found" in capsys.readouterr().err
|
|
|
|
|
|
def test_main_reads_sys_argv(fake_registry, plain, monkeypatch, capsys):
|
|
"""Without argv, main uses sys.argv."""
|
|
monkeypatch.setattr(sys, "argv", ["daedalus", "--list"])
|
|
assert cli.main() == 0
|
|
assert "alpha" in capsys.readouterr().out
|
|
|
|
|
|
# --- categories (SPEC items 3.10, 3.11) -------------------------------------
|
|
|
|
|
|
def test_category_lists_scripts(fake_registry, plain, capsys):
|
|
"""daedalus <category> lists scripts that have the category."""
|
|
assert cli.main(["media"]) == 0
|
|
assert capsys.readouterr().out.splitlines()[2:] == [
|
|
row("script", "type", "categories"),
|
|
"--------- ---- ------------",
|
|
row("beta_tool", "sh", "media, linux"),
|
|
" Second tool",
|
|
row("gamma", "ps1", "media"),
|
|
" Third tool",
|
|
]
|
|
|
|
|
|
def test_categories_are_anded(fake_registry, plain, capsys):
|
|
"""Several categories list scripts that have every one of them."""
|
|
assert cli.main(["media", "linux"]) == 0
|
|
out = capsys.readouterr().out
|
|
assert "beta_tool" in out
|
|
assert "alpha" not in out
|
|
assert "gamma" not in out
|
|
|
|
|
|
def test_categories_match_no_script(fake_registry, plain, capsys):
|
|
"""Disjoint categories exit 2 with the no-scripts message."""
|
|
assert cli.main(["media", "general"]) == 2
|
|
assert "no scripts for categories: media, general" in capsys.readouterr().err
|
|
|
|
|
|
def test_category_prefix_runs_script(fake_registry, plain, monkeypatch):
|
|
"""A category-prefixed run passes the remaining args to the script."""
|
|
calls = []
|
|
monkeypatch.setattr(runners, "run", lambda path, args: calls.append((path, args)) or 5)
|
|
assert cli.main(["media", "beta_tool", "x", "--help"]) == 5
|
|
assert calls == [(LIBRARY_ROOT / "bash/beta_tool.sh", ["x", "--help"])]
|
|
|
|
|
|
def test_script_name_stops_the_prefix(fake_registry, plain, monkeypatch):
|
|
"""A token that is a script name is never eaten as a category."""
|
|
calls = []
|
|
monkeypatch.setattr(runners, "run", lambda path, args: calls.append((path, args)) or 0)
|
|
assert cli.main(["alpha"]) == 0
|
|
assert calls == [(LIBRARY_ROOT / "python/src/alpha/alpha.py", [])]
|
|
|
|
|
|
def test_script_lacks_a_prefix_category(fake_registry, plain, capsys):
|
|
"""A registered script outside the prefix explains its categories (SPEC item 3.11)."""
|
|
assert cli.main(["media", "alpha"]) == 2
|
|
err = capsys.readouterr().err
|
|
assert "script 'alpha' has categories 'general', not [media]" in err
|
|
assert "run 'daedalus alpha' or pick another prefix" in err
|
|
|
|
|
|
def test_unknown_script_after_prefix_with_close_match(fake_registry, plain, capsys):
|
|
"""Close matches after a prefix come from the prefix's scripts only."""
|
|
assert cli.main(["media", "beta"]) == 2
|
|
assert "unknown script 'beta' for 'media'; did you mean: beta_tool" in capsys.readouterr().err
|
|
|
|
|
|
def test_unknown_script_after_prefix_without_close_match(fake_registry, plain, capsys):
|
|
"""No close match after a prefix suggests listing the categories."""
|
|
assert cli.main(["media", "zzz"]) == 2
|
|
assert "unknown script 'zzz' for 'media'; run 'daedalus media'" in capsys.readouterr().err
|
|
|
|
|
|
def test_close_matches_respect_the_prefix(fake_registry, plain, capsys):
|
|
"""Names of scripts outside the prefix are never suggested."""
|
|
assert cli.main(["media", "gama"]) == 2
|
|
assert "did you mean: gamma" in capsys.readouterr().err
|
|
|
|
|
|
def run_cli(*command: str) -> subprocess.CompletedProcess:
|
|
"""Run a daedalus command from a directory outside the repo."""
|
|
env = {**os.environ, "PYTHONPATH": str(REPO_ROOT)}
|
|
return subprocess.run(command, capture_output=True, text=True, check=False,
|
|
cwd=REPO_ROOT.parent, env=env)
|
|
|
|
|
|
def test_module_runs_list():
|
|
"""python -m daedalus --list prints the version line with an empty registry."""
|
|
result = run_cli(sys.executable, "-m", "daedalus", "--list")
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout.startswith("Daedalus Version:")
|
|
|
|
|
|
@pytest.mark.skipif(os.name != "nt", reason="cmd.exe is Windows only")
|
|
def test_bat_launcher():
|
|
"""bin/daedalus.bat runs the CLI and passes the exit code."""
|
|
launcher = str(REPO_ROOT / "bin" / "daedalus.bat")
|
|
assert run_cli(launcher, "--list").returncode == 0
|
|
assert run_cli(launcher, "no_such_script").returncode == 2
|
|
|
|
|
|
@pytest.mark.skipif(os.name == "nt", reason="bash launcher is for Linux")
|
|
def test_bash_launcher():
|
|
"""bin/daedalus runs the CLI and passes the exit code."""
|
|
launcher = str(REPO_ROOT / "bin" / "daedalus")
|
|
assert run_cli("bash", launcher, "--list").returncode == 0
|
|
assert run_cli("bash", launcher, "no_such_script").returncode == 2
|