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

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