"""Tests for the daedalus bash script interface (SPEC item 6.10). `bash/daedalus_common.sh` routes the help words to the script's help() and everything else to its main(); every registered .sh script must implement the interface. """ import re import subprocess import pytest from daedalus import REPO_ROOT from daedalus import registry COMMON = REPO_ROOT / "bash" / "daedalus_common.sh" SCRIPT_TEMPLATE = """#!/usr/bin/env bash . "{common}" help() {{ echo "usage: sample [args...] (help from help)" }} main() {{ echo "main $*" echo "count $#" return 3 }} daedalus_bash_dispatch "$@" """ @pytest.fixture def sample(tmp_path): """A minimal script that implements the interface (SPEC items 6.10.1-6.10.4).""" path = tmp_path / "sample.sh" path.write_text(SCRIPT_TEMPLATE.format(common=COMMON)) path.chmod(0o755) return path def run(script, *args): """Run a script with bash and capture everything.""" return subprocess.run(["bash", str(script), *args], capture_output=True, text=True, check=False) @pytest.mark.parametrize("word", ["help", "-h", "--help"]) def test_help_words_route_to_help(sample, word): """help, -h, and --help print help() and exit with its status (SPEC item 6.10.4).""" result = run(sample, word) assert result.returncode == 0 assert result.stdout == "usage: sample [args...] (help from help)\n" @pytest.mark.parametrize("args, count", [(("build", "--x", "y z"), 3), ((), 0)]) def test_main_receives_the_arguments(sample, args, count): """Everything except the help words goes to main() unchanged (SPEC item 6.10.4).""" result = run(sample, *args) assert result.returncode == 3 assert result.stdout.splitlines() == [f"main {' '.join(args)}", f"count {count}"] def test_common_script_parses(): """bash -n accepts the shared helper itself.""" assert subprocess.run(["bash", "-n", str(COMMON)], check=False).returncode == 0 def test_every_registered_bash_script_implements_the_interface(): """Every .sh entry in SCRIPTS: source line, help(), main(), dispatch.""" for name, script in sorted(registry.SCRIPTS.items()): path = script.absolute_path if path.suffix != ".sh": continue assert path.is_file(), f"{name}: {path} does not exist" text = path.read_text(encoding="utf-8") assert "daedalus_common.sh" in text, \ f"{name} must source the shared helper (SPEC item 6.10.1)" assert re.search(r"^help\s*\(\s*\)", text, re.MULTILINE), \ f"{name} must define help() (SPEC item 6.10.2)" assert re.search(r"^main\s*\(\s*\)", text, re.MULTILINE), \ f"{name} must define main() (SPEC item 6.10.3)" assert 'daedalus_bash_dispatch "$@"' in text, \ f"{name} must end with the dispatch call (SPEC item 6.10.4)"