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

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