"""Tests for hello_bash: the shared bash interface end to end (SPEC item 6.10). Direct runs, daedalus runs, help routing, and the completion of the help words after the script name (SPEC item 3.13.5.2). """ import os import shlex import subprocess import sys import pytest from daedalus import REPO_ROOT from daedalus import registry SCRIPT = registry.SCRIPTS["hello_bash"].absolute_path def run_script(*args): """Run the script directly with bash.""" return subprocess.run(["bash", str(SCRIPT), *args], capture_output=True, text=True, check=False) def run_daedalus(*args): """Run python -m daedalus from the checkout, like the installed CLI.""" env = {**os.environ, "PYTHONPATH": str(REPO_ROOT)} return subprocess.run([sys.executable, "-m", "daedalus", *args], capture_output=True, text=True, check=False, cwd=REPO_ROOT.parent, env=env) def test_main_defaults_to_world(): """No arguments: one hello line for the default name.""" result = run_script() assert result.returncode == 0 assert result.stdout == "hello, world (from daedalus bash)\n" def test_main_prints_one_line_per_name(): """Every name gets its hello line, arguments unchanged.""" result = run_script("me", "you too") assert result.returncode == 0 assert result.stdout == ("hello, me (from daedalus bash)\n" "hello, you too (from daedalus bash)\n") @pytest.mark.parametrize("word", ["help", "-h", "--help"]) def test_help_words_print_the_usage(word): """The help words route to hello_bash's help() (SPEC item 6.10.4).""" result = run_script(word) assert result.returncode == 0 assert result.stdout.startswith("usage: hello_bash [name...]") assert "Prints a hello line" in result.stdout def test_daedalus_runs_it(): """daedalus hello_bash runs through the bash runner (SPEC item 5.3).""" result = run_daedalus("hello_bash", "pi") assert result.returncode == 0, result.stderr assert result.stdout == "hello, pi (from daedalus bash)\n" def test_daedalus_category_prefix_runs_it(): """daedalus test hello_bash runs it through the category prefix.""" result = run_daedalus("test", "hello_bash") assert result.returncode == 0, result.stderr assert result.stdout == "hello, world (from daedalus bash)\n" def test_daedalus_help_word_passes_through(): """daedalus hello_bash --help shows the script's help, not daedalus's (SPEC item 3.1).""" result = run_daedalus("hello_bash", "--help") assert result.returncode == 0, result.stderr assert result.stdout.startswith("usage: hello_bash [name...]") def _probe(comp_line): """Replay the argcomplete hook and return the candidates (fd 8).""" env = {**os.environ, "_ARGCOMPLETE": "1", "COMP_LINE": comp_line, "COMP_POINT": str(len(comp_line)), "_ARGCOMPLETE_IFS": "\n", "_ARGCOMPLETE_SHELL": "bash", "PYTHONPATH": str(REPO_ROOT), "NO_COLOR": "1"} command = (f"exec 8>&1; exec 1>/dev/null; exec 2>/dev/null; " f"{shlex.quote(sys.executable)} -m daedalus") result = subprocess.run(["bash", "-c", command], env=env, cwd=REPO_ROOT, capture_output=True, text=True, check=False) return [line.strip() for line in result.stdout.splitlines() if line.strip()] @pytest.mark.skipif(os.name == "nt", reason="bash completion harness") def test_completion_offers_the_script_name(): """daedalus hello_ba completes hello_bash.""" assert _probe("daedalus hello_ba") == ["hello_bash"] @pytest.mark.skipif(os.name == "nt", reason="bash completion harness") def test_completion_offers_the_help_words(): """After the script name: only the help words (SPEC item 3.13.5.2).""" assert _probe("daedalus hello_bash ") == ["--help", "-h", "help"] assert _probe("daedalus hello_bash --") == ["--help"] assert _probe("daedalus test hello_bash ") == ["--help", "-h", "help"]