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

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