Bash · 15633 bytes Raw Blame History
1 #!/bin/sh
2 # =====================================
3 # POSIX Builtin Gap Tests
4 # =====================================
5 # Tests for POSIX shell builtins
6 # Split from posix_compliance_gaps.sh for better organization
7
8 # Colors (POSIX-compliant way)
9 RED='\033[0;31m'
10 GREEN='\033[0;32m'
11 YELLOW='\033[1;33m'
12 BLUE='\033[0;34m'
13 NC='\033[0m'
14
15 # Test identification
16 TEST_PREFIX="[gaps-builtins]"
17 CURRENT_SECTION=""
18 TEST_NUM=0
19
20 PASSED=0
21 FAILED=0
22 SKIPPED=0
23 FAILED_TESTS_LIST=""
24
25 # Get script directory (POSIX way)
26 SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
27 SHELL_BIN="${SHELL_BIN:?ERROR: SHELL_BIN must be set}"
28 BASH_REF="${BASH_REF:-bash}"
29
30 # Check if shell binary exists
31 if [ ! -x "$SHELL_BIN" ]; then
32 printf "${RED}ERROR${NC}: shell binary not found at $SHELL_BIN\n"
33 printf "Please set SHELL_BIN or set SHELL_BIN environment variable\n"
34 exit 1
35 fi
36
37 pass() {
38 TEST_NUM=$((TEST_NUM + 1))
39 printf "${GREEN}✓ PASS${NC} ${TEST_PREFIX} ${CURRENT_SECTION}.${TEST_NUM}: %s\n" "$1"
40 PASSED=$((PASSED + 1))
41 }
42
43 fail() {
44 TEST_NUM=$((TEST_NUM + 1))
45 TEST_ID="${TEST_PREFIX} ${CURRENT_SECTION}.${TEST_NUM}"
46 printf "${RED}✗ FAIL${NC} ${TEST_ID}: %s\n" "$1"
47 FAILED_TESTS_LIST="${FAILED_TESTS_LIST} ${TEST_ID}: $1\n"
48 if [ -n "$2" ]; then printf " posix: %s\n" "$2"; fi
49 if [ -n "$3" ]; then printf " shell: %s\n" "$3"; fi
50 FAILED=$((FAILED + 1))
51 }
52
53 section() {
54 CURRENT_SECTION=$(echo "$1" | grep -oE '^[0-9]+' || echo "0")
55 TEST_NUM=0
56 printf "\n${BLUE}==========================================\n%s\n==========================================${NC}\n" "$1"
57 }
58
59 normalize_output() { sed -e 's|^[^ ]*/[a-z]*sh[0-9]*: |sh: |' -e 's|^[a-z]*sh[0-9]*: |sh: |' -e 's/line [0-9]*: //'; }
60
61 compare_posix_output() {
62 test_name="$1"; command="$2"
63 posix_out=$("$BASH_REF" -c "$command" 2>&1 | normalize_output)
64 shell_out=$("$SHELL_BIN" -c "$command" 2>&1 | normalize_output)
65 if [ "$posix_out" = "$shell_out" ]; then pass "$test_name"
66 else fail "$test_name" "$posix_out" "$shell_out"; fi
67 }
68
69 compare_posix_exit_code() {
70 test_name="$1"; command="$2"
71 "$BASH_REF" -c "$command" >/dev/null 2>&1; posix_code=$?
72 "$SHELL_BIN" -c "$command" >/dev/null 2>&1; shell_code=$?
73 if [ "$posix_code" = "$shell_code" ]; then pass "$test_name"
74 else fail "$test_name" "exit $posix_code" "exit $shell_code"; fi
75 }
76
77 compare_posix_error() {
78 test_name="$1"; command="$2"
79 posix_out=$("$BASH_REF" -c "$command" 2>&1 | normalize_output)
80 shell_out=$("$SHELL_BIN" -c "$command" 2>&1 | normalize_output)
81 if [ "$posix_out" = "$shell_out" ]; then pass "$test_name"
82 else fail "$test_name" "$posix_out" "$shell_out"; fi
83 }
84
85 # ============================================================================
86 # CD BUILTIN
87 # ============================================================================
88
89 section "1. CD BUILTIN"
90 compare_posix_output "cd tmp" 'cd /tmp; pwd'
91 compare_posix_output "cd home" 'cd ~; pwd | grep -c /'
92 compare_posix_output "cd dash" 'cd /tmp; cd /; cd -'
93 compare_posix_output "cd dotdot" 'cd /tmp; cd ..; pwd'
94
95 # ============================================================================
96 # PWD BUILTIN
97 # ============================================================================
98
99 section "2. PWD BUILTIN"
100 compare_posix_output "pwd basic" 'pwd | grep -c /'
101 compare_posix_output "pwd P" 'pwd -P | grep -c /'
102 compare_posix_output "pwd L" 'pwd -L | grep -c /'
103
104 # ============================================================================
105 # ECHO BUILTIN
106 # ============================================================================
107
108 section "3. ECHO BUILTIN"
109 compare_posix_output "echo basic" 'echo hello'
110 compare_posix_output "echo multi" 'echo hello world'
111 compare_posix_output "echo empty" 'echo ""'
112 compare_posix_output "echo n" 'echo -n hello; echo world'
113 compare_posix_output "echo special" 'echo "a\tb"'
114
115 # ============================================================================
116 # PRINTF BUILTIN
117 # ============================================================================
118
119 section "4. PRINTF BUILTIN"
120 compare_posix_output "printf s" 'printf "%s\n" hello'
121 compare_posix_output "printf d" 'printf "%d\n" 42'
122 compare_posix_output "printf x" 'printf "%x\n" 255'
123 compare_posix_output "printf o" 'printf "%o\n" 8'
124 compare_posix_output "printf width" 'printf "%5d\n" 42'
125 compare_posix_output "printf left" 'printf "%-5d|\n" 42'
126 compare_posix_output "printf zero" 'printf "%05d\n" 42'
127
128 # ============================================================================
129 # TEST BUILTIN
130 # ============================================================================
131
132 section "5. TEST NUMERIC OPERATORS"
133 compare_posix_output "test eq" '[ 5 -eq 5 ]; echo $?'
134 compare_posix_output "test ne" '[ 5 -ne 3 ]; echo $?'
135 compare_posix_output "test lt" '[ 3 -lt 5 ]; echo $?'
136 compare_posix_output "test gt" '[ 5 -gt 3 ]; echo $?'
137 compare_posix_output "test le" '[ 5 -le 5 ]; echo $?'
138 compare_posix_output "test ge" '[ 5 -ge 5 ]; echo $?'
139
140 section "6. TEST STRING OPERATORS"
141 compare_posix_output "test z empty" '[ -z "" ]; echo $?'
142 compare_posix_output "test z nonempty" '[ -z "x" ]; echo $?'
143 compare_posix_output "test n empty" '[ -n "" ]; echo $?'
144 compare_posix_output "test n nonempty" '[ -n "x" ]; echo $?'
145 compare_posix_output "test str eq" '[ "a" = "a" ]; echo $?'
146 compare_posix_output "test str ne" '[ "a" != "b" ]; echo $?'
147
148 section "7. TEST FILE OPERATORS"
149 compare_posix_output "test f" '[ -f /etc/passwd ]; echo $?'
150 compare_posix_output "test d" '[ -d /tmp ]; echo $?'
151 compare_posix_output "test e" '[ -e /tmp ]; echo $?'
152 compare_posix_output "test r" '[ -r /etc/passwd ]; echo $?'
153 compare_posix_output "test x" '[ -x /bin/sh ]; echo $?'
154 compare_posix_output "test s" 'echo x > /tmp/ts$$; [ -s /tmp/ts$$ ]; echo $?; rm /tmp/ts$$'
155
156 section "8. TEST COMPOUND"
157 compare_posix_output "test and" '[ -d /tmp -a -f /etc/passwd ]; echo $?'
158 compare_posix_output "test or" '[ -d /nonexistent -o -f /etc/passwd ]; echo $?'
159 compare_posix_output "test not" '[ ! -d /nonexistent ]; echo $?'
160 compare_posix_output "test paren" '[ \( -d /tmp \) ]; echo $?'
161
162 # ============================================================================
163 # TYPE AND COMMAND BUILTINS
164 # ============================================================================
165
166 section "9. TYPE BUILTIN"
167 compare_posix_output "type builtin" "type echo | grep -ci 'builtin\\|built-in\\|shell builtin'"
168 compare_posix_output "type function" "f() { :; }; type f | grep -c function"
169 compare_posix_output "type external" "type cat | grep -c '/'"
170 compare_posix_exit_code "type nonexistent" "type nonexistent_$$ 2>/dev/null"
171
172 section "10. COMMAND BUILTIN"
173 compare_posix_output "command v" 'command -v echo | grep -c echo'
174 compare_posix_output "command V" 'command -V echo 2>/dev/null | grep -c echo'
175 compare_posix_output "command p" 'command -p echo test'
176
177 # ============================================================================
178 # SET BUILTIN
179 # ============================================================================
180
181 section "11. SET BUILTIN"
182 compare_posix_output "set -- clears positionals" "set -- a b; set --; echo \$#"
183 compare_posix_error "set -- with empty" "set -- ''; echo \$# |\$1|"
184 compare_posix_output "set -- with spaces" "set -- 'a b' 'c d'; echo \$1"
185 compare_posix_output "set without args shows vars" "X=1; set | grep -c '^X='"
186 compare_posix_output "set -o lists options" "set -o 2>&1 | wc -l"
187 compare_posix_output "set args" 'set -- a b c; echo $1 $2 $3'
188 compare_posix_output "set count" 'set -- a b c d e; echo $#'
189 compare_posix_output "set all" 'set -- x y z; echo "$@"'
190 compare_posix_output "set star" 'set -- x y z; echo "$*"'
191
192 # ============================================================================
193 # SHIFT BUILTIN
194 # ============================================================================
195
196 section "12. SHIFT BUILTIN"
197 compare_posix_output "shift basic" 'set -- a b c; shift; echo $1'
198 compare_posix_output "shift count" 'set -- a b c; shift; echo $#'
199 compare_posix_output "shift 2" 'set -- a b c d; shift 2; echo $1'
200 compare_posix_output "shift with count" "set -- a b c d e; shift 3; echo \$1"
201 compare_posix_exit_code "shift too many" "set -- a b; shift 5 2>/dev/null"
202 compare_posix_output "shift zero" "set -- a b c; shift 0; echo \$#"
203 compare_posix_output "shift all" "set -- a b c; shift 3; echo \$#"
204 compare_posix_exit_code "shift with no args" "set --; shift 2>/dev/null"
205
206 # ============================================================================
207 # EVAL BUILTIN
208 # ============================================================================
209
210 section "13. EVAL BUILTIN"
211 compare_posix_output "eval with semicolons" "eval 'echo a; echo b' | wc -l"
212 compare_posix_output "eval with pipes" "eval 'echo test | cat'"
213 compare_posix_output "eval with redirects" "eval 'echo test > /tmp/posix_gaps_eval_$$'; cat /tmp/posix_gaps_eval_$$; rm -f /tmp/posix_gaps_eval_$$"
214 compare_posix_output "eval double expansion" "VAR='echo \$HOME'; eval \$VAR | grep -c /"
215 compare_posix_output "eval empty string" "eval ''; echo ok"
216 compare_posix_output "nested eval" "eval eval echo nested"
217
218 # ============================================================================
219 # READONLY AND UNSET
220 # ============================================================================
221
222 section "14. READONLY"
223 compare_posix_exit_code "readonly then unset fails" "readonly X=1; unset X 2>/dev/null"
224 compare_posix_exit_code "export readonly" "readonly X=1; export X; sh -c 'echo \$X' | grep -c 1"
225 compare_posix_output "readonly in subshell" "(readonly Y=2; echo \$Y); readonly | grep -c Y || echo 0"
226 compare_posix_output "readonly basic" 'readonly Y=5; echo $Y'
227 compare_posix_output "readonly list" 'readonly | grep -c .'
228
229 section "15. UNSET"
230 compare_posix_output "unset var" 'x=5; unset x; echo ${x:-unset}'
231 compare_posix_output "unset func" 'f() { echo f; }; unset -f f; f 2>/dev/null || echo unset'
232 compare_posix_output "unset v flag" 'x=5; unset -v x; echo ${x:-unset}'
233
234 # ============================================================================
235 # EXPORT
236 # ============================================================================
237
238 section "16. EXPORT"
239 compare_posix_output "export basic" 'export X=5; sh -c "echo \$X"'
240 compare_posix_output "export list" 'export | grep -c ='
241
242 # ============================================================================
243 # RETURN AND DOT
244 # ============================================================================
245
246 section "17. RETURN BUILTIN"
247 compare_posix_output "return without function" "return 2>/dev/null || echo ok"
248 compare_posix_output "return value preserved" "f() { return 42; }; f; echo \$?"
249 compare_posix_output "return in sourced script" "echo 'return 7' > /tmp/posix_gaps_source_$$; . /tmp/posix_gaps_source_$$ 2>/dev/null || echo \$?; rm -f /tmp/posix_gaps_source_$$"
250
251 section "18. DOT BUILTIN"
252 compare_posix_output "source with PATH search" "echo 'echo sourced' > /tmp/posix_gaps_dot_$$; PATH=/tmp:\$PATH; . posix_gaps_dot_$$ 2>/dev/null || echo 'not found'; rm -f /tmp/posix_gaps_dot_$$"
253 compare_posix_exit_code "source nonexistent" ". /tmp/posix_gaps_nonexistent_$$ 2>/dev/null"
254 compare_posix_output "source preserves variables" "echo 'A=from_source' > /tmp/posix_gaps_dot2_$$; . /tmp/posix_gaps_dot2_$$; echo \$A; rm -f /tmp/posix_gaps_dot2_$$"
255
256 # ============================================================================
257 # GETOPTS
258 # ============================================================================
259
260 section "19. GETOPTS"
261 compare_posix_output "getopts basic" "set -- -a test; getopts 'a:' opt; echo \$opt"
262 compare_posix_output "getopts OPTARG" "set -- -a value; getopts 'a:' opt; echo \$OPTARG"
263 compare_posix_output "getopts OPTIND" "set -- -a -b; getopts 'ab' opt; echo \$OPTIND"
264 compare_posix_output "getopts invalid option" "set -- -z; getopts 'ab' opt 2>/dev/null; echo \$opt | grep -c '?'"
265
266 # ============================================================================
267 # UMASK
268 # ============================================================================
269
270 section "20. UMASK"
271 compare_posix_output "umask get" "umask | grep -c '^[0-9]*\$'"
272 compare_posix_output "umask set and get" "old=\$(umask); umask 022; umask; umask \$old | head -1"
273
274 # ============================================================================
275 # HASH
276 # ============================================================================
277
278 section "21. HASH"
279 compare_posix_exit_code "hash command" "hash echo 2>/dev/null"
280 compare_posix_exit_code "hash -r clears" "hash -r"
281 compare_posix_exit_code "hash nonexistent" "hash nonexistent_cmd_$$ 2>/dev/null"
282
283 # ============================================================================
284 # TIMES
285 # ============================================================================
286
287 section "22. TIMES"
288 compare_posix_output "times output format" "times | wc -l"
289 compare_posix_exit_code "times exit status" "times >/dev/null"
290
291 # ============================================================================
292 # TRAP
293 # ============================================================================
294
295 section "23. TRAP"
296 compare_posix_output "trap with signal number" "trap 'echo sig' 15; trap | grep -c 15"
297 compare_posix_output "trap with multiple signals" "trap 'echo multi' INT TERM; trap | grep -c 'echo multi'"
298 compare_posix_output "trap ignore signal" "trap '' INT; trap | grep INT | grep -c ''"
299
300 # ============================================================================
301 # EXIT
302 # ============================================================================
303
304 section "24. EXIT"
305 compare_posix_exit_code "exit with status" "sh -c 'exit 42'"
306 compare_posix_exit_code "exit in subshell" "(exit 7); echo \$?"
307
308 # ============================================================================
309 # VARIABLE OPERATIONS
310 # ============================================================================
311
312 section "25. VARIABLE ASSIGNMENT"
313 compare_posix_output "var simple" 'x=5; echo $x'
314 compare_posix_output "var empty" 'x=; echo "[$x]"'
315 compare_posix_output "var quoted" 'x="a b"; echo "$x"'
316 compare_posix_output "var concat" 'x=hel; y=lo; echo $x$y'
317 compare_posix_output "var braces" 'x=val; echo ${x}'
318
319 section "26. FUNCTION SCOPE"
320 compare_posix_output "func global" 'x=global; f() { x=func; }; f; echo $x'
321 compare_posix_output "func params" 'f() { echo $# $1 $2; }; f a b c'
322 compare_posix_output "func shift" 'f() { shift; echo $1; }; f a b c'
323
324 # ============================================================================
325 # MISCELLANEOUS
326 # ============================================================================
327
328 section "27. MISCELLANEOUS EDGE CASES"
329 compare_posix_output "empty command in list" ": ; echo ok"
330 compare_posix_output "whitespace only" " ; echo ok"
331 compare_posix_output "multiple empty commands" ": ; : ; echo ok"
332 compare_posix_output "empty string as command" "'' 2>/dev/null || echo ok"
333
334 section "28. PIPELINES"
335 compare_posix_output "five stage pipeline" "echo test | cat | cat | cat | cat"
336 compare_posix_exit_code "pipeline with negation" "! false | false"
337 compare_posix_output "pipeline with subshell" "(echo a; echo b) | wc -l"
338 compare_posix_output "pipeline with brace group" "{ echo x; echo y; } | wc -l"
339
340 # Summary
341 printf "\n==========================================\n"
342 printf "BUILTINS GAP TEST RESULTS\n"
343 printf "==========================================\n"
344 printf "${GREEN}Passed:${NC} %d\n" "$PASSED"
345 printf "${RED}Failed:${NC} %d\n" "$FAILED"
346 printf "Total: %d\n" "$((PASSED + FAILED))"
347 if [ "$FAILED" -gt 0 ]; then
348 printf "\n${RED}Failed tests:${NC}\n%b" "$FAILED_TESTS_LIST"
349 exit 1
350 fi
351 exit 0