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

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