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

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