1 | #### cd with block
|
2 | shopt -s ysh:all
|
3 |
|
4 | const saved = "$PWD"
|
5 |
|
6 | # OLDPWD is NOT defined
|
7 | cd / { echo $PWD; echo OLDPWD=${OLDPWD:-} }; echo done
|
8 |
|
9 | if ! test "$saved" = $PWD; then
|
10 | echo FAIL
|
11 | fi
|
12 |
|
13 | cd /tmp {
|
14 | write PWD=$PWD
|
15 | write --sep ' ' pwd builtin: $(pwd)
|
16 | }
|
17 |
|
18 | if ! test "$saved" = $PWD; then
|
19 | echo FAIL
|
20 | fi
|
21 |
|
22 | ## STDOUT:
|
23 | /
|
24 | OLDPWD=
|
25 | done
|
26 | PWD=/tmp
|
27 | pwd builtin: /tmp
|
28 | ## END
|
29 |
|
30 | #### cd with block: fatal error in block
|
31 | shopt -s oil:all
|
32 | cd / {
|
33 | echo one
|
34 | false
|
35 | echo two
|
36 | }
|
37 | ## status: 1
|
38 | ## STDOUT:
|
39 | one
|
40 | ## END
|
41 |
|
42 |
|
43 | #### cd with block: return in block
|
44 | shopt -s oil:all
|
45 | f() {
|
46 | cd / {
|
47 | echo one
|
48 | return
|
49 | echo two
|
50 | }
|
51 | echo 'end func'
|
52 | }
|
53 | f
|
54 | ## STDOUT:
|
55 | one
|
56 | end func
|
57 | ## END
|
58 |
|
59 | #### cd with block: break in block
|
60 | shopt -s oil:all
|
61 | f() {
|
62 | cd / {
|
63 | echo one
|
64 | for i in 1 2; do
|
65 | echo $i
|
66 | break # break out of loop
|
67 | done
|
68 |
|
69 | break # break out of block isn't valid
|
70 | echo two
|
71 | }
|
72 | echo end func
|
73 | }
|
74 | f
|
75 | ## status: 1
|
76 | ## STDOUT:
|
77 | one
|
78 | 1
|
79 | ## END
|
80 |
|
81 | #### cd with block exits with status 0
|
82 | shopt -s ysh:all
|
83 | cd / {
|
84 | echo block
|
85 |
|
86 | # This return value is ignored.
|
87 | # Or maybe this should be a runtime error?
|
88 | return 1
|
89 | }
|
90 | echo status=$?
|
91 | ## STDOUT:
|
92 | block
|
93 | status=0
|
94 | ## END
|
95 |
|
96 | #### block doesn't have its own scope
|
97 | shopt -s ysh:all
|
98 | var x = 1
|
99 | echo "x=$x"
|
100 | cd / {
|
101 | #set y = 5 # This would be an error because set doesn't do dynamic lookup
|
102 | var x = 42
|
103 | echo "x=$x"
|
104 | }
|
105 | echo "x=$x"
|
106 | ## STDOUT:
|
107 | x=1
|
108 | x=42
|
109 | x=42
|
110 | ## END
|
111 |
|
112 | #### redirects allowed in words, typed args, and after block
|
113 | shopt -s ysh:upgrade
|
114 |
|
115 | rm -f out
|
116 | touch out
|
117 |
|
118 | cd /tmp >>out {
|
119 | echo 1 $PWD
|
120 | }
|
121 |
|
122 | cd /tmp >>out (; ; ^(echo 2 $PWD))
|
123 |
|
124 | cd /tmp (; ; ^(echo 3 $PWD)) >>out
|
125 |
|
126 | cd /tmp {
|
127 | echo 4 $PWD
|
128 | } >> out
|
129 |
|
130 | cat out
|
131 |
|
132 | ## STDOUT:
|
133 | 1 /tmp
|
134 | 2 /tmp
|
135 | 3 /tmp
|
136 | 4 /tmp
|
137 | ## END
|
138 |
|
139 | #### block literal in expression mode: ^(echo $PWD)
|
140 | shopt -s oil:all
|
141 |
|
142 | const myblock = ^(echo $PWD | wc -l)
|
143 | eval (myblock)
|
144 |
|
145 | const b2 = ^(echo one; echo two)
|
146 | eval (b2)
|
147 |
|
148 | ## STDOUT:
|
149 | 1
|
150 | one
|
151 | two
|
152 | ## END
|
153 |
|
154 | #### block arg as typed expression
|
155 |
|
156 | shopt -s oil:all
|
157 |
|
158 | # literal
|
159 | cd /tmp (; ; ^(echo $PWD))
|
160 |
|
161 | const myblock = ^(echo $PWD)
|
162 | cd /tmp (; ; myblock)
|
163 |
|
164 | ## STDOUT:
|
165 | /tmp
|
166 | /tmp
|
167 | ## END
|
168 |
|
169 | #### Pass invalid typed args
|
170 |
|
171 | cd /tmp (42) # should be a block
|
172 | ## status: 3
|
173 |
|
174 | #### Pass too many typed args
|
175 |
|
176 | cd /tmp (1, 2)
|
177 | ## status: 3
|
178 |
|
179 | #### 'builtin' and 'command' with block
|
180 | shopt --set oil:upgrade
|
181 | builtin cd / {
|
182 | echo "builtin $PWD"
|
183 | }
|
184 | command cd / {
|
185 | echo "command $PWD"
|
186 | }
|
187 | ## STDOUT:
|
188 | builtin /
|
189 | command /
|
190 | ## END
|
191 |
|
192 |
|
193 | #### Consistency: Control Flow and Blocks
|
194 | shopt --set parse_brace
|
195 |
|
196 | # "Invalid control flow at top level"
|
197 | eval '
|
198 | cd / {
|
199 | echo cd
|
200 | break
|
201 | }
|
202 | '
|
203 | echo cd no loop $?
|
204 |
|
205 | # warning: "Unexpected control flow in block" (strict_control_flow)
|
206 | eval '
|
207 | while true {
|
208 | cd / {
|
209 | echo cd
|
210 | break
|
211 | }
|
212 | }
|
213 | '
|
214 | echo cd loop $?
|
215 |
|
216 | eval '
|
217 | while true {
|
218 | shopt --unset errexit {
|
219 | echo shopt
|
220 | continue
|
221 | }
|
222 | }
|
223 | '
|
224 | echo shopt continue $?
|
225 |
|
226 | eval '
|
227 | while true {
|
228 | shvar FOO=foo {
|
229 | echo shvar
|
230 | continue
|
231 | }
|
232 | }
|
233 | '
|
234 | echo shvar continue $?
|
235 |
|
236 |
|
237 | eval '
|
238 | while true {
|
239 | try {
|
240 | echo try
|
241 | break
|
242 | }
|
243 | }
|
244 | '
|
245 | echo try break $?
|
246 |
|
247 | ## STDOUT:
|
248 | cd
|
249 | cd no loop 0
|
250 | cd
|
251 | cd loop 1
|
252 | shopt
|
253 | shopt continue 1
|
254 | shvar
|
255 | shvar continue 1
|
256 | try
|
257 | try break 1
|
258 | ## END
|
259 |
|
260 | #### Consistency: Exit Status and Blocks
|
261 | shopt --set parse_brace
|
262 |
|
263 | cd / {
|
264 | false
|
265 | }
|
266 | echo cd=$?
|
267 |
|
268 | shopt --unset errexit {
|
269 | false
|
270 | }
|
271 | echo shopt=$?
|
272 |
|
273 | shvar FOO=foo {
|
274 | echo " FOO=$FOO"
|
275 | false
|
276 | }
|
277 | echo shvar=$?
|
278 |
|
279 | try {
|
280 | false
|
281 | }
|
282 | echo try=$?
|
283 |
|
284 | ## STDOUT:
|
285 | cd=0
|
286 | shopt=0
|
287 | FOO=foo
|
288 | shvar=0
|
289 | try=0
|
290 | ## END
|
291 |
|
292 | #### Consistency: Unwanted Blocks Are Errors
|
293 | shopt --set parse_brace
|
294 |
|
295 | true { echo BAD }
|
296 | echo true $?
|
297 |
|
298 | false ( 42, 43 )
|
299 | echo false $?
|
300 |
|
301 | echo { echo BAD }
|
302 | echo echo block $?
|
303 |
|
304 | echo ( 42, 43 )
|
305 | echo echo args $?
|
306 |
|
307 | command echo 'command block' { echo BAD }
|
308 | echo command echo $?
|
309 |
|
310 | builtin echo 'builtin block' { echo BAD }
|
311 | echo builtin echo $?
|
312 |
|
313 | pushd $TMP { echo BAD }
|
314 | echo pushd $?
|
315 |
|
316 | ## STDOUT:
|
317 | true 2
|
318 | false 2
|
319 | echo block 2
|
320 | echo args 2
|
321 | command echo 2
|
322 | builtin echo 2
|
323 | pushd 2
|
324 | ## END
|
325 |
|
326 | #### Block with Bare Assignments
|
327 |
|
328 | # oil:all has parse_equals
|
329 | # is there any way to turn on parse_equals only in config blocks?
|
330 | # but we don't know what's a block ahead of time
|
331 | # I think we would have to check at runtime. Look at VarChecker
|
332 |
|
333 | shopt --set oil:all
|
334 |
|
335 | proc Rule(s ; ; ; b) {
|
336 | echo "rule $s"
|
337 | }
|
338 |
|
339 | proc myrules(name) {
|
340 | Rule $name-python {
|
341 | kind = 'python'
|
342 | }
|
343 |
|
344 | Rule $name-cc {
|
345 | kind = 'cc' # should NOT conflict
|
346 | }
|
347 | }
|
348 |
|
349 | myrules foo
|
350 | myrules bar
|
351 |
|
352 | ## STDOUT:
|
353 | rule foo-python
|
354 | rule foo-cc
|
355 | rule bar-python
|
356 | rule bar-cc
|
357 | ## END
|
358 |
|
359 | #### Proc that doesn't take a block
|
360 | shopt --set parse_brace parse_proc parse_paren
|
361 |
|
362 | proc task(name ; ; ; b = null) {
|
363 | echo "task name=$name"
|
364 | if (b) {
|
365 | eval (b)
|
366 | return 33
|
367 | } else {
|
368 | echo 'no block'
|
369 | return 44
|
370 | }
|
371 | }
|
372 |
|
373 | task spam
|
374 | echo status=$?
|
375 |
|
376 | echo
|
377 |
|
378 | task foo {
|
379 | echo 'running'
|
380 | echo 'block'
|
381 | }
|
382 | echo status=$?
|
383 |
|
384 | ## STDOUT:
|
385 | task name=spam
|
386 | no block
|
387 | status=44
|
388 |
|
389 | task name=foo
|
390 | running
|
391 | block
|
392 | status=33
|
393 | ## END
|
394 |
|