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

223 lines, 102 significant
1## compare_shells: dash bash mksh
2## oils_failures_allowed: 0
3
4# builtin-trap.test.sh
5
6#### trap accepts/ignores --
7trap -- 'echo hi' EXIT
8echo done
9## STDOUT:
10done
11hi
12## END
13
14#### trap 'echo hi' KILL (regression test, caught by smoosh suite)
15trap 'echo hi' 9
16echo status=$?
17trap 'echo hi' KILL
18echo status=$?
19trap 'echo hi' STOP
20echo status=$?
21trap 'echo hi' TERM
22echo status=$?
23## STDOUT:
24status=0
25status=0
26status=0
27status=0
28## END
29## OK osh STDOUT:
30status=1
31status=1
32status=1
33status=0
34## END
35
36#### Register invalid trap
37trap 'foo' SIGINVALID
38## status: 1
39
40#### Remove invalid trap
41trap - SIGINVALID
42## status: 1
43
44#### SIGINT and INT are aliases
45trap - SIGINT
46echo $?
47trap - INT
48echo $?
49## STDOUT:
500
510
52## END
53## N-I dash STDOUT:
541
550
56## END
57
58#### Invalid trap invocation
59trap 'foo'
60echo status=$?
61## stdout: status=2
62## OK dash stdout: status=1
63## BUG mksh stdout: status=0
64
65#### exit 1 when trap code string is invalid
66# All shells spew warnings to stderr, but don't actually exit! Bad!
67trap 'echo <' EXIT
68echo status=$?
69## stdout: status=1
70## BUG mksh status: 1
71## BUG mksh stdout: status=0
72## BUG dash/bash status: 0
73## BUG dash/bash stdout: status=0
74
75#### trap EXIT calling exit
76cleanup() {
77 echo "cleanup [$@]"
78 exit 42
79}
80trap 'cleanup x y z' EXIT
81## stdout: cleanup [x y z]
82## status: 42
83
84#### trap EXIT return status ignored
85cleanup() {
86 echo "cleanup [$@]"
87 return 42
88}
89trap 'cleanup x y z' EXIT
90## stdout: cleanup [x y z]
91## status: 0
92
93#### trap EXIT with PARSE error
94trap 'echo FAILED' EXIT
95for
96## stdout: FAILED
97## status: 2
98## OK mksh status: 1
99
100#### trap EXIT with PARSE error and explicit exit
101trap 'echo FAILED; exit 0' EXIT
102for
103## stdout: FAILED
104## status: 0
105
106#### trap EXIT with explicit exit
107trap 'echo IN TRAP; echo $stdout' EXIT
108stdout=FOO
109exit 42
110
111## status: 42
112## STDOUT:
113IN TRAP
114FOO
115## END
116
117#### trap EXIT with command sub / subshell / pipeline
118trap 'echo EXIT TRAP' EXIT
119
120echo $(echo command sub)
121
122( echo subshell )
123
124echo pipeline | cat
125
126## STDOUT:
127command sub
128subshell
129pipeline
130EXIT TRAP
131## END
132
133#### trap 0 is equivalent to EXIT
134# not sure why this is, but POSIX wants it.
135trap 'echo EXIT' 0
136echo status=$?
137trap - EXIT
138echo status=$?
139## status: 0
140## STDOUT:
141status=0
142status=0
143## END
144
145#### trap 1 is equivalent to SIGHUP; HUP is equivalent to SIGHUP
146trap 'echo HUP' SIGHUP
147echo status=$?
148trap 'echo HUP' HUP
149echo status=$?
150trap 'echo HUP' 1
151echo status=$?
152trap - HUP
153echo status=$?
154## status: 0
155## STDOUT:
156status=0
157status=0
158status=0
159status=0
160## END
161## N-I dash STDOUT:
162status=1
163status=0
164status=0
165status=0
166## END
167
168#### eval in the exit trap (regression for issue #293)
169trap 'eval "echo hi"' 0
170## STDOUT:
171hi
172## END
173
174
175#### exit codes for traps are isolated
176
177trap 'echo USR1 trap status=$?; ( exit 42 )' USR1
178
179echo before=$?
180
181# Equivalent to 'kill -USR1 $$' except OSH doesn't have "kill" yet.
182# /bin/kill doesn't exist on Debian unless 'procps' is installed.
183sh -c "kill -USR1 $$"
184echo after=$?
185
186## STDOUT:
187before=0
188USR1 trap status=0
189after=0
190## END
191
192#### traps are cleared in subshell (started with &)
193
194# Test with SIGURG because the default handler is SIG_IGN
195#
196# If we use SIGUSR1, I think the shell reverts to killing the process
197
198# https://man7.org/linux/man-pages/man7/signal.7.html
199
200trap 'echo SIGURG' URG
201
202kill -URG $$
203
204# Hm trap doesn't happen here
205{ echo begin child; sleep 0.1; echo end child; } &
206kill -URG $!
207wait
208echo "wait status $?"
209
210# In the CI, mksh sometimes gives:
211#
212# USR1
213# begin child
214# done
215#
216# leaving off 'end child'. This seems like a BUG to me?
217
218## STDOUT:
219SIGURG
220begin child
221end child
222wait status 0
223## END