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

489 lines, 269 significant
1## oils_failures_allowed: 5
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 :y is allowed
149shopt --set parse_proc
150
151echo 'foo bar' | read :x :y
152echo x=$x y=$y
153
154proc 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}
162p
163
164echo x=$x y=$y
165
166## STDOUT:
167x=foo y=bar
168x=spam y=eggs
169x=foo y=bar
170## END
171
172#### read (&x) is usage error
173
174var x = null # allow no initialization
175echo hello | read (&x)
176echo status=$?
177
178## STDOUT:
179status=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:
187TODO
188## END
189
190#### read --line --with-eol
191
192$SH $REPO_ROOT/spec/testdata/ysh-read-1.sh
193
194## STDOUT:
195reply=1
196reply=2
197reply=3
198myline=a
199myline=b
200## END
201
202#### read --line --j8
203
204echo $'u\'foo\'' | read --line --j8
205write -- "$_reply"
206
207## STDOUT:
208foo
209## END
210
211#### echo builtin should disallow typed args - literal
212shopt -s ysh:all
213#shopt -p simple_echo
214
215echo (42)
216## status: 2
217## STDOUT:
218## END
219
220#### echo builtin should disallow typed args - variable
221shopt -s ysh:all
222#shopt -p simple_echo
223
224var x = 43
225echo (x)
226## status: 2
227## STDOUT:
228## END
229
230#### read --all-lines
231seq 3 | read --all-lines :nums
232write --sep ' ' -- @nums
233## STDOUT:
2341 2 3
235## END
236
237#### read --all-lines --with-eol
238seq 3 | read --all-lines --with-eol :nums
239write --sep '' -- @nums
240## STDOUT:
2411
2422
2433
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
259echo foo | read --all
260echo "[$_reply]"
261
262echo bad > tmp.txt
263read --all (&x) < tmp.txt
264echo "[$x]"
265
266## STDOUT:
267[foo
268]
269[bad
270]
271## END
272
273#### read --all from directory is an error (EISDIR)
274mkdir -p ./dir
275read --all < ./dir
276echo status=$?
277## STDOUT:
278status=1
279## END
280
281#### read -0 is like read -r -d ''
282set -o errexit
283
284mkdir -p read0
285cd read0
286touch a\\b\\c\\d
287
288find . -type f -a -print0 | read -r -d '' name
289echo "[$name]"
290
291find . -type f -a -print0 | read -0
292echo "[$REPLY]"
293
294## STDOUT:
295[./a\b\c\d]
296[./a\b\c\d]
297## END
298
299#### simple_test_builtin
300
301test -n "foo"
302echo status=$?
303
304test -n "foo" -a -n "bar"
305echo status=$?
306
307[ -n foo ]
308echo status=$?
309
310shopt --set ysh:all
311shopt --unset errexit
312
313test -n "foo" -a -n "bar"
314echo status=$?
315
316[ -n foo ]
317echo status=$?
318
319test -z foo
320echo status=$?
321
322## STDOUT:
323status=0
324status=0
325status=0
326status=2
327status=2
328status=1
329## END
330
331#### long flags to test
332# no options necessary!
333
334test --dir /
335echo status=$?
336
337touch foo
338test --file foo
339echo status=$?
340
341test --exists /
342echo status=$?
343
344test --symlink foo
345echo status=$?
346
347test --typo foo
348echo status=$?
349
350## STDOUT:
351status=0
352status=0
353status=0
354status=1
355status=2
356## END
357
358
359#### push-registers
360shopt --set ysh:upgrade
361shopt --unset errexit
362
363status_code() {
364 return $1
365}
366
367[[ foo =~ (.*) ]]
368
369status_code 42
370push-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
381echo status=$?
382
383echo ${BASH_REMATCH[@]}
384## STDOUT:
385status=43
386bar bar
387status=42
388foo foo
389## END
390
391#### push-registers usage
392shopt --set parse_brace
393
394push-registers
395echo status=$?
396
397push-registers a b
398echo status=$?
399
400push-registers a b { # hm extra args are ignored
401 echo hi
402}
403echo status=$?
404
405## STDOUT:
406status=2
407status=2
408hi
409status=0
410## END
411
412#### fopen
413shopt --set parse_brace parse_proc
414
415proc p {
416 echo 'proc'
417}
418
419fopen >out.txt {
420 p
421 echo 'builtin'
422}
423
424cat out.txt
425
426echo ---
427
428fopen <out.txt {
429 tac
430}
431
432# Awkward bash syntax, but we'll live with it
433fopen {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
443echo ---
444comm -23 left.txt right.txt
445
446## STDOUT:
447proc
448builtin
449---
450builtin
451proc
452---
4533
454## END
455
456#### type(x)
457echo $[type(1234)]
458echo $[type('foo')]
459echo $[type(false)]
460echo $[type(1.234)]
461echo $[type([])]
462echo $[type({})]
463echo $[type(null)]
464
465shopt --set ysh:upgrade
466
467func f() {
468 return (42)
469}
470
471echo $[type(f)]
472echo $[type(len)]
473echo $[type('foo'->startsWith)]
474echo $[type('foo'=>join)] # Type error happens later
475echo $[type(1..3)]
476## STDOUT:
477Int
478Str
479Bool
480Float
481List
482Dict
483Null
484Func
485BuiltinFunc
486BoundFunc
487BoundFunc
488Range
489## END