/
runtime-errors.sh
executable file
·426 lines (331 loc) · 6.79 KB
/
runtime-errors.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env bash
#
# Usage:
# $SH ./runtime-errors.sh all
#
# Run with bash/dash/mksh/zsh.
source test/common.sh
#
# PARSE ERRORS
#
source_bad_syntax() {
cat >_tmp/bad-syntax.sh <<EOF
if foo; echo ls; fi
EOF
. _tmp/bad-syntax.sh
}
# NOTE:
# - bash correctly reports line 25 (24 would be better)
# - mksh: no line number
# - zsh: line 2 of eval, which doesn't really help.
# - dash: ditto, line 2 of eval
eval_bad_syntax() {
local code='if foo; echo ls; fi'
eval "echo --
$code"
}
#
# COMMAND ERRORS
#
no_such_command() {
set -o errexit
ZZZZZ
echo 'SHOULD NOT GET HERE'
}
no_such_command_commandsub() {
set -o errexit
echo $(ZZZZZ)
echo 'SHOULD NOT GET HERE'
}
no_such_command_heredoc() {
set -o errexit
# Note: bash gives the line of the beginning of the here doc! Not the actual
# line.
# TODO: osh doesn't give any psition info.
cat <<EOF
one
$(ZZZZZ)
three
EOF
echo 'SHOULD NOT GET HERE'
}
failed_command() {
set -o errexit
false
echo 'SHOULD NOT GET HERE'
}
errexit() {
set -o errexit
rm nonexistent/file
echo 'SHOULD NOT GET HERE'
}
pipefail() {
false | wc -l
set -o errexit
set -o pipefail
false | wc -l
echo 'SHOULD NOT GET HERE'
}
pipefail_func() {
set -o errexit -o pipefail
f() {
cat
# NOTE: If you call 'exit 42', there is no error message displayed!
#exit 42
return 42
}
echo hi | f | wc
echo 'SHOULD NOT GET HERE'
}
# TODO: point to {. It's the same sas a subshell so you don't know exactly
# which command failed.
pipefail_group() {
set -o errexit -o pipefail
echo hi | { cat; sh -c 'exit 42'; } | wc
echo 'SHOULD NOT GET HERE'
}
# TODO: point to (
pipefail_subshell() {
set -o errexit -o pipefail
echo hi | (cat; sh -c 'exit 42') | wc
echo 'SHOULD NOT GET HERE'
}
# TODO: point to 'while'
pipefail_while() {
set -o errexit -o pipefail
seq 3 | while true; do
read line
echo X $line X
if test "$line" = 2; then
sh -c 'exit 42'
fi
done | wc
echo 'SHOULD NOT GET HERE'
}
# Multiple errors from multiple processes
pipefail_multiple() {
set -o errexit -o pipefail
{ echo 'four'; sh -c 'exit 4'; } |
{ echo 'five'; sh -c 'exit 5'; } |
{ echo 'six'; sh -c 'exit 6'; }
}
# NOTE: This prints a WARNING in bash. Not fatal in any shell except zsh.
control_flow() {
break
continue
echo 'SHOULD NOT GET HERE'
}
# Errors from core/process.py
core_process() {
echo foo > not/a/file
echo foo > /etc/no-perms-for-this
echo hi 1>&3
}
ambiguous_redirect() {
echo foo > "$@"
echo 'ambiguous redirect not fatal unless errexit'
set -o errexit
echo foo > "$@"
echo 'should not get here'
}
# bash semantics.
ambiguous_redirect_context() {
# Problem: A WORD cannot fail. Only a COMMAND can fail.
# http://stackoverflow.com/questions/29532904/bash-subshell-errexit-semantics
# https://groups.google.com/forum/?fromgroups=#!topic/gnu.bash.bug/NCK_0GmIv2M
# http://unix.stackexchange.com/questions/23026/how-can-i-get-bash-to-exit-on-backtick-failure-in-a-similar-way-to-pipefail
echo $(echo hi > "$@")
echo 'ambiguous is NOT FATAL in command sub'
echo
foo=$(echo hi > "$@")
echo $foo
echo 'ambiguous is NOT FATAL in assignment in command sub'
echo
set -o errexit
# This is strict-errexit!
echo $(echo hi > "$@")
echo 'ambiguous is NOT FATAL in command sub, even if errexit'
echo
# OK this one works. Because the exit code of the assignment is the exit
# code of the RHS?
echo 'But when the command sub is in an assignment, it is fatal'
foo=$(echo hi > "$@")
echo $foo
echo 'SHOULD NOT REACH HERE'
}
#
# WORD ERRORS
#
nounset() {
set -o nounset
echo $x
echo 'SHOULD NOT GET HERE'
}
bad_var_ref() {
name='bad var name'
echo ${!name}
}
#
# ARITHMETIC ERRORS
#
nounset_arith() {
set -o nounset
echo $(( x ))
echo 'SHOULD NOT GET HERE'
}
divzero() {
echo $(( 1 / 0 ))
echo 'SHOULD NOT GET HERE'
}
divzero_var() {
local zero=0
echo $(( 1 / zero ))
echo 'SHOULD NOT GET HERE'
}
divzero_dparen() {
(( 1 / 0 ))
echo 'Divide by zero in dparen is non-fatal unless errexit!'
set -o errexit
(( 1 / 0 ))
echo 'SHOULD NOT GET HERE'
}
# Only dash flags this as an error.
string_to_int_arith() {
local x='ZZZ'
echo $(( x + 5 ))
set -o strict-arith
echo $(( x + 5 ))
echo 'SHOULD NOT GET HERE'
}
# Hm bash treats this as a fatal error
string_to_hex() {
echo $(( 0xGG + 1 ))
echo 'SHOULD NOT GET HERE'
}
# Hm bash treats this as a fatal error
string_to_octal() {
echo $(( 018 + 1 ))
echo 'SHOULD NOT GET HERE'
}
# Hm bash treats this as a fatal error
string_to_intbase() {
echo $(( 16#GG ))
echo 'SHOULD NOT GET HERE'
}
undef_arith() {
(( undef++ )) # doesn't make sense
# Can't assign to characters of string? Is that strong?
(( undef[42]++ ))
}
undef_arith2() {
# undefined cell
a=()
(( a[42]++ ))
}
array_arith() {
a=(1 2)
(( a++ )) # doesn't make sense
echo "${a[@]}"
}
patsub_bad_glob() {
local x='abc'
# inspired by git-completion.bash
echo ${x//[^]}
}
#
# Builtins
#
test_builtin() {
# xxx is not a valid file descriptor
[ -t xxx ]
}
#
# BOOLEAN ERRORS
#
# Only osh cares about this.
string_to_int_bool() {
[[ a -eq 0 ]]
set -o strict-arith
[[ a -eq 0 ]]
echo 'SHOULD NOT GET HERE'
}
strict_array() {
set -- 1 2
echo foo > _tmp/"$@"
set -o strict-array
echo foo > _tmp/"$@"
}
strict_array_2() {
local foo="$@"
set -o strict-array
local foo="$@"
}
strict_array_3() {
local foo=${1:- "[$@]" }
set -o strict-array
local foo=${1:- "[$@]" }
}
strict_array_4() {
local -a x
x[42]=99
echo "x[42] = ${x[42]}"
set -o strict-array
local -a y
y[42]=99
}
array_assign_1() {
s=1
s[0]=x # can't assign value
}
array_assign_2() {
readonly -a array=(1 2 3)
array[0]=x
}
# Need location info
readonly_assign() {
readonly x=1
readonly x=2
}
#
# BUILTINS
#
builtin_builtin() {
set +o errexit
builtin ls
}
#
# TEST DRIVER
#
_run_test() {
local t=$1
echo "--------"
echo " CASE: $t"
# Run in subshell so the whole thing doesn't exit
( $t )
echo " STATUS: $?"
echo
}
all() {
# Can't be done inside a loop!
_run_test control_flow
for t in \
no_such_command no_such_command_commandsub no_such_command_heredoc \
failed_command errexit \
pipefail pipefail_group pipefail_subshell pipefail_func pipefail_while \
pipefail_multiple core_process \
nonexistent nounset bad_var_ref \
nounset_arith divzero divzero_var array_arith undef_arith undef_arith2 \
string_to_int_arith string_to_hex string_to_octal \
string_to_intbase string_to_int_bool \
array_assign_1 array_assign_2 readonly_assign patsub_bad_glob \
builtin_builtin; do
_run_test $t
done
}
run-all-with-osh() {
bin/osh $0 all
}
run-for-release() {
run-other-suite-for-release runtime-errors run-all-with-osh
}
"$@"