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

364 lines, 213 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 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
92PATH="_tmp:$PATH"
93touch _tmp/non-executable _tmp/executable
94chmod +x _tmp/executable
95
96command -v _tmp/non-executable
97echo status=$?
98
99command -v _tmp/executable
100echo status=$?
101
102## STDOUT:
103status=1
104_tmp/executable
105status=0
106## END
107
108## BUG dash/ash STDOUT:
109_tmp/non-executable
110status=0
111_tmp/executable
112status=0
113## END
114
115#### command -V
116myfunc() { echo x; }
117
118shopt -s expand_aliases
119alias ll='ls -l'
120
121backtick=\`
122command -V ll | sed "s/$backtick/'/g"
123echo status=$?
124
125command -V echo
126echo status=$?
127
128# Paper over insignificant difference
129command -V myfunc | sed 's/shell function/function/'
130echo status=$?
131
132command -V nonexistent # doesn't print anything
133echo status=$?
134
135command -V for
136echo status=$?
137
138## STDOUT:
139ll is an alias for "ls -l"
140status=0
141echo is a shell builtin
142status=0
143myfunc is a function
144status=0
145status=1
146for is a shell keyword
147status=0
148## END
149
150## OK zsh STDOUT:
151ll is an alias for ls -l
152status=0
153echo is a shell builtin
154status=0
155myfunc is a function
156status=0
157nonexistent not found
158status=1
159for is a reserved word
160status=0
161## END
162
163## OK bash STDOUT:
164ll is aliased to 'ls -l'
165status=0
166echo is a shell builtin
167status=0
168myfunc is a function
169myfunc ()
170{
171 echo x
172}
173status=0
174status=1
175for is a shell keyword
176status=0
177## END
178
179## OK mksh STDOUT:
180ll is an alias for 'ls -l'
181status=0
182echo is a shell builtin
183status=0
184myfunc is a function
185status=0
186nonexistent not found
187status=1
188for is a reserved word
189status=0
190## END
191
192## OK dash/ash STDOUT:
193ll is an alias for ls -l
194status=0
195echo is a shell builtin
196status=0
197myfunc is a function
198status=0
199nonexistent: not found
200status=127
201for is a shell keyword
202status=0
203## END
204
205#### command -V nonexistent
206command -V nonexistent 2>err.txt
207echo status=$?
208fgrep -o 'nonexistent: not found' err.txt || true
209
210## STDOUT:
211status=1
212nonexistent: not found
213## END
214
215## OK zsh/mksh STDOUT:
216nonexistent not found
217status=1
218## END
219
220## BUG dash/ash STDOUT:
221nonexistent: not found
222status=127
223## END
224
225
226#### command skips function lookup
227seq() {
228 echo "$@"
229}
230command # no-op
231seq 3
232command seq 3
233# subshell shouldn't fork another process (but we don't have a good way of
234# testing it)
235( command seq 3 )
236## STDOUT:
2373
2381
2392
2403
2411
2422
2433
244## END
245
246#### command command seq 3
247command command seq 3
248## STDOUT:
2491
2502
2513
252## END
253## N-I zsh stdout-json: ""
254## N-I zsh status: 127
255
256#### command command -v seq
257seq() {
258 echo 3
259}
260command command -v seq
261## stdout: seq
262## N-I zsh stdout-json: ""
263## N-I zsh status: 127
264
265#### command -p (override existing program)
266# Tests whether command -p overrides the path
267# tr chosen because we need a simple non-builtin
268mkdir -p $TMP/bin
269echo "echo wrong" > $TMP/bin/tr
270chmod +x $TMP/bin/tr
271PATH="$TMP/bin:$PATH"
272echo aaa | tr "a" "b"
273echo aaa | command -p tr "a" "b"
274rm $TMP/bin/tr
275## STDOUT:
276wrong
277bbb
278## END
279
280#### command -p (hide tool in custom path)
281mkdir -p $TMP/bin
282echo "echo hello" > $TMP/bin/hello
283chmod +x $TMP/bin/hello
284export PATH=$TMP/bin
285command -p hello
286## status: 127
287
288#### command -p (find hidden tool in default path)
289export PATH=''
290command -p ls
291## status: 0
292
293
294#### $(command type ls)
295type() { echo FUNCTION; }
296type
297s=$(command type echo)
298echo $s | grep builtin > /dev/null
299echo status=$?
300## STDOUT:
301FUNCTION
302status=0
303## END
304## N-I zsh STDOUT:
305FUNCTION
306status=1
307## END
308## N-I mksh STDOUT:
309status=1
310## END
311
312#### builtin
313cd () { echo "hi"; }
314cd
315builtin cd / && pwd
316unset -f cd
317## STDOUT:
318hi
319/
320## END
321## N-I dash/ash STDOUT:
322hi
323## END
324
325#### builtin ls not found
326builtin ls
327## status: 1
328## N-I dash/ash status: 127
329
330#### builtin usage
331
332builtin
333echo status=$?
334
335builtin --
336echo status=$?
337
338builtin -- false
339echo status=$?
340
341## STDOUT:
342status=0
343status=0
344status=1
345## END
346
347## BUG zsh STDOUT:
348status=0
349status=1
350status=1
351## END
352
353## N-I dash/ash STDOUT:
354status=127
355status=127
356status=127
357## END
358
359#### builtin command echo hi
360builtin command echo hi
361## status: 0
362## stdout: hi
363## N-I dash/ash status: 127
364## N-I dash/ash stdout-json: ""