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 #### simple_test_builtin
327
328 test -n "foo"
329 echo status=$?
330
331 test -n "foo" -a -n "bar"
332 echo status=$?
333
334 [ -n foo ]
335 echo status=$?
336
337 shopt --set ysh:all
338 shopt --unset errexit
339
340 test -n "foo" -a -n "bar"
341 echo status=$?
342
343 [ -n foo ]
344 echo status=$?
345
346 test -z foo
347 echo status=$?
348
349 ## STDOUT:
350 status=0
351 status=0
352 status=0
353 status=2
354 status=2
355 status=1
356 ## END
357
358 #### long flags to test
359 # no options necessary!
360
361 test --dir /
362 echo status=$?
363
364 touch foo
365 test --file foo
366 echo status=$?
367
368 test --exists /
369 echo status=$?
370
371 test --symlink foo
372 echo status=$?
373
374 test --typo foo
375 echo status=$?
376
377 ## STDOUT:
378 status=0
379 status=0
380 status=0
381 status=1
382 status=2
383 ## END
384
385
386 #### push-registers
387 shopt --set ysh:upgrade
388 shopt --unset errexit
389
390 status_code() {
391 return $1
392 }
393
394 [[ foo =~ (.*) ]]
395
396 status_code 42
397 push-registers {
398 status_code 43
399 echo status=$?
400
401 [[ bar =~ (.*) ]]
402 echo ${BASH_REMATCH[@]}
403 }
404 # WEIRD SEMANTIC TO REVISIT: push-registers is "SILENT" as far as exit code
405 # This is for the headless shell, but hasn't been tested.
406 # Better method: maybe we should provide a way of SETTING $?
407
408 echo status=$?
409
410 echo ${BASH_REMATCH[@]}
411 ## STDOUT:
412 status=43
413 bar bar
414 status=42
415 foo foo
416 ## END
417
418 #### push-registers usage
419 shopt --set parse_brace
420
421 push-registers
422 echo status=$?
423
424 push-registers a b
425 echo status=$?
426
427 push-registers a b { # hm extra args are ignored
428 echo hi
429 }
430 echo status=$?
431
432 ## STDOUT:
433 status=2
434 status=2
435 hi
436 status=0
437 ## END
438
439 #### fopen
440 shopt --set parse_brace parse_proc
441
442 proc p {
443 echo 'proc'
444 }
445
446 fopen >out.txt {
447 p
448 echo 'builtin'
449 }
450
451 cat out.txt
452
453 echo ---
454
455 fopen <out.txt {
456 tac
457 }
458
459 # Awkward bash syntax, but we'll live with it
460 fopen {left}>left.txt {right}>right.txt {
461 echo 1 >& $left
462 echo 1 >& $right
463
464 echo 2 >& $left
465 echo 2 >& $right
466
467 echo 3 >& $left
468 }
469
470 echo ---
471 comm -23 left.txt right.txt
472
473 ## STDOUT:
474 proc
475 builtin
476 ---
477 builtin
478 proc
479 ---
480 3
481 ## END
482
483 #### type(x)
484 echo $[type(1234)]
485 echo $[type('foo')]
486 echo $[type(false)]
487 echo $[type(1.234)]
488 echo $[type([])]
489 echo $[type({})]
490 echo $[type(null)]
491
492 shopt --set ysh:upgrade
493
494 func f() {
495 return (42)
496 }
497
498 echo $[type(f)]
499 echo $[type(len)]
500 echo $[type('foo'->startsWith)]
501 echo $[type('foo'=>join)] # Type error happens later
502 echo $[type(1..3)]
503 ## STDOUT:
504 Int
505 Str
506 Bool
507 Float
508 List
509 Dict
510 Null
511 Func
512 BuiltinFunc
513 BoundFunc
514 BoundFunc
515 Range
516 ## END
517
518 #### source ///osh/two.sh and $LIB_OSH
519
520 source ///osh/two.sh
521 echo status=$?
522
523 source $LIB_OSH/two.sh
524 echo status=$?
525
526 # errors
527 source ///
528 echo status=$?
529 source ///x
530 echo status=$?
531
532 ## STDOUT:
533 status=0
534 status=0
535 status=2
536 status=2
537 ## END