Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 09:14:49

0001 #!/usr/bin/env bash
0002 # Test suite for eic-shell wrapper script
0003 
0004 set -u
0005 
0006 EIC_SHELL="${1:-./eic-shell}"
0007 
0008 if [ ! -x "$EIC_SHELL" ]; then
0009   echo "ERROR: eic-shell executable not found at '$EIC_SHELL'"
0010   exit 1
0011 fi
0012 
0013 echo "=================================================="
0014 echo "Testing eic-shell launcher: $EIC_SHELL"
0015 echo "=================================================="
0016 
0017 PASSED=0
0018 FAILED=0
0019 
0020 run_test() {
0021   local desc="$1"
0022   shift
0023   printf "Test %2d: %-60s " "$((PASSED + FAILED + 1))" "$desc"
0024   
0025   local output
0026   local exit_code=0
0027   output=$("$@" 2>&1) || exit_code=$?
0028 
0029   if [ "$exit_code" -eq 0 ]; then
0030     echo "[PASS]"
0031     PASSED=$((PASSED + 1))
0032     return 0
0033   else
0034     echo "[FAIL] (exit code: $exit_code)"
0035     echo "       Output: $output"
0036     FAILED=$((FAILED + 1))
0037     return 1
0038   fi
0039 }
0040 
0041 run_test_expect_code() {
0042   local expected_code="$1"
0043   local desc="$2"
0044   shift 2
0045   printf "Test %2d: %-60s " "$((PASSED + FAILED + 1))" "$desc"
0046 
0047   local output
0048   local exit_code=0
0049   output=$("$@" 2>&1) || exit_code=$?
0050 
0051   if [ "$exit_code" -eq "$expected_code" ]; then
0052     echo "[PASS]"
0053     PASSED=$((PASSED + 1))
0054     return 0
0055   else
0056     echo "[FAIL] (expected: $expected_code, got: $exit_code)"
0057     echo "       Output: $output"
0058     FAILED=$((FAILED + 1))
0059     return 1
0060   fi
0061 }
0062 
0063 run_test_output_match() {
0064   local expected_pattern="$1"
0065   local desc="$2"
0066   shift 2
0067   printf "Test %2d: %-60s " "$((PASSED + FAILED + 1))" "$desc"
0068 
0069   local output
0070   local exit_code=0
0071   output=$("$@" 2>&1) || exit_code=$?
0072 
0073   if [ "$exit_code" -eq 0 ] && echo "$output" | grep -q "$expected_pattern"; then
0074     echo "[PASS]"
0075     PASSED=$((PASSED + 1))
0076     return 0
0077   else
0078     echo "[FAIL] (exit code: $exit_code, pattern: '$expected_pattern')"
0079     echo "       Output: $output"
0080     FAILED=$((FAILED + 1))
0081     return 1
0082   fi
0083 }
0084 
0085 run_test_output_contains() {
0086   local str1="$1"
0087   local str2="$2"
0088   local desc="$3"
0089   shift 3
0090   printf "Test %2d: %-60s " "$((PASSED + FAILED + 1))" "$desc"
0091 
0092   local output
0093   local exit_code=0
0094   output=$("$@" 2>&1) || exit_code=$?
0095 
0096   if [ "$exit_code" -eq 0 ] && echo "$output" | grep -q "$str1" && echo "$output" | grep -q "$str2"; then
0097     echo "[PASS]"
0098     PASSED=$((PASSED + 1))
0099     return 0
0100   else
0101     echo "[FAIL] (exit code: $exit_code, expected to contain '$str1' and '$str2')"
0102     echo "       Output: $output"
0103     FAILED=$((FAILED + 1))
0104     return 1
0105   fi
0106 }
0107 
0108 # 1. Help flag
0109 run_test_output_match "USAGE:" "Print help with --help" \
0110   "$EIC_SHELL" --help
0111 
0112 # 2. Simple command execution
0113 run_test "Execute single command (true)" \
0114   "$EIC_SHELL" -- true
0115 
0116 # 3. Simple command with argument
0117 run_test_output_match "hello" "Execute simple command with argument" \
0118   "$EIC_SHELL" -- echo hello
0119 
0120 # 4. Multi-token command
0121 run_test_output_match "foo bar baz" "Execute multi-token command" \
0122   "$EIC_SHELL" -- echo foo bar baz
0123 
0124 # 5. Quoted single-string command
0125 run_test_output_match "foo bar" "Execute quoted single-string command" \
0126   "$EIC_SHELL" -- "echo foo bar"
0127 
0128 # 6. Explicit subshell with bash -c
0129 run_test_output_match "nested bash -c" "Execute subshell: bash -c 'echo ...'" \
0130   "$EIC_SHELL" -- bash -c "echo nested bash -c"
0131 
0132 # 7. Subshell with complex commands and arguments (reproducing cmake bug scenario)
0133 run_test_output_match "cmake-target-dummy" "Execute bash -c with multi-arg cmake-like flags" \
0134   "$EIC_SHELL" -- bash -c "echo cmake-target-dummy --build build --target lib"
0135 
0136 # 8. Command chaining with &&
0137 run_test_output_contains "part1" "part2" "Execute chained commands with &&" \
0138   "$EIC_SHELL" -- "echo part1 && echo part2"
0139 
0140 # 9. Arguments with spaces preserved
0141 run_test_output_match "arg with multiple spaces" "Preserve arguments containing spaces" \
0142   "$EIC_SHELL" -- bash -c 'echo "$1"' _ "arg with multiple spaces"
0143 
0144 # 10. Positional parameters in bash -c
0145 run_test_output_match "val1:val2:val3" "Preserve positional parameters in subshell" \
0146   "$EIC_SHELL" -- bash -c 'echo "$1:$2:$3"' _ "val1" "val2" "val3"
0147 
0148 # 11. Exit code preservation: success
0149 run_test_expect_code 0 "Preserve exit code 0" \
0150   "$EIC_SHELL" -- true
0151 
0152 # 12. Exit code preservation: failure (exit 1)
0153 run_test_expect_code 1 "Preserve exit code 1 (false)" \
0154   "$EIC_SHELL" -- false
0155 
0156 # 13. Exit code preservation: arbitrary code (exit 42)
0157 run_test_expect_code 42 "Preserve exit code 42" \
0158   "$EIC_SHELL" -- bash -c "exit 42"
0159 
0160 # 14. Exit code preservation: arbitrary code (exit 17)
0161 run_test_expect_code 17 "Preserve exit code 17" \
0162   "$EIC_SHELL" -- bash -c "exit 17"
0163 
0164 # 15. Piped stdin to a command
0165 run_test_output_match "piped data test" "Pipe stdin to a command (cat)" \
0166   bash -c "echo 'piped data test' | '$EIC_SHELL' -- cat"
0167 
0168 # 16. Piped stdin to interactive shell
0169 run_test_output_match "interactive pipe test" "Pipe commands on stdin to interactive shell" \
0170   bash -c "echo 'echo interactive pipe test' | '$EIC_SHELL'"
0171 
0172 # 17. Environment setup verification (/etc/eic-env.sh sourced)
0173 run_test "Environment setup sourced (/etc/eic-env.sh)" \
0174   "$EIC_SHELL" -- bash -c "test -f /etc/eic-env.sh"
0175 
0176 echo "=================================================="
0177 echo "Summary: $PASSED passed, $FAILED failed out of $((PASSED + FAILED)) tests"
0178 echo "=================================================="
0179 
0180 if [ "$FAILED" -ne 0 ]; then
0181   exit 1
0182 fi
0183 exit 0