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

968 lines, 652 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# test/spec.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9shopt -s strict:all 2>/dev/null || true # dogfood for OSH
10
11REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
12
13source test/common.sh
14source test/spec-common.sh
15source devtools/run-task.sh
16
17if test -z "${IN_NIX_SHELL:-}"; then
18 source build/dev-shell.sh # to run 'dash', etc.
19fi
20
21# TODO: Just use 'dash bash' and $PATH
22readonly DASH=dash
23readonly BASH=bash
24readonly MKSH=mksh
25readonly ZSH=zsh
26readonly BUSYBOX_ASH=ash
27
28# ash and dash are similar, so not including ash by default. zsh is not quite
29# POSIX.
30readonly REF_SHELLS=($DASH $BASH $MKSH)
31
32check-survey-shells() {
33 ### Make sure bash, zsh, OSH, etc. exist
34
35 # Note: yash isn't here, but it is used in a couple tests
36
37 test/spec-runner.sh shell-sanity-check "${REF_SHELLS[@]}" $ZSH $BUSYBOX_ASH $OSH_LIST
38}
39
40# TODO: remove this stub after we hollow out this file
41
42run-file() { test/spec-py.sh run-file "$@"; }
43
44#
45# Misc
46#
47
48# Really what I want is enter(func) and exit(func), and filter by regex?
49trace-var-sub() {
50 local out=_tmp/coverage
51 mkdir -p $out
52
53 # This creates *.cover files, with line counts.
54 #python -m trace --count -C $out \
55
56 # This prints trace with line numbers to stdout.
57 #python -m trace --trace -C $out \
58 PYTHONPATH=. python -m trace --trackcalls -C $out \
59 test/sh_spec.py spec/var-sub.test.sh $DASH $BASH "$@"
60
61 ls -l $out
62 head $out/*.cover
63}
64
65#
66# Individual tests.
67#
68# We configure the shells they run on and the number of allowed failures (to
69# prevent regressions.)
70#
71
72interactive-parse() {
73 run-file interactive-parse "$@"
74}
75
76smoke() {
77 run-file smoke "$@"
78}
79
80interactive() {
81 run-file interactive "$@"
82}
83
84prompt() {
85 run-file prompt "$@"
86}
87
88bugs() {
89 run-file bugs "$@"
90}
91
92TODO-deprecate() {
93 run-file TODO-deprecate "$@"
94}
95
96blog1() {
97 sh-spec spec/blog1.test.sh \
98 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
99}
100
101blog2() {
102 sh-spec spec/blog2.test.sh \
103 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
104}
105
106blog-other1() {
107 sh-spec spec/blog-other1.test.sh \
108 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
109}
110
111alias() {
112 run-file alias "$@"
113}
114
115comments() {
116 sh-spec spec/comments.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
117}
118
119word-split() {
120 run-file word-split "$@"
121}
122
123word-eval() {
124 sh-spec spec/word-eval.test.sh \
125 ${REF_SHELLS[@]} $OSH_LIST "$@"
126}
127
128# These cases apply to many shells.
129assign() {
130 run-file assign "$@"
131}
132
133# These cases apply to a few shells.
134assign-extended() {
135 sh-spec spec/assign-extended.test.sh \
136 $BASH $MKSH $OSH_LIST "$@"
137}
138
139# Corner cases that OSH doesn't handle
140assign-deferred() {
141 sh-spec spec/assign-deferred.test.sh \
142 $BASH $MKSH "$@"
143}
144
145# These test associative arrays
146assign-dialects() {
147 run-file assign-dialects "$@"
148}
149
150background() {
151 run-file background "$@"
152}
153
154subshell() {
155 sh-spec spec/subshell.test.sh \
156 ${REF_SHELLS[@]} $OSH_LIST "$@"
157}
158
159quote() {
160 sh-spec spec/quote.test.sh \
161 ${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
162}
163
164unicode() {
165 run-file unicode "$@"
166}
167
168loop() {
169 sh-spec spec/loop.test.sh \
170 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
171}
172
173case_() {
174 run-file case_ "$@"
175}
176
177if_() {
178 sh-spec spec/if_.test.sh \
179 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
180}
181
182builtin-misc() {
183 run-file builtin-misc "$@"
184}
185
186builtin-process() {
187 run-file builtin-process "$@"
188}
189
190builtin-cd() {
191 run-file builtin-cd "$@"
192}
193
194builtin-eval-source() {
195 sh-spec spec/builtin-eval-source.test.sh \
196 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
197}
198
199builtin-echo() {
200 run-file builtin-echo "$@"
201}
202
203builtin-read() {
204 run-file builtin-read "$@"
205}
206
207nul-bytes() {
208 run-file nul-bytes "$@"
209}
210
211whitespace() {
212 run-file whitespace "$@"
213}
214
215# Special bash printf things like -v and %q. Portable stuff goes in builtin-io.
216builtin-printf() {
217 run-file builtin-printf "$@"
218}
219
220builtin-meta() {
221 run-file builtin-meta "$@"
222}
223
224builtin-history() {
225 run-file builtin-history "$@"
226}
227
228# dash and mksh don't implement 'dirs'
229builtin-dirs() {
230 sh-spec spec/builtin-dirs.test.sh \
231 $BASH $ZSH $OSH_LIST "$@"
232}
233
234builtin-vars() {
235 run-file builtin-vars "$@"
236}
237
238builtin-getopts() {
239 run-file builtin-getopts "$@"
240}
241
242builtin-bracket() {
243 run-file builtin-bracket "$@"
244}
245
246builtin-trap() {
247 sh-spec spec/builtin-trap.test.sh \
248 ${REF_SHELLS[@]} $OSH_LIST "$@"
249}
250
251builtin-trap-bash() {
252 run-file builtin-trap-bash "$@"
253}
254
255# Bash implements type -t, but no other shell does. For Nix.
256# zsh/mksh/dash don't have the 'help' builtin.
257builtin-bash() {
258 run-file builtin-bash "$@"
259}
260
261builtin-type() {
262 run-file builtin-type "$@"
263}
264
265builtin-type-bash() {
266 run-file builtin-type-bash "$@"
267}
268
269vars-bash() {
270 run-file vars-bash "$@"
271}
272
273vars-special() {
274 run-file vars-special "$@"
275}
276
277builtin-completion() {
278 run-file builtin-completion "$@"
279}
280
281builtin-special() {
282 run-file builtin-special "$@"
283}
284
285builtin-times() {
286 sh-spec spec/builtin-times.test.sh $BASH $ZSH $OSH_LIST "$@"
287}
288
289command-parsing() {
290 sh-spec spec/command-parsing.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
291}
292
293func-parsing() {
294 sh-spec spec/func-parsing.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
295}
296
297sh-func() {
298 run-file sh-func "$@"
299}
300
301glob() {
302 run-file glob "$@"
303}
304
305globignore() {
306 run-file globignore "$@"
307}
308
309arith() {
310 run-file arith "$@"
311}
312
313command-sub() {
314 sh-spec spec/command-sub.test.sh \
315 ${REF_SHELLS[@]} $OSH_LIST "$@"
316}
317
318command_() {
319 sh-spec spec/command_.test.sh \
320 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
321}
322
323pipeline() {
324 sh-spec spec/pipeline.test.sh \
325 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
326}
327
328explore-parsing() {
329 sh-spec spec/explore-parsing.test.sh \
330 ${REF_SHELLS[@]} $OSH_LIST "$@"
331}
332
333parse-errors() {
334 run-file parse-errors "$@"
335}
336
337here-doc() {
338 # NOTE: The last two tests, 31 and 32, have different behavior on my Ubuntu
339 # and Debian machines.
340 # - On Ubuntu, read_from_fd.py fails with Errno 9 -- bad file descriptor.
341 # - On Debian, the whole process hangs.
342 # Is this due to Python 3.2 vs 3.4? Either way osh doesn't implement the
343 # functionality, so it's probably best to just implement it.
344 sh-spec spec/here-doc.test.sh --range 0-31 \
345 ${REF_SHELLS[@]} $OSH_LIST "$@"
346}
347
348redirect() {
349 run-file redirect "$@"
350}
351
352redirect-multi() {
353 run-file redirect-multi "$@"
354}
355
356posix() {
357 sh-spec spec/posix.test.sh \
358 ${REF_SHELLS[@]} $OSH_LIST "$@"
359}
360
361introspect() {
362 run-file introspect "$@"
363}
364
365tilde() {
366 run-file tilde "$@"
367}
368
369var-op-test() {
370 run-file var-op-test "$@"
371}
372
373var-op-len() {
374 sh-spec spec/var-op-len.test.sh \
375 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
376}
377
378var-op-patsub() {
379 # 1 unicode failure, and [^]] which is a parsing divergence
380 run-file var-op-patsub "$@"
381}
382
383var-op-slice() {
384 run-file var-op-slice "$@"
385}
386
387var-op-bash() {
388 run-file var-op-bash "$@"
389}
390
391var-op-strip() {
392 sh-spec spec/var-op-strip.test.sh \
393 ${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@"
394}
395
396var-sub() {
397 # NOTE: ZSH has interesting behavior, like echo hi > "$@" can write to TWO
398 # FILES! But ultimately we don't really care, so I disabled it.
399 sh-spec spec/var-sub.test.sh \
400 ${REF_SHELLS[@]} $OSH_LIST "$@"
401}
402
403var-num() {
404 run-file var-num "$@"
405}
406
407var-sub-quote() {
408 sh-spec spec/var-sub-quote.test.sh \
409 ${REF_SHELLS[@]} $OSH_LIST "$@"
410}
411
412sh-usage() {
413 run-file sh-usage "$@"
414}
415
416sh-options() {
417 run-file sh-options "$@"
418}
419
420xtrace() {
421 run-file xtrace "$@"
422}
423
424strict-options() {
425 sh-spec spec/strict-options.test.sh \
426 ${REF_SHELLS[@]} $OSH_LIST "$@"
427}
428
429exit-status() {
430 run-file exit-status "$@"
431}
432
433errexit() {
434 sh-spec spec/errexit.test.sh \
435 ${REF_SHELLS[@]} $BUSYBOX_ASH $OSH_LIST "$@"
436}
437
438errexit-osh() {
439 run-file errexit-osh "$@"
440}
441
442fatal-errors() {
443 sh-spec spec/fatal-errors.test.sh \
444 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
445}
446
447#
448# Non-POSIX extensions: arrays, brace expansion, [[, ((, etc.
449#
450
451# There as many non-POSIX arithmetic contexts.
452arith-context() {
453 sh-spec spec/arith-context.test.sh \
454 $BASH $MKSH $ZSH $OSH_LIST "$@"
455}
456
457array() {
458 sh-spec spec/array.test.sh \
459 $BASH $MKSH $OSH_LIST "$@"
460}
461
462array-compat() {
463 run-file array-compat "$@"
464}
465
466type-compat() {
467 run-file type-compat "$@"
468}
469
470# += is not POSIX and not in dash.
471append() {
472 run-file append "$@"
473}
474
475# associative array -- mksh and zsh implement different associative arrays.
476assoc() {
477 run-file assoc "$@"
478}
479
480# ZSH also has associative arrays
481assoc-zsh() {
482 sh-spec spec/assoc-zsh.test.sh $ZSH "$@"
483}
484
485dbracket() {
486 run-file dbracket "$@"
487}
488
489dparen() {
490 run-file dparen "$@"
491}
492
493brace-expansion() {
494 run-file brace-expansion "$@"
495}
496
497regex() {
498 run-file regex "$@"
499}
500
501process-sub() {
502 # mksh and dash don't support it
503 sh-spec spec/process-sub.test.sh \
504 $BASH $ZSH $OSH_LIST "$@"
505}
506
507# This does file system globbing
508extglob-files() {
509 run-file extglob-files "$@"
510}
511
512# This does string matching.
513extglob-match() {
514 sh-spec spec/extglob-match.test.sh \
515 $BASH $MKSH $OSH_LIST "$@"
516}
517
518nocasematch-match() {
519 run-file nocasematch-match "$@"
520}
521
522# ${!var} syntax -- oil should replace this with associative arrays.
523# mksh has completely different behavior for this syntax. Not worth testing.
524var-ref() {
525 run-file var-ref "$@"
526}
527
528nameref() {
529 ### declare -n / local -n
530 run-file nameref "$@"
531}
532
533let() {
534 sh-spec spec/let.test.sh $BASH $MKSH $ZSH "$@"
535}
536
537for-expr() {
538 sh-spec spec/for-expr.test.sh \
539 $BASH $ZSH $OSH_LIST "$@"
540}
541
542empty-bodies() {
543 sh-spec spec/empty-bodies.test.sh "${REF_SHELLS[@]}" $ZSH $OSH_LIST "$@"
544}
545
546# TODO: This is for the ANTLR grammars, in the oil-sketch repo.
547# osh has infinite loop?
548shell-grammar() {
549 sh-spec spec/shell-grammar.test.sh $BASH $MKSH $ZSH "$@"
550}
551
552serialize() {
553 run-file serialize "$@"
554}
555
556#
557# Smoosh
558#
559
560readonly SMOOSH_REPO=~/git/languages/smoosh
561
562sh-spec-smoosh-env() {
563 local test_file=$1
564 shift
565
566 # - smoosh tests use $TEST_SHELL instead of $SH
567 # - cd $TMP to avoid littering repo
568 # - pass -o posix
569 # - timeout of 1 second
570 # - Some tests in smoosh use $HOME and $LOGNAME
571
572 sh-spec $test_file \
573 --sh-env-var-name TEST_SHELL \
574 --posix \
575 --env-pair "TEST_UTIL=$SMOOSH_REPO/tests/util" \
576 --env-pair "LOGNAME=$LOGNAME" \
577 --env-pair "HOME=$HOME" \
578 --timeout 1 \
579 --oils-bin-dir $REPO_ROOT/bin \
580 --compare-shells \
581 "$@"
582}
583
584# For speed, only run with one copy of OSH.
585readonly smoosh_osh_list=$OSH_CPYTHON
586
587smoosh() {
588 ### Run case smoosh from the console
589
590 # TODO: Use --oils-bin-dir
591 # our_shells, etc.
592
593 sh-spec-smoosh-env _tmp/smoosh.test.sh \
594 ${REF_SHELLS[@]} $smoosh_osh_list \
595 "$@"
596}
597
598smoosh-hang() {
599 ### Run case smoosh-hang from the console
600
601 # Need the smoosh timeout tool to run correctly.
602 sh-spec-smoosh-env _tmp/smoosh-hang.test.sh \
603 --timeout-bin "$SMOOSH_REPO/tests/util/timeout" \
604 --timeout 1 \
605 "$@"
606}
607
608_one-html() {
609 local spec_name=$1
610 shift
611
612 local out_dir=_tmp/spec/smoosh
613 local tmp_dir=_tmp/src-smoosh
614 mkdir -p $out_dir $out_dir
615
616 doctools/src_tree.py smoosh-file \
617 _tmp/$spec_name.test.sh \
618 $out_dir/$spec_name.test.html
619
620 local out=$out_dir/${spec_name}.html
621 set +o errexit
622 # Shell function is smoosh or smoosh-hang
623 time $spec_name --format html "$@" > $out
624 set -o errexit
625
626 echo
627 echo "Wrote $out"
628
629 # NOTE: This IGNORES the exit status.
630}
631
632# TODO:
633# - Put these tests in the CI
634# - Import smoosh spec tests into the repo, with 'test/smoosh.sh'
635
636smoosh-html() {
637 ### Run by devtools/release.sh
638 _one-html smoosh "$@"
639}
640
641smoosh-hang-html() {
642 ### Run by devtools/release.sh
643 _one-html smoosh-hang "$@"
644}
645
646html-demo() {
647 ### Test for --format html
648
649 local out=_tmp/spec/demo.html
650 builtin-special --format html "$@" > $out
651
652 echo
653 echo "Wrote $out"
654}
655
656#
657# Hay is part of the YSH suite
658#
659
660hay() {
661 run-file hay "$@"
662}
663
664hay-isolation() {
665 run-file hay-isolation "$@"
666}
667
668hay-meta() {
669 run-file hay-meta "$@"
670}
671
672#
673# YSH
674#
675
676ysh-convert() {
677 run-file ysh-convert "$@"
678}
679
680ysh-completion() {
681 run-file ysh-completion "$@"
682}
683
684ysh-stdlib() {
685 run-file ysh-stdlib "$@"
686}
687
688ysh-stdlib-2() {
689 run-file ysh-stdlib-2 "$@"
690}
691
692ysh-stdlib-args() {
693 run-file ysh-stdlib-args "$@"
694}
695
696ysh-stdlib-testing() {
697 run-file ysh-stdlib-testing "$@"
698}
699
700ysh-stdlib-synch() {
701 run-file ysh-stdlib-synch "$@"
702}
703
704ysh-source() {
705 run-file ysh-source "$@"
706}
707
708ysh-usage() {
709 run-file ysh-usage "$@"
710}
711
712ysh-unicode() {
713 run-file ysh-unicode "$@"
714}
715
716ysh-bin() {
717 run-file ysh-bin "$@"
718}
719
720ysh-dict() {
721 run-file ysh-dict "$@"
722}
723
724ysh-list() {
725 run-file ysh-list "$@"
726}
727
728ysh-place() {
729 run-file ysh-place "$@"
730}
731
732ysh-prompt() {
733 run-file ysh-prompt "$@"
734}
735
736ysh-assign() {
737 run-file ysh-assign "$@"
738}
739
740ysh-augmented() {
741 run-file ysh-augmented "$@"
742}
743
744ysh-blocks() {
745 run-file ysh-blocks "$@"
746}
747
748ysh-bugs() {
749 run-file ysh-bugs "$@"
750}
751
752ysh-builtins() {
753 run-file ysh-builtins "$@"
754}
755
756ysh-builtin-module() {
757 run-file ysh-builtin-module "$@"
758}
759
760ysh-builtin-eval() {
761 run-file ysh-builtin-eval "$@"
762}
763
764# Related to errexit-oil
765ysh-builtin-error() {
766 run-file ysh-builtin-error "$@"
767}
768
769ysh-builtin-meta() {
770 run-file ysh-builtin-meta "$@"
771}
772
773ysh-builtin-process() {
774 run-file ysh-builtin-process "$@"
775}
776
777ysh-builtin-shopt() {
778 run-file ysh-builtin-shopt "$@"
779}
780
781ysh-case() {
782 run-file ysh-case "$@"
783}
784
785ysh-command-sub() {
786 run-file ysh-command-sub "$@"
787}
788
789ysh-demo() {
790 run-file ysh-demo "$@"
791}
792
793ysh-expr() {
794 run-file ysh-expr "$@"
795}
796
797ysh-int-float() {
798 run-file ysh-int-float "$@"
799}
800
801ysh-expr-bool() {
802 run-file ysh-expr-bool "$@"
803}
804
805ysh-expr-arith() {
806 run-file ysh-expr-arith "$@"
807}
808
809ysh-expr-compare() {
810 run-file ysh-expr-compare "$@"
811}
812
813ysh-expr-sub() {
814 run-file ysh-expr-sub "$@"
815}
816
817ysh-for() {
818 run-file ysh-for "$@"
819}
820
821ysh-methods() {
822 run-file ysh-methods "$@"
823}
824
825ysh-func() {
826 run-file ysh-func "$@"
827}
828
829ysh-func-builtin() {
830 run-file ysh-func-builtin "$@"
831}
832
833ysh-funcs-external() {
834 run-file ysh-funcs-external "$@"
835}
836
837ysh-interactive() {
838 run-file ysh-interactive "$@"
839}
840
841ysh-json() {
842 run-file ysh-json "$@"
843}
844
845ysh-keywords() {
846 run-file ysh-keywords "$@"
847}
848
849ysh-multiline() {
850 run-file ysh-multiline "$@"
851}
852
853ysh-options() {
854 run-file ysh-options "$@"
855}
856
857ysh-options-assign() {
858 run-file ysh-options-assign "$@"
859}
860
861ysh-proc() {
862 run-file ysh-proc "$@"
863}
864
865ysh-regex() {
866 run-file ysh-regex "$@"
867}
868
869ysh-regex-api() {
870 run-file ysh-regex-api "$@"
871}
872
873ysh-reserved() {
874 run-file ysh-reserved "$@"
875}
876
877ysh-scope() {
878 run-file ysh-scope "$@"
879}
880
881ysh-slice-range() {
882 run-file ysh-slice-range "$@"
883}
884
885ysh-string() {
886 run-file ysh-string "$@"
887}
888
889ysh-special-vars() {
890 run-file ysh-special-vars "$@"
891}
892
893ysh-tuple() {
894 run-file ysh-tuple "$@"
895}
896
897ysh-var-sub() {
898 run-file ysh-var-sub "$@"
899}
900
901ysh-with-sh() {
902 run-file ysh-with-sh "$@"
903}
904
905ysh-word-eval() {
906 run-file ysh-word-eval "$@"
907}
908
909ysh-xtrace() {
910 run-file ysh-xtrace "$@"
911}
912
913ysh-user-feedback() {
914 run-file ysh-user-feedback "$@"
915}
916
917ysh-builtin-ctx() {
918 run-file ysh-builtin-ctx "$@"
919}
920
921ysh-builtin-error() {
922 run-file ysh-builtin-error "$@"
923}
924
925ysh-builtin-help() {
926 run-file ysh-builtin-help "$@"
927}
928
929ysh-dev() {
930 run-file ysh-dev "$@"
931}
932
933ysh-printing() {
934 run-file ysh-printing "$@"
935}
936
937
938#
939# More OSH
940#
941
942nix-idioms() {
943 run-file nix-idioms "$@"
944}
945
946zsh-idioms() {
947 run-file zsh-idioms "$@"
948}
949
950ble-idioms() {
951 sh-spec spec/ble-idioms.test.sh \
952 $BASH $ZSH $MKSH $BUSYBOX_ASH $OSH_LIST "$@"
953}
954
955ble-features() {
956 sh-spec spec/ble-features.test.sh \
957 $BASH $ZSH $MKSH $BUSYBOX_ASH $DASH yash $OSH_LIST "$@"
958}
959
960toysh() {
961 run-file toysh "$@"
962}
963
964toysh-posix() {
965 run-file toysh-posix "$@"
966}
967
968run-task "$@"