| 1 | #!/bin/sh |
| 2 | TEST_PREFIX="[exec]" |
| 3 | . "$(cd "$(dirname "$0")" && pwd)/test_harness.sh" |
| 4 | |
| 5 | section "1. exec replaces shell" |
| 6 | compare_output "exec replaces shell with command" 'exec echo replaced' |
| 7 | compare_exit "exec replaces shell exit code true" 'exec true' |
| 8 | compare_exit "exec replaces shell exit code false" 'exec false' |
| 9 | compare_output "exec nothing after exec runs" 'exec echo hello; echo unreachable' |
| 10 | compare_output "exec with arguments" 'exec echo one two three' |
| 11 | compare_exit "exec nonexistent command fails" 'exec /nonexistent_cmd_xyz 2>/dev/null' |
| 12 | |
| 13 | section "2. exec with redirections only" |
| 14 | compare_output "exec redirect fd to /dev/null" "exec 3>/dev/null; echo ok" |
| 15 | compare_output "exec redirect fd to file" "exec 3>$TEST_TMPDIR/execout; echo hello >&3; exec 3>&-; cat $TEST_TMPDIR/execout" |
| 16 | compare_output "exec input redirect from file" "echo data > $TEST_TMPDIR/execin; exec 3<$TEST_TMPDIR/execin; read line <&3; echo \$line" |
| 17 | compare_output "exec close fd" "exec 3>$TEST_TMPDIR/execout; exec 3>&-; echo ok" |
| 18 | compare_output "exec redirect stdout" "exec >$TEST_TMPDIR/execstdout; echo captured; exec >/dev/tty 2>/dev/null; cat $TEST_TMPDIR/execstdout" |
| 19 | compare_output "exec redirect stderr" "exec 2>$TEST_TMPDIR/execstderr; echo err >&2; exec 2>/dev/tty 2>/dev/null; cat $TEST_TMPDIR/execstderr" |
| 20 | |
| 21 | section "3. exec in subshell" |
| 22 | compare_output "exec in subshell does not affect parent" '(exec echo sub); echo parent' |
| 23 | compare_exit "exec in subshell exit code" '(exec false)' |
| 24 | |
| 25 | section "4. exec preserves environment" |
| 26 | compare_output "exec preserves exported vars" 'export MYVAR=hello; exec echo $MYVAR' |
| 27 | compare_output "exec with PATH lookup" 'exec ls /dev/null' |
| 28 | |
| 29 | print_summary |