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

366 lines, 147 significant
1## compare_shells: dash bash mksh ash
2## oils_failures_allowed: 1
3
4# builtin-trap.test.sh
5
6#### trap accepts/ignores --
7trap -- 'echo hi' EXIT
8echo ok
9## STDOUT:
10ok
11hi
12## END
13
14#### Register invalid trap
15trap 'foo' SIGINVALID
16## status: 1
17
18#### Remove invalid trap
19trap - SIGINVALID
20## status: 1
21
22#### SIGINT and INT are aliases
23trap - SIGINT
24echo $?
25trap - INT
26echo $?
27## STDOUT:
280
290
30## END
31## N-I dash STDOUT:
321
330
34## END
35
36#### trap without args prints traps, like trap -p
37case $SH in dash) exit ;; esac
38
39if false; then
40 # bash breaks the display across lines
41 trap "true
42false" EXIT
43fi
44
45$SH -c '
46
47trap "true" EXIT
48
49echo status=$?
50trap | grep EXIT
51echo status=$?
52'
53
54## STDOUT:
55status=0
56trap -- 'true' EXIT
57status=0
58## END
59
60## BUG mksh/ash STDOUT:
61status=0
62status=1
63## END
64
65## N-I dash STDOUT:
66## END
67
68
69#### trap 'echo hi' KILL (regression test, caught by smoosh suite)
70trap 'echo hi' 9
71echo status=$?
72
73trap 'echo hi' KILL
74echo status=$?
75
76trap 'echo hi' STOP
77echo status=$?
78
79trap 'echo hi' TERM
80echo status=$?
81
82## STDOUT:
83status=0
84status=0
85status=0
86status=0
87## END
88## OK osh STDOUT:
89status=1
90status=1
91status=1
92status=0
93## END
94
95#### Invalid trap invocation
96trap 'foo'
97echo status=$?
98## STDOUT:
99status=2
100## END
101## OK dash/ash STDOUT:
102status=1
103## END
104## BUG mksh STDOUT:
105status=0
106## END
107
108#### exit 1 when trap code string is invalid
109# All shells spew warnings to stderr, but don't actually exit! Bad!
110trap 'echo <' EXIT
111echo status=$?
112## STDOUT:
113status=1
114## END
115
116## BUG mksh status: 1
117## BUG mksh STDOUT:
118status=0
119## END
120
121## BUG ash status: 2
122## BUG ash STDOUT:
123status=0
124## END
125
126## BUG dash/bash status: 0
127## BUG dash/bash STDOUT:
128status=0
129## END
130
131
132#### trap EXIT calling exit
133cleanup() {
134 echo "cleanup [$@]"
135 exit 42
136}
137trap 'cleanup x y z' EXIT
138## stdout: cleanup [x y z]
139## status: 42
140
141#### trap EXIT return status ignored
142cleanup() {
143 echo "cleanup [$@]"
144 return 42
145}
146trap 'cleanup x y z' EXIT
147## stdout: cleanup [x y z]
148## status: 0
149
150#### trap EXIT with PARSE error
151trap 'echo FAILED' EXIT
152for
153## stdout: FAILED
154## status: 2
155## OK mksh status: 1
156
157#### trap EXIT with PARSE error and explicit exit
158trap 'echo FAILED; exit 0' EXIT
159for
160## stdout: FAILED
161## status: 0
162
163#### trap EXIT with explicit exit
164trap 'echo IN TRAP; echo $stdout' EXIT
165stdout=FOO
166exit 42
167
168## status: 42
169## STDOUT:
170IN TRAP
171FOO
172## END
173
174#### trap EXIT with command sub / subshell / pipeline
175trap 'echo EXIT TRAP' EXIT
176
177echo $(echo command sub)
178
179( echo subshell )
180
181echo pipeline | cat
182
183## STDOUT:
184command sub
185subshell
186pipeline
187EXIT TRAP
188## END
189
190#### trap 0 is equivalent to EXIT
191# not sure why this is, but POSIX wants it.
192trap 'echo EXIT' 0
193echo status=$?
194trap - EXIT
195echo status=$?
196## status: 0
197## STDOUT:
198status=0
199status=0
200## END
201
202#### trap 1 is equivalent to SIGHUP; HUP is equivalent to SIGHUP
203trap 'echo HUP' SIGHUP
204echo status=$?
205trap 'echo HUP' HUP
206echo status=$?
207trap 'echo HUP' 1
208echo status=$?
209trap - HUP
210echo status=$?
211## status: 0
212## STDOUT:
213status=0
214status=0
215status=0
216status=0
217## END
218## N-I dash STDOUT:
219status=1
220status=0
221status=0
222status=0
223## END
224
225#### eval in the exit trap (regression for issue #293)
226trap 'eval "echo hi"' 0
227## STDOUT:
228hi
229## END
230
231
232#### exit codes for traps are isolated
233
234trap 'echo USR1 trap status=$?; ( exit 42 )' USR1
235
236echo before=$?
237
238# Equivalent to 'kill -USR1 $$' except OSH doesn't have "kill" yet.
239# /bin/kill doesn't exist on Debian unless 'procps' is installed.
240sh -c "kill -USR1 $$"
241echo after=$?
242
243## STDOUT:
244before=0
245USR1 trap status=0
246after=0
247## END
248
249#### traps are cleared in subshell (started with &)
250
251# Test with SIGURG because the default handler is SIG_IGN
252#
253# If we use SIGUSR1, I think the shell reverts to killing the process
254
255# https://man7.org/linux/man-pages/man7/signal.7.html
256
257trap 'echo SIGURG' URG
258
259kill -URG $$
260
261# Hm trap doesn't happen here
262{ echo begin child; sleep 0.1; echo end child; } &
263kill -URG $!
264wait
265echo "wait status $?"
266
267# In the CI, mksh sometimes gives:
268#
269# USR1
270# begin child
271# done
272#
273# leaving off 'end child'. This seems like a BUG to me?
274
275## STDOUT:
276SIGURG
277begin child
278end child
279wait status 0
280## END
281
282#### trap USR1, sleep, SIGINT: non-interactively
283
284$REPO_ROOT/spec/testdata/builtin-trap-usr1.sh
285
286## STDOUT:
287usr1
288status=0
289## END
290
291#### trap INT, sleep, SIGINT: non-interactively
292
293# mksh behaves differently in CI -- maybe when it's not connected to a
294# terminal?
295case $SH in mksh) echo mksh; exit ;; esac
296
297$REPO_ROOT/spec/testdata/builtin-trap-int.sh
298
299## STDOUT:
300status=0
301## END
302
303## OK mksh STDOUT:
304mksh
305## END
306
307# Not sure why other shells differ here, but running the trap is consistent
308# with interactive cases in test/bugs.sh
309
310## OK osh STDOUT:
311int
312status=0
313## END
314
315#### trap EXIT, sleep, SIGINT: non-interactively
316
317$REPO_ROOT/spec/testdata/builtin-trap-exit.sh
318
319## STDOUT:
320on exit
321status=0
322## END
323
324#### Remove trap with an unsigned integer
325
326$SH -e -c '
327trap "echo noprint" EXIT
328trap 0 EXIT
329echo ok0
330'
331echo
332
333$SH -e -c '
334trap "echo noprint" EXIT
335trap " 42 " EXIT
336echo ok42space
337'
338echo
339
340# corner case: sometimes 07 is treated as octal, but not here
341$SH -e -c '
342trap "echo noprint" EXIT
343trap 07 EXIT
344echo ok07
345'
346echo
347
348$SH -e -c '
349trap "echo trap-exit" EXIT
350trap -1 EXIT
351echo bad
352'
353if test $? -ne 0; then
354 echo failure
355fi
356
357## STDOUT:
358ok0
359
360ok42space
361
362ok07
363
364trap-exit
365failure
366## END