| 1 | #!/bin/sh |
| 2 | TEST_PREFIX="[int-builtin-cross]" |
| 3 | SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) |
| 4 | SHELL_BIN="${SHELL_BIN:?ERROR: SHELL_BIN must be set}" |
| 5 | export SHELL_BIN |
| 6 | . "$SCRIPT_DIR/../test_harness.sh" |
| 7 | |
| 8 | # Integration tests: builtin-to-builtin interactions |
| 9 | |
| 10 | section "1. declare/export/readonly interaction" |
| 11 | compare_output "declare -x is export" 'declare -x BDX=exported; echo $BDX' |
| 12 | compare_output "declare -r is readonly" 'declare -r BDR=readonly; { BDR=other; } 2>/dev/null; echo $BDR' |
| 13 | compare_output "export then readonly" 'export BER=val; readonly BER; { BER=other; } 2>/dev/null; echo $BER' |
| 14 | compare_output "declare -ix integer export" 'declare -ix BDI=5; echo $BDI' |
| 15 | compare_output "declare -a then export" 'declare -a BDA=(1 2 3); export BDA; echo ${BDA[@]}' |
| 16 | compare_output "readonly then unset fails" 'readonly BRU=val; unset BRU 2>/dev/null; echo $? $BRU' |
| 17 | compare_output "export -p shows exported" 'export BEP=val; export -p | grep BEP | head -1' |
| 18 | |
| 19 | section "2. set/shift interaction" |
| 20 | compare_output "set then shift" 'set -- a b c; shift; echo $@' |
| 21 | compare_output "set then shift 2" 'set -- a b c; shift 2; echo $@' |
| 22 | compare_output "set then count" 'set -- a b c; echo $#; shift; echo $#' |
| 23 | compare_output "set resets positional" 'set -- a b c; set -- x y; echo $@' |
| 24 | compare_output "set empty clears" 'set -- a b c; set --; echo $#' |
| 25 | compare_output "shift all" 'set -- a b c; shift 3; echo $#' |
| 26 | |
| 27 | section "3. pushd/popd/dirs stack" |
| 28 | compare_output "round trip" 'ORIG=$(pwd); pushd /tmp >/dev/null; pushd /var >/dev/null; popd >/dev/null; popd >/dev/null; test "$(pwd)" = "$ORIG" && echo same' |
| 29 | compare_output "deep round trip" 'ORIG=$(pwd); pushd /tmp >/dev/null; pushd /var >/dev/null; pushd / >/dev/null; popd >/dev/null; popd >/dev/null; popd >/dev/null; test "$(pwd)" = "$ORIG" && echo same' |
| 30 | compare_output "dirs shows stack" 'pushd /tmp >/dev/null; pushd /var >/dev/null; dirs 2>/dev/null' |
| 31 | compare_output "dirs -v numbered" 'pushd /tmp >/dev/null; dirs -v 2>/dev/null' |
| 32 | compare_output "pushd swap top two" 'pushd /tmp >/dev/null; pushd /var >/dev/null; pushd >/dev/null 2>/dev/null; pwd' |
| 33 | compare_output "dirs -c clears stack" 'pushd /tmp >/dev/null; dirs -c 2>/dev/null; dirs 2>/dev/null' |
| 34 | |
| 35 | section "4. alias/eval interaction" |
| 36 | compare_output "alias in eval" 'alias bgreet="echo hi" 2>/dev/null; eval bgreet 2>/dev/null || echo no_expand' |
| 37 | compare_output "alias then unalias" 'alias bua="echo test" 2>/dev/null; unalias bua 2>/dev/null; echo $?' |
| 38 | compare_output "alias overwrite" 'alias bao="echo old" 2>/dev/null; alias bao="echo new" 2>/dev/null; eval bao 2>/dev/null || echo no_expand' |
| 39 | |
| 40 | section "5. trap/exit interaction" |
| 41 | compare_output "EXIT trap fires on exit" 'trap "echo bye" EXIT; exit 0' |
| 42 | compare_output "EXIT trap fires on implicit exit" 'trap "echo bye" EXIT; true' |
| 43 | compare_output "trap then trap replaces" 'trap "echo a" EXIT; trap "echo b" EXIT' |
| 44 | compare_output "trap then clear" 'trap "echo a" EXIT; trap - EXIT; echo done' |
| 45 | compare_output "EXIT trap sees last status" 'trap "echo \$?" EXIT; false' |
| 46 | |
| 47 | section "6. hash/command interaction" |
| 48 | compare_output "hash -r then command -v" 'hash -r 2>/dev/null; command -v ls' |
| 49 | compare_output "command -v builtin" 'command -v echo' |
| 50 | compare_output "command -v not found" 'command -v nonexistent_xyz_123; echo $?' |
| 51 | |
| 52 | section "7. type/command interaction" |
| 53 | compare_output "type echo" 'type echo 2>/dev/null | head -1' |
| 54 | compare_output "command -V echo" 'command -V echo 2>/dev/null | head -1' |
| 55 | compare_output "type external" 'type ls 2>/dev/null | head -1' |
| 56 | compare_output "type not found" 'type nonexistent_xyz_123 2>/dev/null; echo $?' |
| 57 | |
| 58 | section "8. read/echo round-trip" |
| 59 | compare_output "echo pipe read" 'echo "hello world" | { read A B; echo "$A $B"; }' |
| 60 | compare_output "printf pipe while read" 'printf "a\nb\nc\n" | { while read line; do echo "[$line]"; done; }' |
| 61 | compare_output "echo to file and read back" "echo 'data here' > $TEST_TMPDIR/brt1; read VAR < $TEST_TMPDIR/brt1; echo \$VAR" |
| 62 | compare_output "printf format and read" 'printf "%d\n" 42 | { read N; echo "got:$N"; }' |
| 63 | |
| 64 | section "9. eval/source chain" |
| 65 | compare_output "eval source" "echo 'BES=sourced' > $TEST_TMPDIR/bchain.sh; eval 'source $TEST_TMPDIR/bchain.sh'; echo \$BES" |
| 66 | compare_output "nested eval" 'eval "eval \"echo deep\""' |
| 67 | compare_output "eval with variable" 'CMD="echo hello"; eval "$CMD"' |
| 68 | compare_output "eval with special chars" 'eval "echo hello world"' |
| 69 | |
| 70 | section "10. let/declare -i interaction" |
| 71 | compare_output "declare -i then let" 'declare -i BDL=0; let BDL+=5; echo $BDL' |
| 72 | compare_output "let standalone" 'let X=3+4 2>/dev/null; echo $X' |
| 73 | compare_output "declare -i auto-arithmetic" 'declare -i BDA2=3+4; echo $BDA2' |
| 74 | compare_output "let comparison" 'let "5 > 3" 2>/dev/null; echo $?' |
| 75 | compare_output "let false comparison" 'let "3 > 5" 2>/dev/null; echo $?' |
| 76 | |
| 77 | print_summary |