OILS / spec / builtin-meta.test.sh View on Github | oils.pub

395 lines, 231 significant
1## compare_shells: dash bash mksh zsh ash
2
3#### command -v
4myfunc() { echo x; }
5command -v echo
6echo $?
7
8command -v myfunc
9echo $?
10
11command -v nonexistent # doesn't print anything
12echo nonexistent=$?
13
14command -v '' # BUG FIX, shouldn't succeed
15echo empty=$?
16
17command -v for
18echo $?
19
20## STDOUT:
21echo
220
23myfunc
240
25nonexistent=1
26empty=1
27for
280
29## OK dash/ash STDOUT:
30echo
310
32myfunc
330
34nonexistent=127
35empty=127
36for
370
38## END
39
40#### command -v executable, builtin
41
42#command -v grep ls
43
44command -v grep | egrep -o '/[^/]+$'
45command -v ls | egrep -o '/[^/]+$'
46echo
47
48command -v true
49command -v eval
50
51## STDOUT:
52/grep
53/ls
54
55true
56eval
57## END
58
59
60#### command -v with multiple names
61# ALL FOUR SHELLS behave differently here!
62#
63# bash chooses to swallow the error! We agree with zsh if ANY word lookup
64# fails, then the whole thing fails.
65
66myfunc() { echo x; }
67command -v echo myfunc ZZZ for
68echo status=$?
69
70## STDOUT:
71echo
72myfunc
73for
74status=1
75## BUG bash STDOUT:
76echo
77myfunc
78for
79status=0
80## BUG-2 dash/ash STDOUT:
81echo
82status=0
83## OK mksh STDOUT:
84echo
85myfunc
86status=1
87## END
88
89#### command -v doesn't find non-executable file
90# PATH resolution is different
91
92mkdir -p _tmp
93PATH="_tmp:$PATH"
94touch _tmp/non-executable _tmp/executable
95chmod +x _tmp/executable
96
97command -v _tmp/non-executable
98echo status=$?
99
100command -v _tmp/executable
101echo status=$?
102
103## STDOUT:
104status=1
105_tmp/executable
106status=0
107## END
108
109## BUG dash/ash STDOUT:
110_tmp/non-executable
111status=0
112_tmp/executable
113status=0
114## END
115
116#### command -v doesn't find executable dir
117
118mkdir -p _tmp
119PATH="_tmp:$PATH"
120mkdir _tmp/cat
121
122command -v _tmp/cat
123echo status=$?
124command -v cat
125echo status=$?
126
127## STDOUT:
128status=1
129/usr/bin/cat
130status=0
131## END
132
133## BUG mksh STDOUT:
134status=1
135cat
136status=0
137## END
138
139## BUG ash/dash STDOUT:
140_tmp/cat
141status=0
142/usr/bin/cat
143status=0
144## END
145
146#### command -V
147myfunc() { echo x; }
148
149shopt -s expand_aliases
150alias ll='ls -l'
151
152backtick=\`
153command -V ll | sed "s/$backtick/'/g"
154echo status=$?
155
156command -V echo
157echo status=$?
158
159# Paper over insignificant difference
160command -V myfunc | sed 's/shell function/function/'
161echo status=$?
162
163command -V nonexistent # doesn't print anything
164echo status=$?
165
166command -V for
167echo status=$?
168
169## STDOUT:
170ll is an alias for "ls -l"
171status=0
172echo is a shell builtin
173status=0
174myfunc is a function
175status=0
176status=1
177for is a shell keyword
178status=0
179## END
180
181## OK zsh STDOUT:
182ll is an alias for ls -l
183status=0
184echo is a shell builtin
185status=0
186myfunc is a function
187status=0
188nonexistent not found
189status=1
190for is a reserved word
191status=0
192## END
193
194## OK bash STDOUT:
195ll is aliased to 'ls -l'
196status=0
197echo is a shell builtin
198status=0
199myfunc is a function
200myfunc ()
201{
202 echo x
203}
204status=0
205status=1
206for is a shell keyword
207status=0
208## END
209
210## OK mksh STDOUT:
211ll is an alias for 'ls -l'
212status=0
213echo is a shell builtin
214status=0
215myfunc is a function
216status=0
217nonexistent not found
218status=1
219for is a reserved word
220status=0
221## END
222
223## OK dash/ash STDOUT:
224ll is an alias for ls -l
225status=0
226echo is a shell builtin
227status=0
228myfunc is a function
229status=0
230nonexistent: not found
231status=127
232for is a shell keyword
233status=0
234## END
235
236#### command -V nonexistent
237command -V nonexistent 2>err.txt
238echo status=$?
239fgrep -o 'nonexistent: not found' err.txt || true
240
241## STDOUT:
242status=1
243nonexistent: not found
244## END
245
246## OK zsh/mksh STDOUT:
247nonexistent not found
248status=1
249## END
250
251## BUG dash/ash STDOUT:
252nonexistent: not found
253status=127
254## END
255
256
257#### command skips function lookup
258seq() {
259 echo "$@"
260}
261command # no-op
262seq 3
263command seq 3
264# subshell shouldn't fork another process (but we don't have a good way of
265# testing it)
266( command seq 3 )
267## STDOUT:
2683
2691
2702
2713
2721
2732
2743
275## END
276
277#### command command seq 3
278command command seq 3
279## STDOUT:
2801
2812
2823
283## END
284## N-I zsh stdout-json: ""
285## N-I zsh status: 127
286
287#### command command -v seq
288seq() {
289 echo 3
290}
291command command -v seq
292## stdout: seq
293## N-I zsh stdout-json: ""
294## N-I zsh status: 127
295
296#### command -p (override existing program)
297# Tests whether command -p overrides the path
298# tr chosen because we need a simple non-builtin
299mkdir -p $TMP/bin
300echo "echo wrong" > $TMP/bin/tr
301chmod +x $TMP/bin/tr
302PATH="$TMP/bin:$PATH"
303echo aaa | tr "a" "b"
304echo aaa | command -p tr "a" "b"
305rm $TMP/bin/tr
306## STDOUT:
307wrong
308bbb
309## END
310
311#### command -p (hide tool in custom path)
312mkdir -p $TMP/bin
313echo "echo hello" > $TMP/bin/hello
314chmod +x $TMP/bin/hello
315export PATH=$TMP/bin
316command -p hello
317## status: 127
318
319#### command -p (find hidden tool in default path)
320export PATH=''
321command -p ls
322## status: 0
323
324
325#### $(command type ls)
326type() { echo FUNCTION; }
327type
328s=$(command type echo)
329echo $s | grep builtin > /dev/null
330echo status=$?
331## STDOUT:
332FUNCTION
333status=0
334## END
335## N-I zsh STDOUT:
336FUNCTION
337status=1
338## END
339## N-I mksh STDOUT:
340status=1
341## END
342
343#### builtin
344cd () { echo "hi"; }
345cd
346builtin cd / && pwd
347unset -f cd
348## STDOUT:
349hi
350/
351## END
352## N-I dash/ash STDOUT:
353hi
354## END
355
356#### builtin ls not found
357builtin ls
358## status: 1
359## N-I dash/ash status: 127
360
361#### builtin usage
362
363builtin
364echo status=$?
365
366builtin --
367echo status=$?
368
369builtin -- false
370echo status=$?
371
372## STDOUT:
373status=0
374status=0
375status=1
376## END
377
378## BUG zsh STDOUT:
379status=0
380status=1
381status=1
382## END
383
384## N-I dash/ash STDOUT:
385status=127
386status=127
387status=127
388## END
389
390#### builtin command echo hi
391builtin command echo hi
392## status: 0
393## stdout: hi
394## N-I dash/ash status: 127
395## N-I dash/ash stdout-json: ""