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

308 lines, 127 significant
1## compare_shells: dash bash mksh ash
2## oils_failures_allowed: 2
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=$?
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 EXIT doesn't run with shopt -s no_fork_last
158
159# There doesn't seem to be a way to get it to run, so specify that it doesn't
160
161$SH -c 'trap "echo exit1" EXIT; /bin/true'
162
163# newline
164$SH -c 'trap "echo exit2" EXIT; /bin/true
165'
166
167# Newline makes a difference!
168# It doesn't get a chance to run
169$SH -c 'shopt -s no_fork_last
170trap "echo exit3" EXIT; /bin/true'
171
172## STDOUT:
173exit1
174exit2
175## END
176
177## N-I dash/bash/mksh/ash STDOUT:
178exit1
179exit2
180exit3
181## END
182
183#### trap 0 is equivalent to EXIT
184# not sure why this is, but POSIX wants it.
185trap 'echo EXIT' 0
186echo status=$?
187trap - EXIT
188echo status=$?
189## status: 0
190## STDOUT:
191status=0
192status=0
193## END
194
195#### trap 1 is equivalent to SIGHUP; HUP is equivalent to SIGHUP
196trap 'echo HUP' SIGHUP
197echo status=$?
198trap 'echo HUP' HUP
199echo status=$?
200trap 'echo HUP' 1
201echo status=$?
202trap - HUP
203echo status=$?
204## status: 0
205## STDOUT:
206status=0
207status=0
208status=0
209status=0
210## END
211## N-I dash STDOUT:
212status=1
213status=0
214status=0
215status=0
216## END
217
218#### eval in the exit trap (regression for issue #293)
219trap 'eval "echo hi"' 0
220## STDOUT:
221hi
222## END
223
224
225#### exit codes for traps are isolated
226
227trap 'echo USR1 trap status=$?; ( exit 42 )' USR1
228
229echo before=$?
230
231# Equivalent to 'kill -USR1 $$' except OSH doesn't have "kill" yet.
232# /bin/kill doesn't exist on Debian unless 'procps' is installed.
233sh -c "kill -USR1 $$"
234echo after=$?
235
236## STDOUT:
237before=0
238USR1 trap status=0
239after=0
240## END
241
242#### traps are cleared in subshell (started with &)
243
244# Test with SIGURG because the default handler is SIG_IGN
245#
246# If we use SIGUSR1, I think the shell reverts to killing the process
247
248# https://man7.org/linux/man-pages/man7/signal.7.html
249
250trap 'echo SIGURG' URG
251
252kill -URG $$
253
254# Hm trap doesn't happen here
255{ echo begin child; sleep 0.1; echo end child; } &
256kill -URG $!
257wait
258echo "wait status $?"
259
260# In the CI, mksh sometimes gives:
261#
262# USR1
263# begin child
264# done
265#
266# leaving off 'end child'. This seems like a BUG to me?
267
268## STDOUT:
269SIGURG
270begin child
271end child
272wait status 0
273## END
274
275#### trap USR1, sleep, SIGINT: non-interactively
276
277$REPO_ROOT/spec/testdata/builtin-trap-usr1.sh
278
279## STDOUT:
280usr1
281status=0
282## END
283
284#### trap INT, sleep, SIGINT: non-interactively
285
286# mksh behaves differently in CI -- maybe when it's not connected to a
287# terminal?
288case $SH in mksh) echo mksh; exit ;; esac
289
290$REPO_ROOT/spec/testdata/builtin-trap-int.sh
291
292## STDOUT:
293status=0
294## END
295
296## OK mksh STDOUT:
297mksh
298## END
299
300#### trap EXIT, sleep, SIGINT: non-interactively
301
302$REPO_ROOT/spec/testdata/builtin-trap-exit.sh
303
304## STDOUT:
305on exit
306status=0
307## END
308