#!/usr/bin/env bats # tests/integration/cli.bats # Tests for trbldoc.sh command-line option parsing. # # Each test runs the full script (with md2html stubs) and asserts that # the correct sources are scanned and outputs produced, based on the options # passed. load "../helpers/common.bash" load "../helpers/profiler.bash" FIXTURES="$BATS_TEST_DIRNAME/../fixtures" _REPO_ROOT="${REPO_ROOT:-$(cd "$BATS_TEST_DIRNAME/../.." && pwd)}" TRBLDOC="$_REPO_ROOT/trbldoc.sh" setup() { setup_workspace CACHE=".trblcache" NS="$CACHE/namespace" } teardown() { teardown_workspace } # ── helpers ────────────────────────────────────────────────────────────────── run_pipeline() { bash "$TRBLDOC" "$@" # 2>/dev/null #profile "$TRBLDOC" "$@" 2>/dev/null #analyze >> profile.txt } stage_fixture() { local src="$1" local dest="$2" mkdir -p "$(dirname "$dest")" cp -r "$src" "$dest" } # ── error cases ────────────────────────────────────────────────────────────── @test "cli: no arguments exits non-zero with an error message" { run bash "$TRBLDOC" 2>&1 [ "$status" -ne 0 ] [[ "$output" == *"trbldoc:"* ]] } @test "cli: unknown option exits non-zero" { run bash "$TRBLDOC" -z 2>&1 [ "$status" -ne 0 ] } @test "cli: option missing its required argument exits non-zero" { run bash "$TRBLDOC" -s 2>&1 [ "$status" -ne 0 ] } # ── -s (source folder) ─────────────────────────────────────────────────────── @test "cli: -s flag specifies the source folder to scan" { stage_fixture "$FIXTURES/c/simple.c" "src/simple.c" run_pipeline -s src assert_dir_exists "$NS/test/simple" } @test "cli: multiple -s flags scan all specified folders" { stage_fixture "$FIXTURES/c/simple.c" "src1/simple.c" stage_fixture "$FIXTURES/c/multi_chunk.c" "src2/multi_chunk.c" run_pipeline -s src1 -s src2 assert_dir_exists "$NS/test/simple" assert_dir_exists "$NS/test/alpha" } # ── -o (output directory) ──────────────────────────────────────────────────── @test "cli: -o flag creates the specified output directory" { stage_fixture "$FIXTURES/c/simple.c" "src/simple.c" run_pipeline -s src -o my_output assert_dir_exists "my_output" assert_file_exists "my_output/test/simple/index.html" } @test "cli: without -o the default output directory .trblcache/built is created" { stage_fixture "$FIXTURES/c/simple.c" "src/simple.c" run_pipeline -s src assert_dir_exists "$CACHE/built" } # ── -f (custom file extension) ─────────────────────────────────────────────── @test "cli: -f flag registers a custom file extension for scanning" { mkdir -p src # Create a .myext file with C-style doc comments. # The -f option takes ext;start_regex;end_regex (AWK patterns without slashes). cat > src/custom.myext <<'SRCEOF' /* md @name test/custom-ext Hello from a custom extension. */ int noop() {} SRCEOF run_pipeline -s src -f 'myext;\/\*;\*\/' assert_dir_exists "$NS/test/custom-ext" } @test "cli: -f registered extension produces correct chunk content" { mkdir -p src cat > src/custom.myext <<'SRCEOF' /* md @name test/custom-content Content from custom ext. */ int noop() {} SRCEOF run_pipeline -s src -f 'myext;\/\*;\*\/' assert_contains "$NS/test/custom-content/index.html" "Content from custom ext" } # ── TRBLDOC_MD (markdown renderer override) ────────────────────────────────── @test "cli: TRBLDOC_MD overrides the markdown renderer for .md files" { mkdir -p src cat > src/doc.md <<'EOF' @name test/md-override # Heading Some content. EOF cat > custom_md.sh <<'EOF' #!/usr/bin/env bash # Stub: emit a recognisable marker instead of real HTML. cat "$@" > /dev/null echo "

custom renderer was here

" EOF chmod +x custom_md.sh TRBLDOC_MD="./custom_md.sh" run_pipeline -s src assert_contains "$NS/test/md-override/index.html" "custom-md-renderer" } @test "cli: TRBLDOC_MD override is not used when unset (default md2html stub runs)" { stage_fixture "$FIXTURES/md/simple.md" "src/simple.md" run_pipeline -s src # The default stub wraps content in class=stub-md; confirm it ran. assert_contains "$NS/test/md-simple/index.html" "stub-md" } # ── built-in site assembly ─────────────────────────────────────────────────── @test "cli: built output is assembled by default without external zod" { mkdir -p src cat > src/simple.c <<'EOF' /* md @name test/simple Hello from a C comment. */ int main() { return 0; } EOF run_pipeline -s src assert_file_exists "$CACHE/built/test/simple/index.html" assert_contains "$CACHE/built/test/simple/index.html" "" assert_contains "$CACHE/built/test/simple/index.html" "Hello from a C comment" assert_not_contains "$CACHE/built/test/simple/index.html" "{{{yield}}}" } @test "cli: default main.layout includes stylesheet and excludes highlightjs mermaid and mathjax" { stage_fixture "$FIXTURES/c/simple.c" "src/simple.c" run_pipeline -s src assert_contains "$NS/main.layout" '' assert_not_contains "$NS/main.layout" "highlight.min.js" assert_not_contains "$NS/main.layout" "highlightAll()" assert_not_contains "$NS/main.layout" "mermaid.min.js" assert_not_contains "$NS/main.layout" "mermaid.initialize" assert_not_contains "$NS/main.layout" "mathjax" } @test "cli: -l uses the provided main.layout template" { stage_fixture "$FIXTURES/c/simple.c" "src/simple.c" cat > alt.layout <<'EOF' {{ title }}
ALT {{ breadcrumb }}
{{ yield }}
EOF run_pipeline -s src -l alt.layout assert_contains "$NS/main.layout" "ALT {{ breadcrumb }}" assert_not_contains "$NS/main.layout" "Generated by trbldoc" } @test "cli: -l fails when layout template file does not exist" { stage_fixture "$FIXTURES/c/simple.c" "src/simple.c" run bash "$TRBLDOC" -s src -l does-not-exist.layout 2>&1 [ "$status" -ne 0 ] [[ "$output" == *"layout template not found"* ]] } @test "cli: files without a registered extension are not scanned" { mkdir -p src # .xyz has no registered comment form; no output namespace should appear. cat > src/ignored.xyz <<'SRCEOF' /* md @name test/should-not-exist This should not be processed. */ SRCEOF run_pipeline -s src if [[ -d "$NS/test/should-not-exist" ]]; then echo "FAILED: .xyz file was unexpectedly processed" >&2 return 1 fi }