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

526 lines, 303 significant
1## oils_failures_allowed: 0
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 a=(x y) etc.
378
379$SH -c 'builtin declare a=(x y)'
380if test $? -ne 0; then
381 echo 'fail'
382fi
383
384$SH -c 'builtin declare -a a=(x y)'
385if test $? -ne 0; then
386 echo 'fail'
387fi
388
389## STDOUT:
390fail
391fail
392## END
393
394## OK osh STDOUT:
395## END
396
397
398#### command export / readonly
399case $SH in zsh) exit ;; esac
400
401# dash doesn't have declare typeset
402
403command export c=export
404echo c=$c
405
406command readonly c=readonly
407echo c=$c
408
409echo --
410
411command command export cc=export
412echo cc=$cc
413
414command command readonly cc=readonly
415echo cc=$cc
416
417## STDOUT:
418c=export
419c=readonly
420--
421cc=export
422cc=readonly
423## END
424## N-I zsh STDOUT:
425## END
426
427#### command local
428
429f() {
430 command local s=local
431 echo s=$s
432}
433
434f
435
436## STDOUT:
437s=local
438## END
439## BUG dash/mksh/zsh STDOUT:
440s=
441## END
442
443
444#### static builtin command ASSIGN, command builtin ASSIGN
445case $SH in dash|zsh) exit ;; esac
446
447# dash doesn't have declare typeset
448
449builtin command export bc=export
450echo bc=$bc
451
452builtin command readonly bc=readonly
453echo bc=$bc
454
455echo --
456
457command builtin export cb=export
458echo cb=$cb
459
460command builtin readonly cb=readonly
461echo cb=$cb
462
463## STDOUT:
464bc=export
465bc=readonly
466--
467cb=export
468cb=readonly
469## END
470## N-I dash/zsh STDOUT:
471## END
472
473#### dynamic builtin command ASSIGN, command builtin ASSIGN
474case $SH in dash|zsh) exit ;; esac
475
476b=builtin
477c=command
478e=export
479r=readonly
480
481$b $c export bc=export
482echo bc=$bc
483
484$b $c readonly bc=readonly
485echo bc=$bc
486
487echo --
488
489$c $b export cb=export
490echo cb=$cb
491
492$c $b readonly cb=readonly
493echo cb=$cb
494
495echo --
496
497$b $c $e bce=export
498echo bce=$bce
499
500$b $c $r bcr=readonly
501echo bcr=$bcr
502
503echo --
504
505$c $b $e cbe=export
506echo cbe=$cbe
507
508$c $b $r cbr=readonly
509echo cbr=$cbr
510
511## STDOUT:
512bc=export
513bc=readonly
514--
515cb=export
516cb=readonly
517--
518bce=export
519bcr=readonly
520--
521cbe=export
522cbr=readonly
523## END
524## N-I dash/zsh STDOUT:
525## END
526