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

322 lines, 190 significant
1## oils_failures_allowed: 0
2## compare_shells: dash bash mksh zsh
3
4
5
6#### command -v
7myfunc() { echo x; }
8command -v echo
9echo $?
10
11command -v myfunc
12echo $?
13
14command -v nonexistent # doesn't print anything
15echo nonexistent=$?
16
17command -v '' # BUG FIX, shouldn't succeed
18echo empty=$?
19
20command -v for
21echo $?
22
23## STDOUT:
24echo
250
26myfunc
270
28nonexistent=1
29empty=1
30for
310
32## OK dash STDOUT:
33echo
340
35myfunc
360
37nonexistent=127
38empty=127
39for
400
41## END
42
43#### command -v with multiple names
44# ALL FOUR SHELLS behave differently here!
45#
46# bash chooses to swallow the error! We agree with zsh if ANY word lookup
47# fails, then the whole thing fails.
48
49myfunc() { echo x; }
50command -v echo myfunc ZZZ for
51echo status=$?
52
53## STDOUT:
54echo
55myfunc
56for
57status=1
58## BUG bash STDOUT:
59echo
60myfunc
61for
62status=0
63## BUG dash STDOUT:
64echo
65status=0
66## OK mksh STDOUT:
67echo
68myfunc
69status=1
70## END
71
72#### command -v doesn't find non-executable file
73# PATH resolution is different
74
75PATH="_tmp:$PATH"
76touch _tmp/non-executable _tmp/executable
77chmod +x _tmp/executable
78
79command -v _tmp/non-executable
80echo status=$?
81
82command -v _tmp/executable
83echo status=$?
84
85## STDOUT:
86status=1
87_tmp/executable
88status=0
89## END
90
91## BUG dash STDOUT:
92_tmp/non-executable
93status=0
94_tmp/executable
95status=0
96## END
97
98#### command -V
99myfunc() { echo x; }
100
101shopt -s expand_aliases
102alias ll='ls -l'
103
104backtick=\`
105command -V ll | sed "s/$backtick/'/g"
106echo status=$?
107
108command -V echo
109echo status=$?
110
111command -V myfunc
112echo status=$?
113
114command -V nonexistent # doesn't print anything
115echo status=$?
116
117command -V for
118echo status=$?
119
120## STDOUT:
121ll is an alias for "ls -l"
122status=0
123echo is a shell builtin
124status=0
125myfunc is a shell function
126status=0
127status=1
128for is a shell keyword
129status=0
130## END
131
132## OK zsh STDOUT:
133ll is an alias for ls -l
134status=0
135echo is a shell builtin
136status=0
137myfunc is a shell function
138status=0
139nonexistent not found
140status=1
141for is a reserved word
142status=0
143## END
144
145## OK bash STDOUT:
146ll is aliased to 'ls -l'
147status=0
148echo is a shell builtin
149status=0
150myfunc is a function
151myfunc ()
152{
153 echo x
154}
155status=0
156status=1
157for is a shell keyword
158status=0
159## END
160
161## OK mksh STDOUT:
162ll is an alias for 'ls -l'
163status=0
164echo is a shell builtin
165status=0
166myfunc is a function
167status=0
168nonexistent not found
169status=1
170for is a reserved word
171status=0
172## END
173
174## OK dash STDOUT:
175ll is an alias for ls -l
176status=0
177echo is a shell builtin
178status=0
179myfunc is a shell function
180status=0
181nonexistent: not found
182status=127
183for is a shell keyword
184status=0
185## END
186
187#### command -V nonexistent
188command -V nonexistent 2>err.txt
189echo status=$?
190fgrep -o 'nonexistent: not found' err.txt || true
191
192## STDOUT:
193status=1
194nonexistent: not found
195## END
196
197## OK zsh/mksh STDOUT:
198nonexistent not found
199status=1
200## END
201
202## BUG dash STDOUT:
203nonexistent: not found
204status=127
205## END
206
207
208#### command skips function lookup
209seq() {
210 echo "$@"
211}
212command # no-op
213seq 3
214command seq 3
215# subshell shouldn't fork another process (but we don't have a good way of
216# testing it)
217( command seq 3 )
218## STDOUT:
2193
2201
2212
2223
2231
2242
2253
226## END
227
228#### command command seq 3
229command command seq 3
230## STDOUT:
2311
2322
2333
234## END
235## N-I zsh stdout-json: ""
236## N-I zsh status: 127
237
238#### command command -v seq
239seq() {
240 echo 3
241}
242command command -v seq
243## stdout: seq
244## N-I zsh stdout-json: ""
245## N-I zsh status: 127
246
247#### command -p (override existing program)
248# Tests whether command -p overrides the path
249# tr chosen because we need a simple non-builtin
250mkdir -p $TMP/bin
251echo "echo wrong" > $TMP/bin/tr
252chmod +x $TMP/bin/tr
253PATH="$TMP/bin:$PATH"
254echo aaa | tr "a" "b"
255echo aaa | command -p tr "a" "b"
256rm $TMP/bin/tr
257## STDOUT:
258wrong
259bbb
260## END
261
262#### command -p (hide tool in custom path)
263mkdir -p $TMP/bin
264echo "echo hello" > $TMP/bin/hello
265chmod +x $TMP/bin/hello
266export PATH=$TMP/bin
267command -p hello
268## status: 127
269
270#### command -p (find hidden tool in default path)
271export PATH=''
272command -p ls
273## status: 0
274
275
276#### $(command type ls)
277type() { echo FUNCTION; }
278type
279s=$(command type echo)
280echo $s | grep builtin > /dev/null
281echo status=$?
282## STDOUT:
283FUNCTION
284status=0
285## END
286## N-I zsh STDOUT:
287FUNCTION
288status=1
289## END
290## N-I mksh STDOUT:
291status=1
292## END
293
294#### builtin
295cd () { echo "hi"; }
296cd
297builtin cd / && pwd
298unset -f cd
299## STDOUT:
300hi
301/
302## END
303## N-I dash STDOUT:
304hi
305## END
306
307#### builtin ls not found
308builtin ls
309## status: 1
310## N-I dash status: 127
311
312#### builtin no args
313builtin
314## status: 0
315## N-I dash status: 127
316
317#### builtin command echo hi
318builtin command echo hi
319## status: 0
320## stdout: hi
321## N-I dash status: 127
322## N-I dash stdout-json: ""