1 # Oil builtins
2
3 #### push onto a=(1 2)
4 shopt -s parse_at
5 a=(1 2)
6 push :a '3 4' '5'
7 argv.py @a
8 push -- :a 6
9 argv.py @a
10 ## STDOUT:
11 ['1', '2', '3 4', '5']
12 ['1', '2', '3 4', '5', '6']
13 ## END
14
15 #### push onto var a = %(1 2)
16 shopt -s parse_at
17 var a = %(1 2)
18 push a '3 4' '5' # : is optional
19 argv.py @a
20 ## STDOUT:
21 ['1', '2', '3 4', '5']
22 ## END
23
24 #### push with invalid type
25 s=''
26 push :s a b
27 echo status=$?
28 ## stdout: status=1
29
30 #### push with invalid var name
31 push - a b
32 echo status=$?
33 ## stdout: status=2
34
35 #### write -sep, -end, -n, varying flag syntax
36 shopt -s oil:all
37 var a = %('a b' 'c d')
38 write @a
39 write .
40 write -- @a
41 write .
42
43 write -sep '' -end '' @a; write
44 write .
45
46 write -sep '_' -- @a
47 write -sep '_' -end $' END\n' -- @a
48
49 # with =
50 write -sep='_' -end=$' END\n' -- @a
51 # long flags
52 write --sep '_' --end $' END\n' -- @a
53 # long flags with =
54 write --sep='_' --end=$' END\n' -- @a
55
56 write -n x
57 write -n y
58 write
59
60 ## STDOUT:
61 a b
62 c d
63 .
64 a b
65 c d
66 .
67 a bc d
68 .
69 a b_c d
70 a b_c d END
71 a b_c d END
72 a b_c d END
73 a b_c d END
74 xy
75 ## END
76
77 #### write --qsn
78 write --qsn foo bar
79 write __
80
81 write --qsn 'abc def' ' 123 456'
82 write __
83
84 write --qsn $'one\ttwo\n'
85
86 write __
87 write --qsn $'\u{3bc}'
88
89
90 ## STDOUT:
91 foo
92 bar
93 __
94 'abc def'
95 ' 123 456'
96 __
97 'one\ttwo\n'
98 __
99 'μ'
100 ## END
101
102
103 #### write --qsn --unicode
104 write --qsn $'\u{3bc}'
105 write --qsn --unicode u $'\u{3bc}'
106 write --qsn --unicode x $'\u{3bc}'
107
108 ## STDOUT:
109 'μ'
110 '\u{3bc}'
111 '\xce\xbc'
112 ## END
113
114 #### write -e not supported
115 shopt -s oil:all
116 write -e foo
117 write status=$?
118 ## stdout-json: ""
119 ## status: 2
120
121 #### write syntax error
122 shopt -s oil:all
123 write ---end foo
124 write status=$?
125 ## stdout-json: ""
126 ## status: 2
127
128 #### write --
129 shopt -s oil:all
130 write --
131 # This is annoying
132 write -- --
133 write done
134
135 # this is a syntax error! Doh.
136 write ---
137 ## status: 2
138 ## STDOUT:
139
140 --
141 done
142 ## END
143
144 #### read flag usage
145 read --lin
146 echo status=$?
147
148 read --line :var extra
149 echo status=$?
150 ## STDOUT:
151 status=2
152 status=2
153 ## END
154
155 #### read --line --with-eol
156 shopt -s oil:basic
157
158 # Hm this preserves the newline?
159 seq 3 | while read --line {
160 write line=$_line # implisict
161 }
162 write a b | while read --line --with-eol :myline {
163 write -end '' line=$myline
164 }
165 ## STDOUT:
166 line=1
167 line=2
168 line=3
169 line=a
170 line=b
171 ## END
172
173 #### read --line --qsn
174 read --line --qsn <<EOF
175 'foo\n'
176 EOF
177 write --qsn -- "$_line"
178
179 read --line --qsn <<EOF
180 'foo\tbar hex=\x01 mu=\u{3bc}'
181 EOF
182 write --qsn --unicode u -- "$_line"
183
184 ## STDOUT:
185 'foo\n'
186 'foo\tbar hex=\u{1} mu=\u{3bc}'
187 ## END
188
189 #### read --line --with-eol --qsn
190 read --line --with-eol --qsn <<EOF
191 'foo\n'
192 EOF
193 write --qsn -- "$_line"
194 ## STDOUT:
195 'foo\n'
196 ## END
197
198 #### read --qsn usage
199 read --qsn << EOF
200 foo
201 EOF
202 echo status=$?
203
204 ## STDOUT:
205 status=2
206 ## END
207
208 #### read --all
209 echo foo | read --all
210 echo "[$_all]"
211
212 echo bad > tmp.txt
213 read --all :x < tmp.txt
214 echo "[$x]"
215
216 ## STDOUT:
217 [foo
218 ]
219 [bad
220 ]
221 ## END
222
223 #### read -0 is like read -r -d ''
224 set -o errexit
225
226 mkdir -p read0
227 cd read0
228 touch a\\b\\c\\d
229
230 find . -type f -a -print0 | read -r -d '' name
231 echo "[$name]"
232
233 find . -type f -a -print0 | read -0
234 echo "[$REPLY]"
235
236 ## STDOUT:
237 [./a\b\c\d]
238 [./a\b\c\d]
239 ## END
240
241
242 #### shopt supports long flags
243 shopt -p nullglob
244
245 shopt --set nullglob
246 shopt -p nullglob
247
248 shopt --unset nullglob
249 shopt -p nullglob
250 ## STDOUT:
251 shopt -u nullglob
252 shopt -s nullglob
253 shopt -u nullglob
254 ## END
255
256 #### shopt supports 'set' options
257 shopt -p errexit
258
259 shopt --set errexit
260 false
261
262 echo should not get here
263 ## status: 1
264 ## STDOUT:
265 shopt -u errexit
266 ## END
267
268
269 #### shopt and block
270 shopt --set oil:all
271
272 echo one
273
274 shopt --unset errexit {
275 echo two
276 false
277 echo three
278 }
279
280 false
281 echo 'should not get here'
282
283 ## status: 1
284 ## STDOUT:
285 one
286 two
287 three
288 ## END
289
290 #### shopt and block status
291 shopt --set oil:all
292
293 shopt --unset errexit {
294 false
295 }
296 # this is still 0, even though last command was 1
297 echo status=$?
298
299 ## STDOUT:
300 status=0
301 ## END
302
303 #### shopt usage error
304 shopt --set oil:all
305
306 echo one
307 shopt --set a {
308 echo two
309 }
310 echo status=$?
311 ## status: 2
312 ## STDOUT:
313 one
314 ## END
315
316 #### shopt --print
317
318 # TODO: It would be nice to print long flags ...
319
320 shopt -p errexit
321 shopt -p nullglob
322
323 echo --
324 shopt -p strict:all | head -n 3
325
326 echo --
327 shopt --set strict:all
328 shopt -p strict:all | head -n 3
329
330 ## STDOUT:
331 shopt -u errexit
332 shopt -u nullglob
333 --
334 shopt -u errexit
335 shopt -u inherit_errexit
336 shopt -u nounset
337 --
338 shopt -s errexit
339 shopt -s inherit_errexit
340 shopt -s nounset
341 ## END
342
343 #### simple_test_builtin
344
345 test -n "foo"
346 echo status=$?
347
348 test -n "foo" -a -n "bar"
349 echo status=$?
350
351 [ -n foo ]
352 echo status=$?
353
354 shopt --set oil:all
355 shopt --unset errexit
356
357 test -n "foo" -a -n "bar"
358 echo status=$?
359
360 [ -n foo ]
361 echo status=$?
362
363 test -z foo
364 echo status=$?
365
366 ## STDOUT:
367 status=0
368 status=0
369 status=0
370 status=2
371 status=2
372 status=1
373 ## END
374
375 #### long flags to test
376 # no options necessary!
377
378 test --dir /
379 echo status=$?
380
381 touch foo
382 test --file foo
383 echo status=$?
384
385 test --exists /
386 echo status=$?
387
388 test --symlink foo
389 echo status=$?
390
391 test --typo foo
392 echo status=$?
393
394 ## STDOUT:
395 status=0
396 status=0
397 status=0
398 status=1
399 status=2
400 ## END
401