OILS / spec / builtin-meta.test.sh View on Github | oilshell.org

518 lines, 299 significant
1## oils_failures_allowed: 1
2## compare_shells: dash bash mksh zsh
3
4#### command -v
5myfunc() { echo x; }
6command -v echo
7echo $?
8
9command -v myfunc
10echo $?
11
12command -v nonexistent # doesn't print anything
13echo nonexistent=$?
14
15command -v '' # BUG FIX, shouldn't succeed
16echo empty=$?
17
18command -v for
19echo $?
20
21## STDOUT:
22echo
230
24myfunc
250
26nonexistent=1
27empty=1
28for
290
30## OK dash STDOUT:
31echo
320
33myfunc
340
35nonexistent=127
36empty=127
37for
380
39## END
40
41#### command -v with multiple names
42# ALL FOUR SHELLS behave differently here!
43#
44# bash chooses to swallow the error! We agree with zsh if ANY word lookup
45# fails, then the whole thing fails.
46
47myfunc() { echo x; }
48command -v echo myfunc ZZZ for
49echo status=$?
50
51## STDOUT:
52echo
53myfunc
54for
55status=1
56## BUG bash STDOUT:
57echo
58myfunc
59for
60status=0
61## BUG dash STDOUT:
62echo
63status=0
64## OK mksh STDOUT:
65echo
66myfunc
67status=1
68## END
69
70#### command -v doesn't find non-executable file
71# PATH resolution is different
72
73PATH="_tmp:$PATH"
74touch _tmp/non-executable _tmp/executable
75chmod +x _tmp/executable
76
77command -v _tmp/non-executable
78echo status=$?
79
80command -v _tmp/executable
81echo status=$?
82
83## STDOUT:
84status=1
85_tmp/executable
86status=0
87## END
88
89## BUG dash STDOUT:
90_tmp/non-executable
91status=0
92_tmp/executable
93status=0
94## END
95
96#### command -V
97myfunc() { echo x; }
98
99shopt -s expand_aliases
100alias ll='ls -l'
101
102backtick=\`
103command -V ll | sed "s/$backtick/'/g"
104echo status=$?
105
106command -V echo
107echo status=$?
108
109command -V myfunc
110echo status=$?
111
112command -V nonexistent # doesn't print anything
113echo status=$?
114
115command -V for
116echo status=$?
117
118## STDOUT:
119ll is an alias for "ls -l"
120status=0
121echo is a shell builtin
122status=0
123myfunc is a shell function
124status=0
125status=1
126for is a shell keyword
127status=0
128## END
129
130## OK zsh STDOUT:
131ll is an alias for ls -l
132status=0
133echo is a shell builtin
134status=0
135myfunc is a shell function
136status=0
137nonexistent not found
138status=1
139for is a reserved word
140status=0
141## END
142
143## OK bash STDOUT:
144ll is aliased to 'ls -l'
145status=0
146echo is a shell builtin
147status=0
148myfunc is a function
149myfunc ()
150{
151 echo x
152}
153status=0
154status=1
155for is a shell keyword
156status=0
157## END
158
159## OK mksh STDOUT:
160ll is an alias for 'ls -l'
161status=0
162echo is a shell builtin
163status=0
164myfunc is a function
165status=0
166nonexistent not found
167status=1
168for is a reserved word
169status=0
170## END
171
172## OK dash STDOUT:
173ll is an alias for ls -l
174status=0
175echo is a shell builtin
176status=0
177myfunc is a shell function
178status=0
179nonexistent: not found
180status=127
181for is a shell keyword
182status=0
183## END
184
185#### command -V nonexistent
186command -V nonexistent 2>err.txt
187echo status=$?
188fgrep -o 'nonexistent: not found' err.txt || true
189
190## STDOUT:
191status=1
192nonexistent: not found
193## END
194
195## OK zsh/mksh STDOUT:
196nonexistent not found
197status=1
198## END
199
200## BUG dash STDOUT:
201nonexistent: not found
202status=127
203## END
204
205
206#### command skips function lookup
207seq() {
208 echo "$@"
209}
210command # no-op
211seq 3
212command seq 3
213# subshell shouldn't fork another process (but we don't have a good way of
214# testing it)
215( command seq 3 )
216## STDOUT:
2173
2181
2192
2203
2211
2222
2233
224## END
225
226#### command command seq 3
227command command seq 3
228## STDOUT:
2291
2302
2313
232## END
233## N-I zsh stdout-json: ""
234## N-I zsh status: 127
235
236#### command command -v seq
237seq() {
238 echo 3
239}
240command command -v seq
241## stdout: seq
242## N-I zsh stdout-json: ""
243## N-I zsh status: 127
244
245#### command -p (override existing program)
246# Tests whether command -p overrides the path
247# tr chosen because we need a simple non-builtin
248mkdir -p $TMP/bin
249echo "echo wrong" > $TMP/bin/tr
250chmod +x $TMP/bin/tr
251PATH="$TMP/bin:$PATH"
252echo aaa | tr "a" "b"
253echo aaa | command -p tr "a" "b"
254rm $TMP/bin/tr
255## STDOUT:
256wrong
257bbb
258## END
259
260#### command -p (hide tool in custom path)
261mkdir -p $TMP/bin
262echo "echo hello" > $TMP/bin/hello
263chmod +x $TMP/bin/hello
264export PATH=$TMP/bin
265command -p hello
266## status: 127
267
268#### command -p (find hidden tool in default path)
269export PATH=''
270command -p ls
271## status: 0
272
273
274#### $(command type ls)
275type() { echo FUNCTION; }
276type
277s=$(command type echo)
278echo $s | grep builtin > /dev/null
279echo status=$?
280## STDOUT:
281FUNCTION
282status=0
283## END
284## N-I zsh STDOUT:
285FUNCTION
286status=1
287## END
288## N-I mksh STDOUT:
289status=1
290## END
291
292#### builtin
293cd () { echo "hi"; }
294cd
295builtin cd / && pwd
296unset -f cd
297## STDOUT:
298hi
299/
300## END
301## N-I dash STDOUT:
302hi
303## END
304
305#### builtin ls not found
306builtin ls
307## status: 1
308## N-I dash status: 127
309
310#### builtin no args
311builtin
312## status: 0
313## N-I dash status: 127
314
315#### builtin command echo hi
316builtin command echo hi
317## status: 0
318## stdout: hi
319## N-I dash status: 127
320## N-I dash stdout-json: ""
321
322#### builtin typeset / export / readonly
323case $SH in dash) exit ;; esac
324
325builtin typeset s=typeset
326echo s=$s
327
328builtin export s=export
329echo s=$s
330
331builtin readonly s=readonly
332echo s=$s
333
334echo --
335
336builtin builtin typeset s2=typeset
337echo s2=$s2
338
339builtin builtin export s2=export
340echo s2=$s2
341
342builtin builtin readonly s2=readonly
343echo s2=$s2
344
345## STDOUT:
346s=typeset
347s=export
348s=readonly
349--
350s2=typeset
351s2=export
352s2=readonly
353## END
354## N-I dash STDOUT:
355## END
356
357#### builtin declare / local
358case $SH in dash|mksh) exit ;; esac
359
360builtin declare s=declare
361echo s=$s
362
363f() {
364 builtin local s=local
365 echo s=$s
366}
367
368f
369
370## STDOUT:
371s=declare
372s=local
373## END
374## N-I dash/mksh STDOUT:
375## END
376
377#### builtin declare etc. with array is not parsed
378
379$SH -c 'builtin declare a=(x y)'
380test $? -ne 0 && echo 'fail'
381
382$SH -c 'builtin declare -a a=(x y)'
383test $? -ne 0 && echo 'fail'
384
385## STDOUT:
386fail
387fail
388## END
389
390#### command export / readonly
391case $SH in zsh) exit ;; esac
392
393# dash doesn't have declare typeset
394
395command export c=export
396echo c=$c
397
398command readonly c=readonly
399echo c=$c
400
401echo --
402
403command command export cc=export
404echo cc=$cc
405
406command command readonly cc=readonly
407echo cc=$cc
408
409## STDOUT:
410c=export
411c=readonly
412--
413cc=export
414cc=readonly
415## END
416## N-I zsh STDOUT:
417## END
418
419#### command local
420
421f() {
422 command local s=local
423 echo s=$s
424}
425
426f
427
428## STDOUT:
429s=local
430## END
431## BUG dash/mksh/zsh STDOUT:
432s=
433## END
434
435
436#### static builtin command ASSIGN, command builtin ASSIGN
437case $SH in dash|zsh) exit ;; esac
438
439# dash doesn't have declare typeset
440
441builtin command export bc=export
442echo bc=$bc
443
444builtin command readonly bc=readonly
445echo bc=$bc
446
447echo --
448
449command builtin export cb=export
450echo cb=$cb
451
452command builtin readonly cb=readonly
453echo cb=$cb
454
455## STDOUT:
456bc=export
457bc=readonly
458--
459cb=export
460cb=readonly
461## END
462## N-I dash/zsh STDOUT:
463## END
464
465#### dynamic builtin command ASSIGN, command builtin ASSIGN
466case $SH in dash|zsh) exit ;; esac
467
468b=builtin
469c=command
470e=export
471r=readonly
472
473$b $c export bc=export
474echo bc=$bc
475
476$b $c readonly bc=readonly
477echo bc=$bc
478
479echo --
480
481$c $b export cb=export
482echo cb=$cb
483
484$c $b readonly cb=readonly
485echo cb=$cb
486
487echo --
488
489$b $c $e bce=export
490echo bce=$bce
491
492$b $c $r bcr=readonly
493echo bcr=$bcr
494
495echo --
496
497$c $b $e cbe=export
498echo cbe=$cbe
499
500$c $b $r cbr=readonly
501echo cbr=$cbr
502
503## STDOUT:
504bc=export
505bc=readonly
506--
507cb=export
508cb=readonly
509--
510bce=export
511bcr=readonly
512--
513cbe=export
514cbr=readonly
515## END
516## N-I dash/zsh STDOUT:
517## END
518