-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
spec.sh
executable file
·661 lines (533 loc) · 13.9 KB
/
spec.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/env bash
#
# Usage:
# test/spec.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
shopt -s all:strict 2>/dev/null || true # dogfood for OSH
source test/common.sh
# For now, fall back to the shell in $PATH.
shell-path() {
local name=$1
if test -f _tmp/spec-bin/$name; then
echo _tmp/spec-bin/$name
else
which $name
fi
}
readonly DASH=$(shell-path dash)
readonly BASH=$(shell-path bash)
readonly MKSH=$(shell-path mksh)
readonly ZSH=$(shell-path zsh)
if test -f _tmp/spec-bin/ash; then
readonly BUSYBOX_ASH=_tmp/spec-bin/ash
else
readonly BUSYBOX_ASH=_tmp/shells/ash
fi
# Usage: callers can override OSH_LIST to test on more than one version.
#
# Example:
# OSH_LIST='bin/osh _bin/osh' test/spec.sh all
readonly OSH_CPYTHON='bin/osh'
readonly OSH_OVM=${OSH_OVM:-_bin/osh}
OSH_LIST=${OSH_LIST:-} # A space-separated list.
if test -z "$OSH_LIST"; then
if test -e $OSH_OVM; then
# TODO: Does it make sense to copy the binary to an unrelated to directory,
# like /tmp? /tmp/{oil.ovm,osh}.
OSH_LIST="$OSH_CPYTHON $OSH_OVM"
else
OSH_LIST="$OSH_CPYTHON"
fi
fi
# ash and dash are similar, so not including ash by default. zsh is not quite
# POSIX.
readonly REF_SHELLS=($DASH $BASH $MKSH)
#
# Setup (TODO: Delete this once test/spec-bin.sh binaries are deployed)
#
link-busybox-ash() {
mkdir -p _tmp/shells
ln -s -f --verbose "$(which busybox)" _tmp/shells/ash
}
# dash and bash should be there by default on Ubuntu.
install-shells() {
sudo apt-get install busybox-static mksh zsh
link-busybox-ash
}
# TODO: Maybe do this before running all tests.
check-shells() {
for sh in "${REF_SHELLS[@]}" $ZSH $OSH_LIST; do
test -e $sh || { echo "ERROR: $sh does not exist"; break; }
test -x $sh || { echo "ERROR: $sh isn't executable"; break; }
done
}
maybe-show() {
local path=$1
if test -e $path; then
echo "--- $path ---"
cat $path
echo
fi
}
version-text() {
date-and-git-info
for bin in $OSH_LIST; do
echo ---
echo "\$ $bin --version"
$bin --version
echo
done
echo ---
$BASH --version | head -n 1
ls -l $BASH
echo
echo ---
$ZSH --version | head -n 1
ls -l $ZSH
echo
# No -v or -V or --version. TODO: Only use hermetic version on release.
echo ---
local my_dash=_tmp/spec-bin/dash
if test -f $my_dash; then
ls -l $my_dash
else
dpkg -s dash | egrep '^Package|Version'
fi
echo
echo ---
local my_mksh=_tmp/spec-bin/mksh
if test -f $my_mksh; then
ls -l $my_mksh
else
dpkg -s mksh | egrep '^Package|Version'
fi
echo
echo ---
local my_busybox=_tmp/spec-bin/busybox-1.22.0/busybox
if test -f $my_busybox; then
{ $my_busybox || true; } | head -n 1
ls -l $my_busybox
else
# Need || true because of pipefail
{ busybox || true; } | head -n 1
fi
echo
maybe-show /etc/debian_version
maybe-show /etc/lsb-release
}
#
# Helpers
#
sh-spec() {
local test_file=$1
shift
if [[ $test_file != *.test.sh ]]; then
die "Test file should end with .test.sh"
fi
local this_dir=$(cd $(dirname $0) && pwd)
local tmp_env=$this_dir/../_tmp/spec-tmp/$(basename $test_file)
mkdir -p $tmp_env
test/sh_spec.py \
--tmp-env $tmp_env \
--path-env "$this_dir/../spec/bin:$PATH" \
"$test_file" \
"$@"
}
#
# Misc
#
# Really what I want is enter(func) and exit(func), and filter by regex?
trace-var-sub() {
local out=_tmp/coverage
mkdir -p $out
# This creates *.cover files, with line counts.
#python -m trace --count -C $out \
# This prints trace with line numbers to stdout.
#python -m trace --trace -C $out \
python -m trace --trackcalls -C $out \
test/sh_spec.py spec/var-sub.test.sh $DASH $BASH "$@"
ls -l $out
head $out/*.cover
}
#
# Run All tests
#
all() {
test/spec-runner.sh all-parallel "$@"
}
# Usgae: test/spec.sh dbg smoke, dbg-all
dbg() {
OSH_LIST='_bin/osh-dbg' $0 "$@"
}
# For completion
dbg-all() {
$0 dbg all
}
#
# Invidual tests.
#
# We configure the shells they run on and the number of allowed failures (to
# prevent regressions.)
#
smoke() {
sh-spec spec/smoke.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
interactive() {
sh-spec spec/interactive.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
prompt() {
sh-spec spec/prompt.test.sh --osh-failures-allowed 1 \
$BASH $OSH_LIST "$@"
}
osh-only() {
# 2 failures until we build in a JSON encoder.
sh-spec spec/osh-only.test.sh --osh-failures-allowed 2 \
$OSH_LIST "$@"
}
# Regress bugs
bugs() {
sh-spec spec/bugs.test.sh ${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@"
}
blog1() {
sh-spec spec/blog1.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
blog2() {
sh-spec spec/blog2.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
blog-other1() {
sh-spec spec/blog-other1.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
alias() {
sh-spec spec/alias.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
comments() {
sh-spec spec/comments.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
word-split() {
sh-spec spec/word-split.test.sh --osh-failures-allowed 3 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
word-eval() {
sh-spec spec/word-eval.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
# These cases apply to many shells.
assign() {
sh-spec spec/assign.test.sh --osh-failures-allowed 5 \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
# These cases apply to a few shells.
assign-extended() {
sh-spec spec/assign-extended.test.sh --osh-failures-allowed 2 \
$BASH $MKSH $OSH_LIST "$@"
}
# These test associative arrays
assign-dialects() {
sh-spec spec/assign-dialects.test.sh --osh-failures-allowed 1 \
$BASH $MKSH $OSH_LIST "$@"
}
background() {
sh-spec spec/background.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
subshell() {
sh-spec spec/subshell.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
quote() {
sh-spec spec/quote.test.sh \
${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
}
loop() {
sh-spec spec/loop.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
case_() {
sh-spec spec/case_.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
if_() {
sh-spec spec/if_.test.sh --osh-failures-allowed 1 \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
builtins() {
sh-spec spec/builtins.test.sh --osh-failures-allowed 1 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
builtin-eval-source() {
sh-spec spec/builtin-eval-source.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
builtin-io() {
sh-spec spec/builtin-io.test.sh \
${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@"
}
# Special bash printf things like -v and %q. Portable stuff goes in builtin-io.
builtin-printf() {
sh-spec spec/builtin-printf.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@"
}
builtins2() {
sh-spec spec/builtins2.test.sh ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
# dash and mksh don't implement 'dirs'
builtin-dirs() {
sh-spec spec/builtin-dirs.test.sh $BASH $ZSH $OSH_LIST "$@"
}
builtin-vars() {
sh-spec spec/builtin-vars.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
builtin-getopts() {
sh-spec spec/builtin-getopts.test.sh --osh-failures-allowed 1 \
${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
}
builtin-bracket() {
sh-spec spec/builtin-bracket.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
builtin-trap() {
sh-spec spec/builtin-trap.test.sh --osh-failures-allowed 3 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
# Bash implements type -t, but no other shell does. For Nix.
# zsh/mksh/dash don't have the 'help' builtin.
builtin-bash() {
sh-spec spec/builtin-bash.test.sh $BASH $OSH_LIST "$@"
}
# This is bash/OSH only
builtin-completion() {
sh-spec spec/builtin-completion.test.sh --osh-failures-allowed 1 \
$BASH $OSH_LIST "$@"
}
builtins-special() {
sh-spec spec/builtins-special.test.sh --osh-failures-allowed 4 \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
command-parsing() {
sh-spec spec/command-parsing.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
func-parsing() {
sh-spec spec/func-parsing.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
func() {
sh-spec spec/func.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
glob() {
sh-spec spec/glob.test.sh --osh-failures-allowed 4 \
${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
}
arith() {
sh-spec spec/arith.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
command-sub() {
sh-spec spec/command-sub.test.sh --osh-failures-allowed 1 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
command_() {
sh-spec spec/command_.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
pipeline() {
sh-spec spec/pipeline.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
explore-parsing() {
sh-spec spec/explore-parsing.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
parse-errors() {
sh-spec spec/parse-errors.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
here-doc() {
# NOTE: The last two tests, 31 and 32, have different behavior on my Ubuntu
# and Debian machines.
# - On Ubuntu, read_from_fd.py fails with Errno 9 -- bad file descriptor.
# - On Debian, the whole process hangs.
# Is this due to Python 3.2 vs 3.4? Either way osh doesn't implement the
# functionality, so it's probably best to just implement it.
sh-spec spec/here-doc.test.sh --range 0-31 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
redirect() {
sh-spec spec/redirect.test.sh --osh-failures-allowed 5 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
posix() {
sh-spec spec/posix.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
special-vars() {
sh-spec spec/special-vars.test.sh --osh-failures-allowed 5 \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
introspect() {
sh-spec spec/introspect.test.sh \
$BASH $OSH_LIST "$@"
}
# DONE -- pysh is the most conformant!
tilde() {
sh-spec spec/tilde.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
var-op-test() {
sh-spec spec/var-op-test.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
var-op-len() {
sh-spec spec/var-op-len.test.sh \
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}
var-op-patsub() {
sh-spec spec/var-op-patsub.test.sh \
$BASH $MKSH $ZSH $OSH_LIST "$@"
}
var-op-other() {
# dash doesn't support any of these operations
sh-spec spec/var-op-other.test.sh --osh-failures-allowed 2 \
$BASH $MKSH $ZSH $OSH_LIST "$@"
}
var-op-strip() {
sh-spec spec/var-op-strip.test.sh --osh-failures-allowed 1 \
${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@"
}
var-sub() {
# NOTE: ZSH has interesting behavior, like echo hi > "$@" can write to TWO
# FILES! But ultimately we don't really care, so I disabled it.
sh-spec spec/var-sub.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
var-num() {
sh-spec spec/var-num.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
var-sub-quote() {
sh-spec spec/var-sub-quote.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
sh-usage() {
sh-spec spec/sh-usage.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
sh-options() {
sh-spec spec/sh-options.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
xtrace() {
sh-spec spec/xtrace.test.sh --osh-failures-allowed 5 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
strict-options() {
sh-spec spec/strict-options.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}
errexit() {
sh-spec spec/errexit.test.sh \
${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
}
errexit-strict() {
sh-spec spec/errexit-strict.test.sh \
${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
}
#
# Non-POSIX extensions: arrays, brace expansion, [[, ((, etc.
#
# There as many non-POSIX arithmetic contexts.
arith-context() {
sh-spec spec/arith-context.test.sh --osh-failures-allowed 1 \
$BASH $MKSH $ZSH $OSH_LIST "$@"
}
array() {
sh-spec spec/array.test.sh --osh-failures-allowed 2 \
$BASH $MKSH $OSH_LIST "$@"
}
array-compat() {
sh-spec spec/array-compat.test.sh \
$BASH $MKSH $OSH_LIST "$@"
}
type-compat() {
sh-spec spec/type-compat.test.sh $BASH "$@"
}
# += is not POSIX and not in dash.
append() {
sh-spec spec/append.test.sh --osh-failures-allowed 1 \
$BASH $MKSH $OSH_LIST "$@"
}
# associative array -- mksh and zsh implement different associative arrays.
assoc() {
sh-spec spec/assoc.test.sh --osh-failures-allowed 6 \
$BASH $OSH_LIST "$@"
}
# ZSH also has associative arrays
assoc-zsh() {
sh-spec spec/assoc-zsh.test.sh $ZSH "$@"
}
# NOTE: zsh passes about half and fails about half. It supports a subset of [[
# I guess.
dbracket() {
sh-spec spec/dbracket.test.sh \
$BASH $MKSH $OSH_LIST "$@"
#sh-spec spec/dbracket.test.sh $BASH $MKSH $OSH_LIST $ZSH "$@"
}
dparen() {
sh-spec spec/dparen.test.sh --osh-failures-allowed 4 \
$BASH $MKSH $ZSH $OSH_LIST "$@"
}
brace-expansion() {
sh-spec spec/brace-expansion.test.sh \
$BASH $MKSH $ZSH $OSH_LIST "$@"
}
regex() {
sh-spec spec/regex.test.sh --osh-failures-allowed 2 \
$BASH $ZSH $OSH_LIST "$@"
}
process-sub() {
# mksh and dash don't support it
sh-spec spec/process-sub.test.sh \
$BASH $ZSH $OSH_LIST "$@"
}
# This does file globbing
extended-glob() {
# Do NOT use dash here. Lack of brace sub means it leaves bad files on the
# file system.
sh-spec spec/extended-glob.test.sh $BASH $MKSH $OSH_LIST "$@"
}
# This does string matching.
extglob-match() {
sh-spec spec/extglob-match.test.sh $BASH $MKSH $OSH_LIST "$@"
}
# ${!var} syntax -- oil should replace this with associative arrays.
# mksh has completely different behavior for this syntax. Not worth testing.
var-ref() {
sh-spec spec/var-ref.test.sh --osh-failures-allowed 4 \
$BASH $OSH_LIST "$@"
}
# local -n
# mksh implements some of this. OSH doesn't implement it at all.
named-ref() {
sh-spec spec/named-ref.test.sh \
$BASH $MKSH "$@"
}
let() {
sh-spec spec/let.test.sh $BASH $MKSH $ZSH "$@"
}
for-expr() {
sh-spec spec/for-expr.test.sh \
$BASH $ZSH $OSH_LIST "$@"
}
empty-bodies() {
sh-spec spec/empty-bodies.test.sh "${REF_SHELLS[@]}" $ZSH $OSH_LIST "$@"
}
# TODO: This is for the ANTLR grammars, in the oil-sketch repo.
# osh has infinite loop?
shell-grammar() {
sh-spec spec/shell-grammar.test.sh $BASH $MKSH $ZSH "$@"
}
"$@"