Files
daedalus/bash/tests/test_bash_interface.py
hermes-pi ec9848b169 daedalus 0.4.0: bash script interface (help() dispatch) + hello_bash
- bash/daedalus_common.sh (new): the shared interface for bash scripts
  (SPEC item 6.10) - a script sources its sibling helper, defines help()
  (usage) and main(), and ends with daedalus_bash_dispatch "$@" which
  routes help/-h/--help to help() and everything else to main()
  unchanged (clear fallback errors when a script forgets either)
- completion for bash scripts (SPEC item 3.13.5.2): after a completed
  script name daedalus offers --help, -h, help, prefix-filtered, written
  itself on the fd-8 argcomplete protocol - no script runs on TAB;
  scripts without the interface get the shell default
- bash/hello_bash.sh (new, category test): sample script through the
  interface, the bash twin of hello, with tests; registry entry, README
  row, CHANGELOG bullet
- tests: bash/tests/test_bash_interface.py (helper behavior, bash -n,
  enforcement of the interface for every registered .sh),
  test_hello_bash.py (direct and daedalus runs, help routing, SPEC 3.1
  pass-through, completion probes including the user's
  daedalus hello_bash<TAB><TAB> case), completions units + e2e harness
- README/INSTALL/SPEC updated (6.10, 3.13.5.2, tree, boilerplate with
  shellcheck directive); autotest green (212 tests)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-18 22:35:11 +01:00

83 lines
2.8 KiB
Python

"""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)"