|
| 1 | +#!/usr/bin/env bash |
| 2 | +# tests/scripts/test-bash-runner-discovery.sh |
| 3 | +# Tests for plugins/dso/scripts/runners/bash-runner.sh discovery behavior. |
| 4 | +# |
| 5 | +# Verifies that the bash runner discovers test-*.sh files but excludes |
| 6 | +# run-*-tests.sh aggregator scripts (which are suite orchestrators, not |
| 7 | +# individual test items). |
| 8 | +# |
| 9 | +# Usage: bash tests/scripts/test-bash-runner-discovery.sh |
| 10 | +# Returns: exit 0 if all tests pass, exit 1 if any fail |
| 11 | + |
| 12 | +set -uo pipefail |
| 13 | + |
| 14 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 15 | +PLUGIN_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 16 | +DSO_PLUGIN_DIR="$PLUGIN_ROOT/plugins/dso" |
| 17 | +REPO_ROOT="$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)" |
| 18 | +BASH_RUNNER="$DSO_PLUGIN_DIR/scripts/runners/bash-runner.sh" |
| 19 | +TEST_BATCHED="$DSO_PLUGIN_DIR/scripts/test-batched.sh" |
| 20 | + |
| 21 | +source "$SCRIPT_DIR/../lib/run_test.sh" |
| 22 | + |
| 23 | +echo "=== test-bash-runner-discovery.sh ===" |
| 24 | + |
| 25 | +# ── Helpers ────────────────────────────────────────────────────────────────── |
| 26 | + |
| 27 | +_CLEANUP_DIRS=() |
| 28 | +_cleanup() { for d in "${_CLEANUP_DIRS[@]}"; do rm -rf "$d"; done; } |
| 29 | +trap _cleanup EXIT |
| 30 | + |
| 31 | +# ── Test 1: bash-runner.sh exists and is executable ────────────────────────── |
| 32 | +echo "Test 1: bash-runner.sh exists and is executable" |
| 33 | +if [ -f "$BASH_RUNNER" ] && [ -r "$BASH_RUNNER" ]; then |
| 34 | + echo " PASS: bash-runner.sh exists" |
| 35 | + (( PASS++ )) |
| 36 | +else |
| 37 | + echo " FAIL: bash-runner.sh not found at $BASH_RUNNER" >&2 |
| 38 | + (( FAIL++ )) |
| 39 | +fi |
| 40 | + |
| 41 | +# ── Test 2: Discovery does NOT include run-*-tests.sh files ────────────────── |
| 42 | +echo "Test 2: test_discovery_excludes_aggregators — run-*-tests.sh not discovered" |
| 43 | +test_discovery_excludes_aggregators() { |
| 44 | + local tmpdir |
| 45 | + tmpdir=$(mktemp -d) |
| 46 | + _CLEANUP_DIRS+=("$tmpdir") |
| 47 | + |
| 48 | + # Create test-*.sh files (should be discovered) |
| 49 | + cat > "$tmpdir/test-alpha.sh" << 'EOF' |
| 50 | +#!/usr/bin/env bash |
| 51 | +echo "PASSED: 1 FAILED: 0" |
| 52 | +exit 0 |
| 53 | +EOF |
| 54 | + chmod +x "$tmpdir/test-alpha.sh" |
| 55 | + |
| 56 | + cat > "$tmpdir/test-beta.sh" << 'EOF' |
| 57 | +#!/usr/bin/env bash |
| 58 | +echo "PASSED: 1 FAILED: 0" |
| 59 | +exit 0 |
| 60 | +EOF |
| 61 | + chmod +x "$tmpdir/test-beta.sh" |
| 62 | + |
| 63 | + # Create run-*-tests.sh aggregator (should NOT be discovered) |
| 64 | + cat > "$tmpdir/run-all-tests.sh" << 'EOF' |
| 65 | +#!/usr/bin/env bash |
| 66 | +echo "I am an aggregator — I should not be run as an individual test item" |
| 67 | +exit 0 |
| 68 | +EOF |
| 69 | + chmod +x "$tmpdir/run-all-tests.sh" |
| 70 | + |
| 71 | + # Run batched runner and check output for the aggregator filename |
| 72 | + local output exit_code=0 |
| 73 | + output=$(TEST_BATCHED_STATE_FILE="$tmpdir/state.json" \ |
| 74 | + bash "$TEST_BATCHED" --timeout=30 --runner=bash --test-dir="$tmpdir" 2>&1) || exit_code=$? |
| 75 | + |
| 76 | + # The aggregator must NOT appear in the run output |
| 77 | + if echo "$output" | grep -q "run-all-tests.sh"; then |
| 78 | + echo " DEBUG: aggregator was discovered and run" >&2 |
| 79 | + return 1 |
| 80 | + fi |
| 81 | + |
| 82 | + # test-alpha.sh and test-beta.sh must appear |
| 83 | + echo "$output" | grep -q "test-alpha.sh" || return 1 |
| 84 | + echo "$output" | grep -q "test-beta.sh" || return 1 |
| 85 | +} |
| 86 | +if test_discovery_excludes_aggregators; then |
| 87 | + echo " PASS: run-*-tests.sh excluded from discovery" |
| 88 | + (( PASS++ )) |
| 89 | +else |
| 90 | + echo " FAIL: run-*-tests.sh was discovered as a test item" >&2 |
| 91 | + (( FAIL++ )) |
| 92 | +fi |
| 93 | + |
| 94 | +# ── Test 3: Discovery DOES include test-*.sh files ─────────────────────────── |
| 95 | +echo "Test 3: test_discovery_includes_test_files — test-*.sh files discovered" |
| 96 | +test_discovery_includes_test_files() { |
| 97 | + local tmpdir |
| 98 | + tmpdir=$(mktemp -d) |
| 99 | + _CLEANUP_DIRS+=("$tmpdir") |
| 100 | + |
| 101 | + cat > "$tmpdir/test-gamma.sh" << 'EOF' |
| 102 | +#!/usr/bin/env bash |
| 103 | +echo "PASSED: 1 FAILED: 0" |
| 104 | +exit 0 |
| 105 | +EOF |
| 106 | + chmod +x "$tmpdir/test-gamma.sh" |
| 107 | + |
| 108 | + local output exit_code=0 |
| 109 | + output=$(TEST_BATCHED_STATE_FILE="$tmpdir/state.json" \ |
| 110 | + bash "$TEST_BATCHED" --timeout=30 --runner=bash --test-dir="$tmpdir" 2>&1) || exit_code=$? |
| 111 | + |
| 112 | + echo "$output" | grep -q "test-gamma.sh" |
| 113 | +} |
| 114 | +if test_discovery_includes_test_files; then |
| 115 | + echo " PASS: test-*.sh files discovered" |
| 116 | + (( PASS++ )) |
| 117 | +else |
| 118 | + echo " FAIL: test-*.sh files not discovered" >&2 |
| 119 | + (( FAIL++ )) |
| 120 | +fi |
| 121 | + |
| 122 | +# ── Test 4: Warning message does not mention run-*-tests.sh ────────────────── |
| 123 | +echo "Test 4: test_warning_message_no_aggregator_mention — fallback warning updated" |
| 124 | +test_warning_message_no_aggregator_mention() { |
| 125 | + # When no test files exist, the warning should only mention test-*.sh |
| 126 | + local tmpdir |
| 127 | + tmpdir=$(mktemp -d) |
| 128 | + _CLEANUP_DIRS+=("$tmpdir") |
| 129 | + mkdir -p "$tmpdir/empty-dir" |
| 130 | + |
| 131 | + local output exit_code=0 |
| 132 | + output=$(TEST_BATCHED_STATE_FILE="$tmpdir/state.json" \ |
| 133 | + bash "$TEST_BATCHED" --timeout=30 --runner=bash --test-dir="$tmpdir/empty-dir" 2>&1) || exit_code=$? |
| 134 | + |
| 135 | + # Warning should NOT mention run-*-tests.sh |
| 136 | + if echo "$output" | grep -q "run-\*-tests.sh"; then |
| 137 | + return 1 |
| 138 | + fi |
| 139 | + return 0 |
| 140 | +} |
| 141 | +if test_warning_message_no_aggregator_mention; then |
| 142 | + echo " PASS: warning message does not mention run-*-tests.sh" |
| 143 | + (( PASS++ )) |
| 144 | +else |
| 145 | + echo " FAIL: warning message still mentions run-*-tests.sh" >&2 |
| 146 | + (( FAIL++ )) |
| 147 | +fi |
| 148 | + |
| 149 | +echo "" |
| 150 | +echo "Results: $PASS passed, $FAIL failed" |
| 151 | +[ "$FAIL" -eq 0 ] |
0 commit comments