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

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