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

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