1 ## oils_failures_allowed: 5
2
3 #### append onto BashArray a=(1 2)
4 shopt -s parse_at
5 a=(1 2)
6 append '3 4' '5' (a)
7 argv.py "${a[@]}"
8
9 append -- 6 (a)
10 argv.py "${a[@]}"
11
12 ## STDOUT:
13 ['1', '2', '3 4', '5']
14 ['1', '2', '3 4', '5', '6']
15 ## END
16
17 #### append onto var a = :| 1 2 |
18 shopt -s parse_at parse_proc
19 var a = :| 1 2 |
20 append '3 4' '5' (a)
21 argv.py @a
22 ## STDOUT:
23 ['1', '2', '3 4', '5']
24 ## END
25
26 #### append onto var a = ['1', '2']
27 shopt -s parse_at parse_proc
28 var a = ['1', '2']
29 append '3 4' '5' (a)
30 argv.py @a
31 ## STDOUT:
32 ['1', '2', '3 4', '5']
33 ## END
34
35 #### append without typed arg
36 append a b
37 ## status: 3
38
39 #### append passed invalid type
40 s=''
41 append a b (s)
42 echo status=$?
43 ## status: 3
44
45 #### write --sep, --end, -n, varying flag syntax
46 shopt -s ysh:all
47 var a = %('a b' 'c d')
48 write @a
49 write .
50 write -- @a
51 write .
52
53 write --sep '' --end '' @a; write
54 write .
55
56 write --sep '_' -- @a
57 write --sep '_' --end $' END\n' -- @a
58
59 # with =
60 write --sep='_' --end=$' END\n' -- @a
61
62 write -n x
63 write -n y
64 write
65
66 ## STDOUT:
67 a b
68 c d
69 .
70 a b
71 c d
72 .
73 a bc d
74 .
75 a b_c d
76 a b_c d END
77 a b_c d END
78 xy
79 ## END
80
81 #### write --json
82 shopt --set ysh:upgrade
83
84 write --json u'\u{3bc}' x
85 write --json b'\yfe\yff' y
86
87 ## STDOUT:
88 "μ"
89 "x"
90 "��"
91 "y"
92 ## END
93
94 #### write --j8
95 shopt --set ysh:upgrade
96
97 write --j8 u'\u{3bc}' x
98 write --j8 b'\yfe\yff' y
99
100 ## STDOUT:
101 "μ"
102 "x"
103 b'\yfe\yff'
104 "y"
105 ## END
106
107 #### write -e not supported
108 shopt -s ysh:all
109 write -e foo
110 write status=$?
111 ## stdout-json: ""
112 ## status: 2
113
114 #### write syntax error
115 shopt -s ysh:all
116 write ---end foo
117 write status=$?
118 ## stdout-json: ""
119 ## status: 2
120
121 #### write --
122 shopt -s ysh:all
123 write --
124 # This is annoying
125 write -- --
126 write done
127
128 # this is a syntax error! Doh.
129 write ---
130 ## status: 2
131 ## STDOUT:
132
133 --
134 done
135 ## END
136
137 #### read flag usage
138 read --lin
139 echo status=$?
140
141 read --line :var extra
142 echo status=$?
143 ## STDOUT:
144 status=2
145 status=2
146 ## END
147
148 #### read :x :y is allowed
149 shopt --set parse_proc
150
151 echo 'foo bar' | read :x :y
152 echo x=$x y=$y
153
154 proc p {
155 # If these aren't here, it will LEAK because 'read' uses DYNAMIC SCOPE.
156 # TODO: Change semantics of : to be enforce that a local exists too?
157 var x = ''
158 var y = ''
159 echo 'spam eggs' | read x :y # OPTIONAL
160 echo x=$x y=$y
161 }
162 p
163
164 echo x=$x y=$y
165
166 ## STDOUT:
167 x=foo y=bar
168 x=spam y=eggs
169 x=foo y=bar
170 ## END
171
172 #### read (&x) is usage error
173
174 var x = null # allow no initialization
175 echo hello | read (&x)
176 echo status=$?
177
178 ## STDOUT:
179 status=2
180 ## END
181
182 #### Mixing read --line with read -r
183
184 $SH $REPO_ROOT/spec/testdata/ysh-read-0.sh
185
186 ## STDOUT:
187 TODO
188 ## END
189
190 #### read --line --with-eol
191
192 $SH $REPO_ROOT/spec/testdata/ysh-read-1.sh
193
194 ## STDOUT:
195 reply=1
196 reply=2
197 reply=3
198 myline=a
199 myline=b
200 ## END
201
202 #### read --line --j8
203
204 echo $'u\'foo\'' | read --line --j8
205 write -- "$_reply"
206
207 ## STDOUT:
208 foo
209 ## END
210
211 #### echo builtin should disallow typed args - literal
212 shopt -s ysh:all
213 #shopt -p simple_echo
214
215 echo (42)
216 ## status: 2
217 ## STDOUT:
218 ## END
219
220 #### echo builtin should disallow typed args - variable
221 shopt -s ysh:all
222 #shopt -p simple_echo
223
224 var x = 43
225 echo (x)
226 ## status: 2
227 ## STDOUT:
228 ## END
229
230 #### read --all-lines
231 seq 3 | read --all-lines :nums
232 write --sep ' ' -- @nums
233 ## STDOUT:
234 1 2 3
235 ## END
236
237 #### read --all-lines --with-eol
238 seq 3 | read --all-lines --with-eol :nums
239 write --sep '' -- @nums
240 ## STDOUT:
241 1
242 2
243 3
244 ## END
245
246 #### Can simulate read --all-lines with a proc and value.Place
247
248 $SH $REPO_ROOT/spec/testdata/ysh-read-2.sh
249
250 ## STDOUT:
251 [
252 "1",
253 "2",
254 "3"
255 ]
256 ## END
257
258 #### read --all
259 echo foo | read --all
260 echo "[$_reply]"
261
262 echo bad > tmp.txt
263 read --all (&x) < tmp.txt
264 echo "[$x]"
265
266 ## STDOUT:
267 [foo
268 ]
269 [bad
270 ]
271 ## END
272
273 #### read --all from directory is an error (EISDIR)
274 mkdir -p ./dir
275 read --all < ./dir
276 echo status=$?
277 ## STDOUT:
278 status=1
279 ## END
280
281 #### read -0 is like read -r -d ''
282 set -o errexit
283
284 mkdir -p read0
285 cd read0
286 touch a\\b\\c\\d
287
288 find . -type f -a -print0 | read -r -d '' name
289 echo "[$name]"
290
291 find . -type f -a -print0 | read -0
292 echo "[$REPLY]"
293
294 ## STDOUT:
295 [./a\b\c\d]
296 [./a\b\c\d]
297 ## END
298
299 #### simple_test_builtin
300
301 test -n "foo"
302 echo status=$?
303
304 test -n "foo" -a -n "bar"
305 echo status=$?
306
307 [ -n foo ]
308 echo status=$?
309
310 shopt --set ysh:all
311 shopt --unset errexit
312
313 test -n "foo" -a -n "bar"
314 echo status=$?
315
316 [ -n foo ]
317 echo status=$?
318
319 test -z foo
320 echo status=$?
321
322 ## STDOUT:
323 status=0
324 status=0
325 status=0
326 status=2
327 status=2
328 status=1
329 ## END
330
331 #### long flags to test
332 # no options necessary!
333
334 test --dir /
335 echo status=$?
336
337 touch foo
338 test --file foo
339 echo status=$?
340
341 test --exists /
342 echo status=$?
343
344 test --symlink foo
345 echo status=$?
346
347 test --typo foo
348 echo status=$?
349
350 ## STDOUT:
351 status=0
352 status=0
353 status=0
354 status=1
355 status=2
356 ## END
357
358
359 #### push-registers
360 shopt --set ysh:upgrade
361 shopt --unset errexit
362
363 status_code() {
364 return $1
365 }
366
367 [[ foo =~ (.*) ]]
368
369 status_code 42
370 push-registers {
371 status_code 43
372 echo status=$?
373
374 [[ bar =~ (.*) ]]
375 echo ${BASH_REMATCH[@]}
376 }
377 # WEIRD SEMANTIC TO REVISIT: push-registers is "SILENT" as far as exit code
378 # This is for the headless shell, but hasn't been tested.
379 # Better method: maybe we should provide a way of SETTING $?
380
381 echo status=$?
382
383 echo ${BASH_REMATCH[@]}
384 ## STDOUT:
385 status=43
386 bar bar
387 status=42
388 foo foo
389 ## END
390
391 #### push-registers usage
392 shopt --set parse_brace
393
394 push-registers
395 echo status=$?
396
397 push-registers a b
398 echo status=$?
399
400 push-registers a b { # hm extra args are ignored
401 echo hi
402 }
403 echo status=$?
404
405 ## STDOUT:
406 status=2
407 status=2
408 hi
409 status=0
410 ## END
411
412 #### fopen
413 shopt --set parse_brace parse_proc
414
415 proc p {
416 echo 'proc'
417 }
418
419 fopen >out.txt {
420 p
421 echo 'builtin'
422 }
423
424 cat out.txt
425
426 echo ---
427
428 fopen <out.txt {
429 tac
430 }
431
432 # Awkward bash syntax, but we'll live with it
433 fopen {left}>left.txt {right}>right.txt {
434 echo 1 >& $left
435 echo 1 >& $right
436
437 echo 2 >& $left
438 echo 2 >& $right
439
440 echo 3 >& $left
441 }
442
443 echo ---
444 comm -23 left.txt right.txt
445
446 ## STDOUT:
447 proc
448 builtin
449 ---
450 builtin
451 proc
452 ---
453 3
454 ## END
455
456 #### type(x)
457 echo $[type(1234)]
458 echo $[type('foo')]
459 echo $[type(false)]
460 echo $[type(1.234)]
461 echo $[type([])]
462 echo $[type({})]
463 echo $[type(null)]
464
465 shopt --set ysh:upgrade
466
467 func f() {
468 return (42)
469 }
470
471 echo $[type(f)]
472 echo $[type(len)]
473 echo $[type('foo'->startsWith)]
474 echo $[type('foo'=>join)] # Type error happens later
475 echo $[type(1..3)]
476 ## STDOUT:
477 Int
478 Str
479 Bool
480 Float
481 List
482 Dict
483 Null
484 Func
485 BuiltinFunc
486 BoundFunc
487 BoundFunc
488 Range
489 ## END