1 ## oils_failures_allowed: 3
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) is usage error
149
150 var x = null # allow no initialization
151 echo hello | read (&x)
152 echo status=$?
153
154 ## STDOUT:
155 status=2
156 ## END
157
158 #### read --raw-line
159
160 echo hi | read --raw-line
161 echo "reply=$_reply"
162 echo len=$[len(_reply)]
163
164 echo hi | read -r
165 if test "$REPLY" = "$_reply"; then
166 echo pass
167 fi
168
169 ## STDOUT:
170 reply=hi
171 len=2
172 pass
173 ## END
174
175 #### Mixing read --line with read -r
176
177 $SH $REPO_ROOT/spec/testdata/ysh-read-0.sh
178
179 ## STDOUT:
180 read -r
181 REPLY=1
182 REPLY=2
183
184 read --raw-line
185 _reply=1
186 _reply=2
187
188 Mixed
189 REPLY=1
190 REPLY=2
191 _reply=3
192 REPLY=4
193 ## END
194
195 #### read --line --with-eol
196
197 $SH $REPO_ROOT/spec/testdata/ysh-read-1.sh
198
199 ## STDOUT:
200 reply=1
201 reply=2
202 reply=3
203 myline=a
204 myline=b
205 ## END
206
207 #### read --raw-line --j8
208
209 # TODO: is this similar to @() ? It reads j8 lines?
210 #
211 # But using a function is better?
212 #
213 # var x = fromJ8Line(_reply)
214 # var x = fromJson(_reply) # this is https://jsonlines.org
215
216 echo $'u\'foo\'' | read --raw-line --j8
217 write -- "$_reply"
218
219 ## STDOUT:
220 foo
221 ## END
222
223 #### echo builtin should disallow typed args - literal
224 shopt -s ysh:all
225 #shopt -p simple_echo
226
227 echo (42)
228 ## status: 2
229 ## STDOUT:
230 ## END
231
232 #### echo builtin should disallow typed args - variable
233 shopt -s ysh:all
234 #shopt -p simple_echo
235
236 var x = 43
237 echo (x)
238 ## status: 2
239 ## STDOUT:
240 ## END
241
242 #### read --all-lines
243 seq 3 | read --all-lines :nums
244 write --sep ' ' -- @nums
245 ## STDOUT:
246 1 2 3
247 ## END
248
249 #### read --all-lines --with-eol
250 seq 3 | read --all-lines --with-eol :nums
251 write --sep '' -- @nums
252 ## STDOUT:
253 1
254 2
255 3
256 ## END
257
258 #### Can simulate read --all-lines with a proc and value.Place
259
260 $SH $REPO_ROOT/spec/testdata/ysh-read-2.sh
261
262 ## STDOUT:
263 [
264 "1",
265 "2",
266 "3"
267 ]
268 ## END
269
270 #### read --all
271 echo foo | read --all
272 echo "[$_reply]"
273
274 echo bad > tmp.txt
275 read --all (&x) < tmp.txt
276 echo "[$x]"
277
278 ## STDOUT:
279 [foo
280 ]
281 [bad
282 ]
283 ## END
284
285 #### read --all from directory is an error (EISDIR)
286 mkdir -p ./dir
287 read --all < ./dir
288 echo status=$?
289 ## STDOUT:
290 status=1
291 ## END
292
293 #### read --num-bytes
294
295 echo ' a b ' | read --num-bytes 4; echo "reply=[$_reply]"
296 echo ' a b ' | read --num-bytes 5; echo "reply=[$_reply]"
297
298 echo ' a b ' | read --num-bytes 4 (&x); echo "x=[$x]"
299 echo ' a b ' | read --num-bytes 5 (&x); echo "x=[$x]"
300
301 ## STDOUT:
302 reply=[ a ]
303 reply=[ a b]
304 x=[ a ]
305 x=[ a b]
306 ## END
307
308 #### read -0 is like read -r -d ''
309 set -o errexit
310
311 mkdir -p read0
312 cd read0
313 touch a\\b\\c\\d
314
315 find . -type f -a -print0 | read -r -d '' name
316 echo "[$name]"
317
318 find . -type f -a -print0 | read -0
319 echo "[$REPLY]"
320
321 ## STDOUT:
322 [./a\b\c\d]
323 [./a\b\c\d]
324 ## END
325
326 #### read -0 myvar doesn't do anything with IFS
327
328 touch 'foo bar '
329 find -type f -print0 | read -0
330 echo "[$REPLY]"
331
332 find -type f -print0 | read -0 myvar
333 echo "[$myvar]"
334
335 ## STDOUT:
336 [./foo bar ]
337 [./foo bar ]
338 ## END
339
340 #### simple_test_builtin
341
342 test -n "foo"
343 echo status=$?
344
345 test -n "foo" -a -n "bar"
346 echo status=$?
347
348 [ -n foo ]
349 echo status=$?
350
351 shopt --set ysh:all
352 shopt --unset errexit
353
354 test -n "foo" -a -n "bar"
355 echo status=$?
356
357 [ -n foo ]
358 echo status=$?
359
360 test -z foo
361 echo status=$?
362
363 ## STDOUT:
364 status=0
365 status=0
366 status=0
367 status=2
368 status=2
369 status=1
370 ## END
371
372 #### long flags to test
373 # no options necessary!
374
375 test --dir /
376 echo status=$?
377
378 touch foo
379 test --file foo
380 echo status=$?
381
382 test --exists /
383 echo status=$?
384
385 test --symlink foo
386 echo status=$?
387
388 test --typo foo
389 echo status=$?
390
391 ## STDOUT:
392 status=0
393 status=0
394 status=0
395 status=1
396 status=2
397 ## END
398
399
400 #### push-registers
401 shopt --set ysh:upgrade
402 shopt --unset errexit
403
404 status_code() {
405 return $1
406 }
407
408 [[ foo =~ (.*) ]]
409
410 status_code 42
411 push-registers {
412 status_code 43
413 echo status=$?
414
415 [[ bar =~ (.*) ]]
416 echo ${BASH_REMATCH[@]}
417 }
418 # WEIRD SEMANTIC TO REVISIT: push-registers is "SILENT" as far as exit code
419 # This is for the headless shell, but hasn't been tested.
420 # Better method: maybe we should provide a way of SETTING $?
421
422 echo status=$?
423
424 echo ${BASH_REMATCH[@]}
425 ## STDOUT:
426 status=43
427 bar bar
428 status=42
429 foo foo
430 ## END
431
432 #### push-registers usage
433 shopt --set parse_brace
434
435 push-registers
436 echo status=$?
437
438 push-registers a b
439 echo status=$?
440
441 push-registers a b { # hm extra args are ignored
442 echo hi
443 }
444 echo status=$?
445
446 ## STDOUT:
447 status=2
448 status=2
449 hi
450 status=0
451 ## END
452
453 #### fopen
454 shopt --set parse_brace parse_proc
455
456 proc p {
457 echo 'proc'
458 }
459
460 fopen >out.txt {
461 p
462 echo 'builtin'
463 }
464
465 cat out.txt
466
467 echo ---
468
469 fopen <out.txt {
470 tac
471 }
472
473 # Awkward bash syntax, but we'll live with it
474 fopen {left}>left.txt {right}>right.txt {
475 echo 1 >& $left
476 echo 1 >& $right
477
478 echo 2 >& $left
479 echo 2 >& $right
480
481 echo 3 >& $left
482 }
483
484 echo ---
485 comm -23 left.txt right.txt
486
487 ## STDOUT:
488 proc
489 builtin
490 ---
491 builtin
492 proc
493 ---
494 3
495 ## END
496
497 #### type(x)
498 echo $[type(1234)]
499 echo $[type('foo')]
500 echo $[type(false)]
501 echo $[type(1.234)]
502 echo $[type([])]
503 echo $[type({})]
504 echo $[type(null)]
505
506 shopt --set ysh:upgrade
507
508 func f() {
509 return (42)
510 }
511
512 echo $[type(f)]
513 echo $[type(len)]
514 echo $[type('foo'->startsWith)]
515 echo $[type('foo'=>join)] # Type error happens later
516 echo $[type(1..3)]
517 ## STDOUT:
518 Int
519 Str
520 Bool
521 Float
522 List
523 Dict
524 Null
525 Func
526 BuiltinFunc
527 BoundFunc
528 BoundFunc
529 Range
530 ## END
531
532 #### source ///osh/two.sh and $LIB_OSH
533
534 source ///osh/two.sh
535 echo status=$?
536
537 source $LIB_OSH/two.sh
538 echo status=$?
539
540 # errors
541 source ///
542 echo status=$?
543 source ///x
544 echo status=$?
545
546 ## STDOUT:
547 status=0
548 status=0
549 status=2
550 status=2
551 ## END