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

192 lines, 64 significant
1
2#### Bad var sub
3echo $%
4## stdout: $%
5
6#### Bad braced var sub -- not allowed
7echo ${%}
8## status: 2
9## OK bash/mksh status: 1
10
11#### Bad var sub caught at parse time
12if test -f /; then
13 echo ${%}
14else
15 echo ok
16fi
17## status: 2
18## BUG dash/bash/mksh status: 0
19
20#### Incomplete while
21echo hi; while
22echo status=$?
23## status: 2
24## stdout-json: ""
25## OK mksh status: 1
26
27#### Incomplete for
28echo hi; for
29echo status=$?
30## status: 2
31## stdout-json: ""
32## OK mksh status: 1
33
34#### Incomplete if
35echo hi; if
36echo status=$?
37## status: 2
38## stdout-json: ""
39## OK mksh status: 1
40
41#### do unexpected
42do echo hi
43## status: 2
44## stdout-json: ""
45## OK mksh status: 1
46
47#### } is a parse error
48}
49echo should not get here
50## stdout-json: ""
51## status: 2
52## OK mksh status: 1
53
54#### { is its own word, needs a space
55# bash and mksh give parse time error because of }
56# dash gives 127 as runtime error
57{ls; }
58echo "status=$?"
59## stdout-json: ""
60## status: 2
61## OK mksh status: 1
62
63#### } on the second line
64set -o errexit
65{ls;
66}
67## status: 127
68
69#### Invalid for loop variable name
70for i.j in a b c; do
71 echo hi
72done
73echo done
74## stdout-json: ""
75## status: 2
76## OK mksh status: 1
77## OK bash status: 0
78## BUG bash stdout: done
79
80#### bad var name globally isn't parsed like an assignment
81# bash and dash disagree on exit code.
82FOO-BAR=foo
83## status: 127
84
85#### bad var name in export
86# bash and dash disagree on exit code.
87export FOO-BAR=foo
88## status: 1
89## OK dash status: 2
90
91#### bad var name in local
92# bash and dash disagree on exit code.
93f() {
94 local FOO-BAR=foo
95}
96f
97## status: 1
98## OK dash status: 2
99
100#### misplaced parentheses are not a subshell
101echo a(b)
102## status: 2
103## OK mksh status: 1
104
105#### incomplete command sub
106$(x
107## status: 2
108## OK mksh status: 1
109
110#### incomplete backticks
111`x
112## status: 2
113## OK mksh status: 1
114
115#### misplaced ;;
116echo 1 ;; echo 2
117## stdout-json: ""
118## status: 2
119## OK mksh status: 1
120
121#### empty clause in [[
122# regression test for commit 451ca9e2b437e0326fc8155783d970a6f32729d8
123[[ || true ]]
124## status: 2
125## N-I dash status: 0
126## OK mksh status: 1
127
128#### interactive parse error (regression)
129flags=''
130case $SH in
131 *bash|*osh)
132 flags='--rcfile /dev/null'
133 ;;
134esac
135$SH $flags -i -c 'var=)'
136
137## status: 2
138## OK bash/mksh status: 1
139
140#### array literal inside array is a parse error
141a=( inside=() )
142echo len=${#a[@]}
143## status: 2
144## stdout-json: ""
145## OK mksh status: 1
146## BUG bash status: 0
147## BUG bash stdout: len=0
148
149#### array literal inside loop is a parse error
150f() {
151 for x in a=(); do
152 echo $x
153 done
154 echo done
155}
156## status: 2
157## stdout-json: ""
158## OK mksh status: 1
159
160#### array literal in case
161f() {
162 case a=() in
163 foo)
164 echo hi
165 ;;
166 esac
167}
168## status: 2
169## stdout-json: ""
170## OK mksh status: 1
171
172#### %foo=() is parse error (regression)
173
174# Lit_VarLike and then (, but NOT at the beginning of a word.
175
176f() {
177 %foo=()
178}
179## status: 2
180## stdout-json: ""
181## OK mksh status: 1
182
183#### leading =word is not allowed regardless of shopt -s parse_equals
184=word
185## OK osh status: 2
186## status: 127
187
188#### echo =word is allowed
189echo =word
190## STDOUT:
191=word
192## END