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

994 lines, 668 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
304arith() {
305 run-file arith "$@"
306}
307
308arith-dynamic() {
309 run-file arith-dynamic "$@"
310}
311
312command-sub() {
313 sh-spec spec/command-sub.test.sh \
314 ${REF_SHELLS[@]} $OSH_LIST "$@"
315}
316
317command_() {
318 sh-spec spec/command_.test.sh \
319 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
320}
321
322pipeline() {
323 run-file pipeline "$@"
324}
325
326explore-parsing() {
327 sh-spec spec/explore-parsing.test.sh \
328 ${REF_SHELLS[@]} $OSH_LIST "$@"
329}
330
331parse-errors() {
332 run-file parse-errors "$@"
333}
334
335here-doc() {
336 # NOTE: The last two tests, 31 and 32, have different behavior on my Ubuntu
337 # and Debian machines.
338 # - On Ubuntu, read_from_fd.py fails with Errno 9 -- bad file descriptor.
339 # - On Debian, the whole process hangs.
340 # Is this due to Python 3.2 vs 3.4? Either way osh doesn't implement the
341 # functionality, so it's probably best to just implement it.
342 sh-spec spec/here-doc.test.sh --range 0-31 \
343 ${REF_SHELLS[@]} $OSH_LIST "$@"
344}
345
346redirect() {
347 run-file redirect "$@"
348}
349
350redirect-command() {
351 run-file redirect-command "$@"
352}
353
354redirect-multi() {
355 run-file redirect-multi "$@"
356}
357
358posix() {
359 sh-spec spec/posix.test.sh \
360 ${REF_SHELLS[@]} $OSH_LIST "$@"
361}
362
363introspect() {
364 run-file introspect "$@"
365}
366
367tilde() {
368 run-file tilde "$@"
369}
370
371var-op-test() {
372 run-file var-op-test "$@"
373}
374
375var-op-len() {
376 sh-spec spec/var-op-len.test.sh \
377 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
378}
379
380var-op-patsub() {
381 # 1 unicode failure, and [^]] which is a parsing divergence
382 run-file var-op-patsub "$@"
383}
384
385var-op-slice() {
386 run-file var-op-slice "$@"
387}
388
389var-op-bash() {
390 run-file var-op-bash "$@"
391}
392
393var-op-strip() {
394 sh-spec spec/var-op-strip.test.sh \
395 ${REF_SHELLS[@]} $ZSH $BUSYBOX_ASH $OSH_LIST "$@"
396}
397
398var-sub() {
399 # NOTE: ZSH has interesting behavior, like echo hi > "$@" can write to TWO
400 # FILES! But ultimately we don't really care, so I disabled it.
401 sh-spec spec/var-sub.test.sh \
402 ${REF_SHELLS[@]} $OSH_LIST "$@"
403}
404
405var-num() {
406 run-file var-num "$@"
407}
408
409var-sub-quote() {
410 sh-spec spec/var-sub-quote.test.sh \
411 ${REF_SHELLS[@]} $OSH_LIST "$@"
412}
413
414sh-usage() {
415 run-file sh-usage "$@"
416}
417
418sh-options() {
419 run-file sh-options "$@"
420}
421
422xtrace() {
423 run-file xtrace "$@"
424}
425
426strict-options() {
427 run-file strict-options "$@"
428}
429
430exit-status() {
431 run-file exit-status "$@"
432}
433
434errexit() {
435 run-file errexit "$@"
436}
437
438errexit-osh() {
439 run-file errexit-osh "$@"
440}
441
442fatal-errors() {
443 sh-spec spec/fatal-errors.test.sh \
444 ${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
445}
446
447#
448# Non-POSIX extensions: arrays, brace expansion, [[, ((, etc.
449#
450
451# There as many non-POSIX arithmetic contexts.
452arith-context() {
453 run-file arith-context "$@"
454}
455
456array() {
457 run-file array "$@"
458}
459
460array-basic() {
461 run-file array-basic "$@"
462}
463
464array-compat() {
465 run-file array-compat "$@"
466}
467
468type-compat() {
469 run-file type-compat "$@"
470}
471
472# += is not POSIX and not in dash.
473append() {
474 run-file append "$@"
475}
476
477# associative array -- mksh and zsh implement different associative arrays.
478assoc() {
479 run-file assoc "$@"
480}
481
482# ZSH also has associative arrays
483assoc-zsh() {
484 run-file assoc-zsh "$@"
485}
486
487dbracket() {
488 run-file dbracket "$@"
489}
490
491dparen() {
492 run-file dparen "$@"
493}
494
495brace-expansion() {
496 run-file brace-expansion "$@"
497}
498
499regex() {
500 run-file regex "$@"
501}
502
503process-sub() {
504 run-file process-sub "$@"
505}
506
507# This does file system globbing
508extglob-files() {
509 run-file extglob-files "$@"
510}
511
512# This does string matching.
513extglob-match() {
514 sh-spec spec/extglob-match.test.sh \
515 $BASH $MKSH $OSH_LIST "$@"
516}
517
518nocasematch-match() {
519 run-file nocasematch-match "$@"
520}
521
522# ${!var} syntax -- oil should replace this with associative arrays.
523# mksh has completely different behavior for this syntax. Not worth testing.
524var-ref() {
525 run-file var-ref "$@"
526}
527
528nameref() {
529 ### declare -n / local -n
530 run-file nameref "$@"
531}
532
533let() {
534 run-file let "$@"
535}
536
537for-expr() {
538 run-file for-expr "$@"
539}
540
541empty-bodies() {
542 sh-spec spec/empty-bodies.test.sh "${REF_SHELLS[@]}" $ZSH $OSH_LIST "$@"
543}
544
545# TODO: This is for the ANTLR grammars, in the oil-sketch repo.
546# osh has infinite loop?
547shell-grammar() {
548 sh-spec spec/shell-grammar.test.sh $BASH $MKSH $ZSH "$@"
549}
550
551serialize() {
552 run-file serialize "$@"
553}
554
555#
556# Smoosh
557#
558
559readonly SMOOSH_REPO=~/git/languages/smoosh
560
561sh-spec-smoosh-env() {
562 local test_file=$1
563 shift
564
565 # - smoosh tests use $TEST_SHELL instead of $SH
566 # - cd $TMP to avoid littering repo
567 # - pass -o posix
568 # - timeout of 1 second
569 # - Some tests in smoosh use $HOME and $LOGNAME
570
571 sh-spec $test_file \
572 --sh-env-var-name TEST_SHELL \
573 --posix \
574 --env-pair "TEST_UTIL=$SMOOSH_REPO/tests/util" \
575 --env-pair "LOGNAME=$LOGNAME" \
576 --env-pair "HOME=$HOME" \
577 --timeout 1 \
578 --oils-bin-dir $REPO_ROOT/bin \
579 --compare-shells \
580 "$@"
581}
582
583# For speed, only run with one copy of OSH.
584readonly smoosh_osh_list=$OSH_CPYTHON
585
586smoosh() {
587 ### Run case smoosh from the console
588
589 # TODO: Use --oils-bin-dir
590 # our_shells, etc.
591
592 sh-spec-smoosh-env _tmp/smoosh.test.sh \
593 ${REF_SHELLS[@]} $smoosh_osh_list \
594 "$@"
595}
596
597smoosh-hang() {
598 ### Run case smoosh-hang from the console
599
600 # Need the smoosh timeout tool to run correctly.
601 sh-spec-smoosh-env _tmp/smoosh-hang.test.sh \
602 --timeout-bin "$SMOOSH_REPO/tests/util/timeout" \
603 --timeout 1 \
604 "$@"
605}
606
607_one-html() {
608 local spec_name=$1
609 shift
610
611 local out_dir=_tmp/spec/smoosh
612 local tmp_dir=_tmp/src-smoosh
613 mkdir -p $out_dir $out_dir
614
615 doctools/src_tree.py smoosh-file \
616 _tmp/$spec_name.test.sh \
617 $out_dir/$spec_name.test.html
618
619 local out=$out_dir/${spec_name}.html
620 set +o errexit
621 # Shell function is smoosh or smoosh-hang
622 time $spec_name --format html "$@" > $out
623 set -o errexit
624
625 echo
626 echo "Wrote $out"
627
628 # NOTE: This IGNORES the exit status.
629}
630
631# TODO:
632# - Put these tests in the CI
633# - Import smoosh spec tests into the repo, with 'test/smoosh.sh'
634
635smoosh-html() {
636 ### Run by devtools/release.sh
637 _one-html smoosh "$@"
638}
639
640smoosh-hang-html() {
641 ### Run by devtools/release.sh
642 _one-html smoosh-hang "$@"
643}
644
645html-demo() {
646 ### Test for --format html
647
648 local out=_tmp/spec/demo.html
649 builtin-special --format html "$@" > $out
650
651 echo
652 echo "Wrote $out"
653}
654
655#
656# Hay is part of the YSH suite
657#
658
659hay() {
660 run-file hay "$@"
661}
662
663hay-isolation() {
664 run-file hay-isolation "$@"
665}
666
667hay-meta() {
668 run-file hay-meta "$@"
669}
670
671#
672# YSH
673#
674
675ysh-TODO-deprecate() {
676 run-file ysh-TODO-deprecate "$@"
677}
678
679ysh-convert() {
680 run-file ysh-convert "$@"
681}
682
683ysh-completion() {
684 run-file ysh-completion "$@"
685}
686
687ysh-stdlib() {
688 run-file ysh-stdlib "$@"
689}
690
691ysh-stdlib-args() {
692 run-file ysh-stdlib-args "$@"
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-control-flow() {
744 run-file ysh-control-flow "$@"
745}
746
747ysh-bugs() {
748 run-file ysh-bugs "$@"
749}
750
751ysh-builtins() {
752 run-file ysh-builtins "$@"
753}
754
755ysh-builtin-module() {
756 run-file ysh-builtin-module "$@"
757}
758
759ysh-builtin-eval() {
760 run-file ysh-builtin-eval "$@"
761}
762
763# Related to errexit-oil
764ysh-builtin-error() {
765 run-file ysh-builtin-error "$@"
766}
767
768ysh-builtin-meta() {
769 run-file ysh-builtin-meta "$@"
770}
771
772ysh-builtin-process() {
773 run-file ysh-builtin-process "$@"
774}
775
776ysh-builtin-shopt() {
777 run-file ysh-builtin-shopt "$@"
778}
779
780ysh-case() {
781 run-file ysh-case "$@"
782}
783
784ysh-command-sub() {
785 run-file ysh-command-sub "$@"
786}
787
788ysh-demo() {
789 run-file ysh-demo "$@"
790}
791
792ysh-env() {
793 run-file ysh-env "$@"
794}
795
796ysh-expr() {
797 run-file ysh-expr "$@"
798}
799
800ysh-int-float() {
801 run-file ysh-int-float "$@"
802}
803
804ysh-expr-bool() {
805 run-file ysh-expr-bool "$@"
806}
807
808ysh-expr-arith() {
809 run-file ysh-expr-arith "$@"
810}
811
812ysh-expr-compare() {
813 run-file ysh-expr-compare "$@"
814}
815
816ysh-expr-sub() {
817 run-file ysh-expr-sub "$@"
818}
819
820ysh-cmd-lang() {
821 run-file ysh-cmd-lang "$@"
822}
823
824ysh-for() {
825 run-file ysh-for "$@"
826}
827
828ysh-methods() {
829 run-file ysh-methods "$@"
830}
831
832ysh-method-io() {
833 run-file ysh-method-io "$@"
834}
835
836ysh-namespaces() {
837 run-file ysh-namespaces "$@"
838}
839
840ysh-object() {
841 run-file ysh-object "$@"
842}
843
844ysh-closures() {
845 run-file ysh-closures "$@"
846}
847
848ysh-func() {
849 run-file ysh-func "$@"
850}
851
852ysh-func-builtin() {
853 run-file ysh-func-builtin "$@"
854}
855
856ysh-funcs-external() {
857 run-file ysh-funcs-external "$@"
858}
859
860ysh-interactive() {
861 run-file ysh-interactive "$@"
862}
863
864ysh-json() {
865 run-file ysh-json "$@"
866}
867
868ysh-keywords() {
869 run-file ysh-keywords "$@"
870}
871
872ysh-multiline() {
873 run-file ysh-multiline "$@"
874}
875
876ysh-options() {
877 run-file ysh-options "$@"
878}
879
880ysh-options-assign() {
881 run-file ysh-options-assign "$@"
882}
883
884ysh-proc() {
885 run-file ysh-proc "$@"
886}
887
888ysh-proc-meta() {
889 run-file ysh-proc-meta "$@"
890}
891
892ysh-regex() {
893 run-file ysh-regex "$@"
894}
895
896ysh-regex-api() {
897 run-file ysh-regex-api "$@"
898}
899
900ysh-reserved() {
901 run-file ysh-reserved "$@"
902}
903
904ysh-scope() {
905 run-file ysh-scope "$@"
906}
907
908ysh-slice-range() {
909 run-file ysh-slice-range "$@"
910}
911
912ysh-string() {
913 run-file ysh-string "$@"
914}
915
916ysh-special-vars() {
917 run-file ysh-special-vars "$@"
918}
919
920ysh-tuple() {
921 run-file ysh-tuple "$@"
922}
923
924ysh-var-sub() {
925 run-file ysh-var-sub "$@"
926}
927
928ysh-with-sh() {
929 run-file ysh-with-sh "$@"
930}
931
932ysh-word-eval() {
933 run-file ysh-word-eval "$@"
934}
935
936ysh-xtrace() {
937 run-file ysh-xtrace "$@"
938}
939
940ysh-user-feedback() {
941 run-file ysh-user-feedback "$@"
942}
943
944ysh-builtin-ctx() {
945 run-file ysh-builtin-ctx "$@"
946}
947
948ysh-builtin-error() {
949 run-file ysh-builtin-error "$@"
950}
951
952ysh-builtin-help() {
953 run-file ysh-builtin-help "$@"
954}
955
956ysh-dev() {
957 run-file ysh-dev "$@"
958}
959
960ysh-printing() {
961 run-file ysh-printing "$@"
962}
963
964
965#
966# More OSH
967#
968
969nix-idioms() {
970 run-file nix-idioms "$@"
971}
972
973zsh-idioms() {
974 run-file zsh-idioms "$@"
975}
976
977ble-idioms() {
978 sh-spec spec/ble-idioms.test.sh \
979 $BASH $ZSH $MKSH $BUSYBOX_ASH $OSH_LIST "$@"
980}
981
982ble-features() {
983 run-file ble-features "$@"
984}
985
986toysh() {
987 run-file toysh "$@"
988}
989
990toysh-posix() {
991 run-file toysh-posix "$@"
992}
993
994task-five "$@"