OILS / spec / builtin-trap-bash.test.sh View on Github | oilshell.org

550 lines, 342 significant
1## oils_failures_allowed: 1
2## compare_shells: bash
3
4# Notes on bash semantics:
5
6# https://www.gnu.org/software/bash/manual/bash.html
7#
8# Commands specified with a DEBUG trap are executed before every simple
9# command, for command, case command, select command, every arithmetic for
10# command, and before the first command executes in a shell function. The DEBUG
11# trap is not inherited by shell functions unless the function has been given
12# the trace attribute or the functrace option has been enabled using the shopt
13# builtin. The extdebug shell option has additional effects on the DEBUG trap.
14
15#### trap -l
16trap -l | grep INT >/dev/null
17## status: 0
18
19
20#### trap -p
21
22trap 'echo exit' EXIT
23
24trap -p > parent.txt
25
26grep EXIT parent.txt >/dev/null
27if test $? -eq 0; then
28 echo shown
29else
30 echo not shown
31fi
32
33## STDOUT:
34shown
35exit
36## END
37
38#### trap -p in child is BUGGY in bash
39
40# It shows the trap even though it doesn't execute it!
41
42trap 'echo exit' EXIT
43
44trap -p | cat > child.txt
45
46grep EXIT child.txt >/dev/null
47if test $? -eq 0; then
48 echo shown
49else
50 echo not shown
51fi
52
53## STDOUT:
54not shown
55exit
56## END
57## BUG bash STDOUT:
58shown
59exit
60## END
61
62#### trap DEBUG ignores $?
63debuglog() {
64 echo " [$@]"
65 return 42 # IGNORED FAILURE
66}
67
68trap 'debuglog $LINENO' DEBUG
69
70echo status=$?
71echo A
72echo status=$?
73echo B
74echo status=$?
75
76## STDOUT:
77 [8]
78status=0
79 [9]
80A
81 [10]
82status=0
83 [11]
84B
85 [12]
86status=0
87## END
88
89#### but trap DEBUG respects errexit
90set -o errexit
91
92debuglog() {
93 echo " [$@]"
94 return 42
95}
96
97trap 'debuglog $LINENO' DEBUG
98
99echo status=$?
100echo A
101echo status=$?
102echo B
103echo status=$?
104
105## status: 42
106## STDOUT:
107 [10]
108## END
109
110#### trap DEBUG with 'return'
111
112debuglog() {
113 echo " [$@]"
114}
115
116trap 'debuglog $LINENO; return 42' DEBUG
117
118echo status=$?
119echo A
120echo status=$?
121echo B
122echo status=$?
123
124## status: 0
125
126## STDOUT:
127 [7]
128status=0
129 [8]
130A
131 [9]
132status=0
133 [10]
134B
135 [11]
136status=0
137## END
138
139# OSH doesn't ignore this
140
141## OK osh status: 42
142## OK osh STDOUT:
143 [7]
144## END
145
146#### trap DEBUG with 'exit'
147debuglog() {
148 echo " [$@]"
149}
150
151trap 'debuglog $LINENO; exit 42' DEBUG
152
153echo status=$?
154echo A
155echo status=$?
156echo B
157echo status=$?
158
159## status: 42
160## STDOUT:
161 [7]
162## END
163
164
165
166#### trap DEBUG with non-compound commands
167case $SH in (dash|mksh) exit ;; esac
168
169debuglog() {
170 echo " [$@]"
171}
172trap 'debuglog $LINENO' DEBUG
173
174echo a
175echo b; echo c
176
177echo d && echo e
178echo f || echo g
179
180(( h = 42 ))
181[[ j == j ]]
182
183var=value
184
185readonly r=value
186
187## STDOUT:
188 [8]
189a
190 [9]
191b
192 [9]
193c
194 [11]
195d
196 [11]
197e
198 [12]
199f
200 [14]
201 [15]
202 [17]
203 [19]
204## END
205
206#### trap DEBUG and control flow
207
208debuglog() {
209 echo " [$@]"
210}
211trap 'debuglog $LINENO' DEBUG
212
213while true; do
214 echo hello
215 break
216done
217
218## STDOUT:
219 [6]
220 [7]
221hello
222 [8]
223## END
224## STDOUT:
225 [6]
226 [7]
227hello
228 [8]
229## END
230
231#### trap DEBUG and command sub / subshell
232case $SH in (dash|mksh) exit ;; esac
233
234debuglog() {
235 echo " [$@]"
236}
237trap 'debuglog $LINENO' DEBUG
238
239echo "result = $(echo command sub; echo two)"
240( echo subshell
241 echo two
242)
243echo done
244
245## STDOUT:
246 [8]
247result = command sub
248two
249subshell
250two
251 [12]
252done
253## END
254
255#### trap DEBUG not run in forked interpreter for first pipeline part
256
257debuglog() {
258 #echo " PID=$$ BASHPID=$BASHPID LINENO=$1"
259 echo " LINENO=$1"
260}
261trap 'debuglog $LINENO' DEBUG
262
263{ echo pipe1;
264 echo pipe2; } \
265 | cat
266echo ok
267
268## STDOUT:
269 LINENO=8
270pipe1
271pipe2
272 LINENO=9
273ok
274## END
275
276#### One 'echo' in first pipeline part - why does bash behave differently from case above?
277
278# TODO: bash runs the trap 3 times, and osh only twice. I don't see why. Is
279# it because Process::Run() does trap_state.ClearForSubProgram()? Probably
280#echo top PID=$$ BASHPID=$BASHPID
281#shopt -s lastpipe
282
283debuglog() {
284 #echo " PID=$$ BASHPID=$BASHPID LINENO=$1"
285 #echo " LINENO=$1 $BASH_COMMAND"
286 # LINENO=6 echo pipeline
287 # LINENO=7 cat
288 echo " LINENO=$1"
289}
290trap 'debuglog $LINENO' DEBUG
291
292echo pipeline \
293 | cat
294echo ok
295
296## STDOUT:
297 LINENO=6
298 LINENO=7
299pipeline
300 LINENO=8
301ok
302## END
303## OK osh STDOUT:
304 LINENO=7
305pipeline
306 LINENO=8
307ok
308## END
309
310#### trap DEBUG and pipeline (lastpipe difference)
311debuglog() {
312 echo " [$@]"
313}
314trap 'debuglog $LINENO' DEBUG
315
316# gets run for each one of these
317{ echo a; echo b; }
318
319# only run for the last one, maybe I guess because traps aren't inherited?
320{ echo x; echo y; } | wc -l
321
322# bash runs for all of these, but OSH doesn't because we have SubProgramThunk
323# Hm.
324date | cat | wc -l
325
326date |
327 cat |
328 wc -l
329
330## STDOUT:
331 [6]
332a
333 [6]
334b
335 [8]
3362
337 [10]
338 [10]
339 [10]
3401
341 [12]
342 [13]
343 [14]
3441
345## END
346
347# Marking OK due to lastpipe execution difference
348
349## OK osh STDOUT:
350 [6]
351a
352 [6]
353b
354 [8]
3552
356 [10]
3571
358 [14]
3591
360## END
361
362#### trap DEBUG function call
363debuglog() {
364 echo " [$@]"
365}
366trap 'debuglog $LINENO' DEBUG
367
368f() {
369 local mylocal=1
370 for i in "$@"; do
371 echo i=$i
372 done
373}
374
375f A B # executes ONCE here, but does NOT go into the function call
376
377echo next
378
379f X Y
380
381echo ok
382
383## STDOUT:
384 [13]
385i=A
386i=B
387 [15]
388next
389 [17]
390i=X
391i=Y
392 [19]
393ok
394## END
395
396#### trap DEBUG case
397debuglog() {
398 echo " [$@]"
399}
400trap 'debuglog $LINENO' DEBUG
401
402name=foo.py
403
404case $name in
405 *.py)
406 echo python
407 ;;
408 *.sh)
409 echo shell
410 ;;
411esac
412echo ok
413
414## STDOUT:
415 [6]
416 [8]
417 [10]
418python
419 [16]
420ok
421## END
422
423#### trap DEBUG for each
424
425debuglog() {
426 echo " [$@]"
427}
428trap 'debuglog $LINENO' DEBUG
429
430for x in 1 2; do
431 echo x=$x
432done
433
434echo ok
435
436## STDOUT:
437 [6]
438 [7]
439x=1
440 [6]
441 [7]
442x=2
443 [10]
444ok
445## END
446
447# NOT matching bash right now because 'while' loops don't have it
448# And we have MORE LOOPS
449#
450# What we really need is a trap that runs in the main loop and TELLS you what
451# kind of node it is?
452
453## N-I osh STDOUT:
454 [7]
455x=1
456 [7]
457x=2
458 [10]
459ok
460## END
461
462#### trap DEBUG for expr
463debuglog() {
464 echo " [$@]"
465}
466trap 'debuglog $LINENO' DEBUG
467
468for (( i =3 ; i < 5; ++i )); do
469 echo i=$i
470done
471
472echo ok
473
474## STDOUT:
475 [6]
476 [6]
477 [7]
478i=3
479 [6]
480 [6]
481 [7]
482i=4
483 [6]
484 [6]
485 [10]
486ok
487## END
488## N-I osh STDOUT:
489 [7]
490i=3
491 [7]
492i=4
493 [10]
494ok
495## END
496
497#### trap DEBUG if while
498debuglog() {
499 echo " [$@]"
500}
501trap 'debuglog $LINENO' DEBUG
502
503if test x = x; then
504 echo if
505fi
506
507while test x != x; do
508 echo while
509done
510
511## STDOUT:
512 [6]
513 [7]
514if
515 [10]
516## END
517
518
519#### trap RETURN
520profile() {
521 echo "profile [$@]"
522}
523g() {
524 echo --
525 echo g
526 echo --
527 return
528}
529f() {
530 echo --
531 echo f
532 echo --
533 g
534}
535# RETURN trap doesn't fire when a function returns, only when a script returns?
536# That's not what the manual says.
537trap 'profile x y' RETURN
538f
539. $REPO_ROOT/spec/testdata/return-helper.sh
540## status: 42
541## STDOUT:
542--
543f
544--
545--
546g
547--
548return-helper.sh
549profile [x y]
550## END