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

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