OILS / spec / ysh-builtins.test.sh View on Github | oilshell.org

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