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

290 lines, 121 significant
1## compare_shells: dash bash mksh ash
2## oils_failures_allowed: 0
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#### trap 'echo hi' KILL (regression test, caught by smoosh suite)
15trap 'echo hi' 9
16echo status=$?
17
18trap 'echo hi' KILL
19echo status=$?
20
21trap 'echo hi' STOP
22echo status=$?
23
24trap 'echo hi' TERM
25echo status=$?
26
27## STDOUT:
28status=0
29status=0
30status=0
31status=0
32## END
33## OK osh STDOUT:
34status=1
35status=1
36status=1
37status=0
38## END
39
40#### Register invalid trap
41trap 'foo' SIGINVALID
42## status: 1
43
44#### Remove invalid trap
45trap - SIGINVALID
46## status: 1
47
48#### SIGINT and INT are aliases
49trap - SIGINT
50echo $?
51trap - INT
52echo $?
53## STDOUT:
540
550
56## END
57## N-I dash STDOUT:
581
590
60## END
61
62#### Invalid trap invocation
63trap 'foo'
64echo status=$?
65## STDOUT:
66status=2
67## END
68## OK dash/ash STDOUT:
69status=1
70## END
71## BUG mksh STDOUT:
72status=0
73## END
74
75#### exit 1 when trap code string is invalid
76# All shells spew warnings to stderr, but don't actually exit! Bad!
77trap 'echo <' EXIT
78echo status=$?
79## STDOUT:
80status=1
81## END
82
83## BUG mksh status: 1
84## BUG mksh STDOUT:
85status=0
86## END
87
88## BUG ash status: 2
89## BUG ash STDOUT:
90status=0
91## END
92
93## BUG dash/bash status: 0
94## BUG dash/bash STDOUT:
95status=0
96## END
97
98
99#### trap EXIT calling exit
100cleanup() {
101 echo "cleanup [$@]"
102 exit 42
103}
104trap 'cleanup x y z' EXIT
105## stdout: cleanup [x y z]
106## status: 42
107
108#### trap EXIT return status ignored
109cleanup() {
110 echo "cleanup [$@]"
111 return 42
112}
113trap 'cleanup x y z' EXIT
114## stdout: cleanup [x y z]
115## status: 0
116
117#### trap EXIT with PARSE error
118trap 'echo FAILED' EXIT
119for
120## stdout: FAILED
121## status: 2
122## OK mksh status: 1
123
124#### trap EXIT with PARSE error and explicit exit
125trap 'echo FAILED; exit 0' EXIT
126for
127## stdout: FAILED
128## status: 0
129
130#### trap EXIT with explicit exit
131trap 'echo IN TRAP; echo $stdout' EXIT
132stdout=FOO
133exit 42
134
135## status: 42
136## STDOUT:
137IN TRAP
138FOO
139## END
140
141#### trap EXIT with command sub / subshell / pipeline
142trap 'echo EXIT TRAP' EXIT
143
144echo $(echo command sub)
145
146( echo subshell )
147
148echo pipeline | cat
149
150## STDOUT:
151command sub
152subshell
153pipeline
154EXIT TRAP
155## END
156
157#### trap 0 is equivalent to EXIT
158# not sure why this is, but POSIX wants it.
159trap 'echo EXIT' 0
160echo status=$?
161trap - EXIT
162echo status=$?
163## status: 0
164## STDOUT:
165status=0
166status=0
167## END
168
169#### trap 1 is equivalent to SIGHUP; HUP is equivalent to SIGHUP
170trap 'echo HUP' SIGHUP
171echo status=$?
172trap 'echo HUP' HUP
173echo status=$?
174trap 'echo HUP' 1
175echo status=$?
176trap - HUP
177echo status=$?
178## status: 0
179## STDOUT:
180status=0
181status=0
182status=0
183status=0
184## END
185## N-I dash STDOUT:
186status=1
187status=0
188status=0
189status=0
190## END
191
192#### eval in the exit trap (regression for issue #293)
193trap 'eval "echo hi"' 0
194## STDOUT:
195hi
196## END
197
198
199#### exit codes for traps are isolated
200
201trap 'echo USR1 trap status=$?; ( exit 42 )' USR1
202
203echo before=$?
204
205# Equivalent to 'kill -USR1 $$' except OSH doesn't have "kill" yet.
206# /bin/kill doesn't exist on Debian unless 'procps' is installed.
207sh -c "kill -USR1 $$"
208echo after=$?
209
210## STDOUT:
211before=0
212USR1 trap status=0
213after=0
214## END
215
216#### traps are cleared in subshell (started with &)
217
218# Test with SIGURG because the default handler is SIG_IGN
219#
220# If we use SIGUSR1, I think the shell reverts to killing the process
221
222# https://man7.org/linux/man-pages/man7/signal.7.html
223
224trap 'echo SIGURG' URG
225
226kill -URG $$
227
228# Hm trap doesn't happen here
229{ echo begin child; sleep 0.1; echo end child; } &
230kill -URG $!
231wait
232echo "wait status $?"
233
234# In the CI, mksh sometimes gives:
235#
236# USR1
237# begin child
238# done
239#
240# leaving off 'end child'. This seems like a BUG to me?
241
242## STDOUT:
243SIGURG
244begin child
245end child
246wait status 0
247## END
248
249#### trap USR1, sleep, SIGINT: non-interactively
250
251$REPO_ROOT/spec/testdata/builtin-trap-usr1.sh
252
253## STDOUT:
254usr1
255status=0
256## END
257
258#### trap INT, sleep, SIGINT: non-interactively
259
260# mksh behaves differently in CI -- maybe when it's not connected to a
261# terminal?
262case $SH in mksh) echo mksh; exit ;; esac
263
264$REPO_ROOT/spec/testdata/builtin-trap-int.sh
265
266## STDOUT:
267status=0
268## END
269
270## OK mksh STDOUT:
271mksh
272## END
273
274# Not sure why other shells differ here, but running the trap is consistent
275# with interactive cases in test/bugs.sh
276
277## OK osh STDOUT:
278int
279status=0
280## END
281
282#### trap EXIT, sleep, SIGINT: non-interactively
283
284$REPO_ROOT/spec/testdata/builtin-trap-exit.sh
285
286## STDOUT:
287on exit
288status=0
289## END
290