tenseleyflow/bensch / 4936ef0

Browse files

extract extended builtin tests (bash/zsh features)

test_complete_compgen.sh, test_coproc.sh, test_let.sh — bash extensions
not part of POSIX but supported by bash, zsh, ksh, fortsh.

Source: fortsh/tests/builtins/
Authored by espadonne
SHA
4936ef0fd778c88a090ea907fd2556b80d152493
Parents
8636752
Tree
40b075d

3 changed files

StatusFile+-
A suites/builtins/extended/test_complete_compgen.sh 31 0
A suites/builtins/extended/test_coproc.sh 34 0
A suites/builtins/extended/test_let.sh 29 0
suites/builtins/extended/test_complete_compgen.shadded
@@ -0,0 +1,31 @@
1
+#!/bin/sh
2
+TEST_PREFIX="[complete-compgen]"
3
+. "$(cd "$(dirname "$0")" && pwd)/test_harness.sh"
4
+
5
+section "1. complete define"
6
+check_exit "complete with -W word list" 'complete -W "start stop restart" myservice' "0"
7
+check_exit "complete with -F function" 'complete -F _my_completer mycommand 2>/dev/null; true' "0"
8
+check_exit "complete with -A action" 'complete -A command mytest 2>/dev/null; true' "0"
9
+check_exit "complete multiple options" 'complete -W "a b" -o nospace testcmd 2>/dev/null; true' "0"
10
+
11
+section "2. complete list and remove"
12
+check_exit "complete -p prints completions" 'complete -W "a b c" testcmd; complete -p' "0"
13
+compare_output "complete -p shows defined spec" 'complete -W "alpha beta" testcmd; complete -p testcmd'
14
+check_exit "complete -r removes completion" 'complete -W "a b" testcmd; complete -r testcmd' "0"
15
+check_exit "complete -r nonexistent" 'complete -r nonexistent_cmd 2>/dev/null; true' "0"
16
+compare_output "complete -p after -r is empty" 'complete -W "a b" testcmd; complete -r testcmd; complete -p testcmd 2>/dev/null; echo $?'
17
+
18
+section "3. compgen basic"
19
+compare_output "compgen -W filters by prefix" 'compgen -W "start stop status" st'
20
+compare_output "compgen -W no match empty output" 'compgen -W "alpha beta" z'
21
+compare_output "compgen -W exact match" 'compgen -W "alpha beta gamma" alpha'
22
+compare_output "compgen -W all match empty prefix" 'compgen -W "a b c"'
23
+
24
+section "4. compgen edge cases"
25
+compare_output "compgen -W single word" 'compgen -W "only" o'
26
+compare_output "compgen -W empty string" 'compgen -W "" a'
27
+compare_output "compgen -W multiple matches" 'compgen -W "start stop stash status" st'
28
+compare_output "compgen -W case sensitive" 'compgen -W "Alpha Beta" a'
29
+compare_output "compgen -W no prefix matches all" 'compgen -W "x y z" | wc -l | tr -d " "'
30
+
31
+print_summary
suites/builtins/extended/test_coproc.shadded
@@ -0,0 +1,34 @@
1
+#!/bin/sh
2
+TEST_PREFIX="[coproc]"
3
+
4
+# Coproc tests require job control (PTY) — skip in CI/non-interactive mode
5
+if [ ! -t 0 ] && [ -z "$FORCE_COPROC_TESTS" ]; then
6
+    echo "Passed:  0"
7
+    echo "Failed:  0"
8
+    echo "Skipped: 0"
9
+    echo "Total:   0"
10
+    exit 0
11
+fi
12
+
13
+. "$(cd "$(dirname "$0")" && pwd)/test_harness.sh"
14
+
15
+section "1. coproc basic launch"
16
+compare_output "coproc sets COPROC_PID" 'coproc sleep 60; test -n "$COPROC_PID" && echo yes; kill $COPROC_PID 2>/dev/null; wait 2>/dev/null'
17
+compare_output "coproc fd array exists" 'coproc sleep 60; test -n "${COPROC[0]}" && test -n "${COPROC[1]}" && echo yes; kill $COPROC_PID 2>/dev/null; wait 2>/dev/null'
18
+compare_exit "coproc PID is valid process" 'coproc sleep 60; kill -0 $COPROC_PID; ret=$?; kill $COPROC_PID 2>/dev/null; wait 2>/dev/null; exit $ret'
19
+
20
+section "2. coproc I/O"
21
+compare_output "coproc bidirectional cat" 'coproc cat; echo hello >&${COPROC[1]}; read line <&${COPROC[0]}; echo $line; kill $COPROC_PID 2>/dev/null; wait 2>/dev/null'
22
+compare_output "coproc multiple writes and reads" 'coproc cat; echo one >&${COPROC[1]}; echo two >&${COPROC[1]}; read l1 <&${COPROC[0]}; read l2 <&${COPROC[0]}; echo "$l1 $l2"; kill $COPROC_PID 2>/dev/null; wait 2>/dev/null'
23
+compare_output "coproc with tr" 'coproc tr a-z A-Z; echo hello >&${COPROC[1]}; eval "exec ${COPROC[1]}>&-"; read line <&${COPROC[0]}; echo $line; wait 2>/dev/null'
24
+
25
+section "3. named coproc"
26
+# bash only supports named coprocs with compound commands; fortsh extends to simple commands
27
+check_output "named coproc I/O" 'coproc MYPROC cat; echo test >&${MYPROC[1]}; read line <&${MYPROC[0]}; echo $line; kill $MYPROC_PID 2>/dev/null; wait 2>/dev/null' "test"
28
+check_output "named coproc PID var" 'coproc MYPROC sleep 60; test -n "$MYPROC_PID" && echo yes; kill $MYPROC_PID 2>/dev/null; wait 2>/dev/null' "yes"
29
+
30
+section "4. coproc cleanup"
31
+compare_output "coproc cleanup after kill" 'coproc sleep 60; kill $COPROC_PID 2>/dev/null; wait 2>/dev/null; echo done'
32
+compare_output "coproc wait returns exit" 'coproc true; wait $COPROC_PID; echo $?'
33
+
34
+print_summary
suites/builtins/extended/test_let.shadded
@@ -0,0 +1,29 @@
1
+#!/bin/sh
2
+TEST_PREFIX="[let]"
3
+. "$(cd "$(dirname "$0")" && pwd)/test_harness.sh"
4
+
5
+section "1. let arithmetic"
6
+compare_output "let addition" 'let "x=5+3"; echo $x'
7
+compare_output "let subtraction" 'let "x=10-3"; echo $x'
8
+compare_output "let multiplication" 'let "x=4*3"; echo $x'
9
+compare_output "let division" 'let "x=20/4"; echo $x'
10
+compare_output "let modulo" 'let "x=17%5"; echo $x'
11
+
12
+section "2. let compound assignment"
13
+compare_output "let increment" 'x=10; let "x++"; echo $x'
14
+compare_output "let decrement" 'x=10; let "x--"; echo $x'
15
+compare_output "let multiply assign" 'x=4; let "x*=3"; echo $x'
16
+compare_output "let add assign" 'x=10; let "x+=5"; echo $x'
17
+compare_output "let subtract assign" 'x=10; let "x-=3"; echo $x'
18
+
19
+section "3. let exit codes"
20
+compare_exit "let nonzero result exits 0" 'let "1+1"'
21
+compare_exit "let zero result exits 1" 'let "0"'
22
+compare_exit "let comparison true" 'let "5 > 3"'
23
+compare_exit "let comparison false" 'let "3 > 5"'
24
+
25
+section "4. let multiple expressions"
26
+compare_output "let multiple args" 'let "x=5" "y=10" "z=x+y"; echo $z'
27
+compare_output "let with variables" 'a=3; b=4; let "c=a*b"; echo $c'
28
+
29
+print_summary