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

326 lines, 172 significant
1## oils_failures_allowed: 0
2## compare_shells: bash
3
4#### type -t -> function
5f() { echo hi; }
6type -t f
7## stdout: function
8
9#### type -t -> alias
10shopt -s expand_aliases
11alias foo=bar
12type -t foo
13## stdout: alias
14
15#### type -t -> builtin
16type -t echo read : [ declare local
17## STDOUT:
18builtin
19builtin
20builtin
21builtin
22builtin
23builtin
24## END
25
26#### type -t -> keyword
27type -t for time ! fi do {
28## STDOUT:
29keyword
30keyword
31keyword
32keyword
33keyword
34keyword
35## END
36
37#### type -t control flow
38
39# this differs from bash, but don't lie!
40type -t break continue return exit
41## STDOUT:
42keyword
43keyword
44keyword
45keyword
46## END
47## OK bash STDOUT:
48builtin
49builtin
50builtin
51builtin
52## END
53
54
55#### type -t -> file
56type -t find xargs
57## STDOUT:
58file
59file
60## END
61
62#### type -t doesn't find non-executable (like command -v)
63PATH="$TMP:$PATH"
64touch $TMP/non-executable
65type -t non-executable
66## STDOUT:
67## END
68## status: 1
69## BUG bash STDOUT:
70file
71## END
72## BUG bash status: 0
73
74#### type -t -> not found
75type -t echo ZZZ find ==
76echo status=$?
77## STDOUT:
78builtin
79file
80status=1
81## END
82## STDERR:
83## END
84
85#### type -p and -P builtin -> file
86touch /tmp/{mv,tar,grep}
87chmod +x /tmp/{mv,tar,grep}
88PATH=/tmp:$PATH
89
90type -p mv tar grep
91echo --
92type -P mv tar grep
93## STDOUT:
94/tmp/mv
95/tmp/tar
96/tmp/grep
97--
98/tmp/mv
99/tmp/tar
100/tmp/grep
101## END
102
103#### type -a -P gives multiple files
104
105touch _tmp/pwd
106chmod +x _tmp/pwd
107PATH="_tmp:/bin"
108
109type -a -P pwd
110
111## STDOUT:
112_tmp/pwd
113/bin/pwd
114## END
115
116#### type -p builtin -> not found
117type -p FOO BAR NOT_FOUND
118## status: 1
119## STDOUT:
120## END
121
122#### type -p builtin -> not a file
123type -p cd type builtin command
124## STDOUT:
125## END
126
127#### type -P builtin -> not found
128type -P FOO BAR NOT_FOUND
129## status: 1
130## STDOUT:
131## END
132
133#### type -P builtin -> not a file
134type -P cd type builtin command
135## status: 1
136## STDOUT:
137## END
138
139#### type -P builtin -> not a file but file found
140touch _tmp/{mv,tar,grep}
141chmod +x _tmp/{mv,tar,grep}
142PATH=_tmp:$PATH
143
144mv () { ls; }
145tar () { ls; }
146grep () { ls; }
147type -P mv tar grep cd builtin command type
148## status: 1
149## STDOUT:
150_tmp/mv
151_tmp/tar
152_tmp/grep
153## END
154
155#### type -f builtin -> not found
156type -f FOO BAR NOT FOUND
157## status: 1
158
159#### type -f builtin -> function and file exists
160touch /tmp/{mv,tar,grep}
161chmod +x /tmp/{mv,tar,grep}
162PATH=/tmp:$PATH
163
164mv () { ls; }
165tar () { ls; }
166grep () { ls; }
167type -f mv tar grep
168## STDOUT:
169mv is /tmp/mv
170tar is /tmp/tar
171grep is /tmp/grep
172## END
173
174#### type -a -> function; prints shell source code
175f () { :; }
176type -a f
177## STDOUT:
178f is a function
179f ()
180{
181 :
182}
183## END
184## OK osh STDOUT:
185f is a shell function
186## END
187
188#### type -ap -> function
189f () { :; }
190type -ap f
191## STDOUT:
192## END
193
194#### type -a -> alias; prints alias definition
195shopt -s expand_aliases
196alias ll="ls -lha"
197type -a ll
198## stdout: ll is an alias for "ls -lha"
199## OK bash stdout: ll is aliased to `ls -lha'
200
201#### type -ap -> alias
202shopt -s expand_aliases
203alias ll="ls -lha"
204type -ap ll
205## STDOUT:
206## END
207
208#### type -a -> builtin
209type -a cd
210## stdout: cd is a shell builtin
211
212#### type -ap -> builtin
213type -ap cd
214## STDOUT:
215## END
216
217#### type -a -> keyword
218type -a while
219## stdout: while is a shell keyword
220
221#### type -a -> file
222touch _tmp/date
223chmod +x _tmp/date
224PATH=/bin:_tmp # control output
225
226type -a date
227
228## STDOUT:
229date is /bin/date
230date is _tmp/date
231## END
232
233#### type -ap -> file; abbreviated
234touch _tmp/date
235chmod +x _tmp/date
236PATH=/bin:_tmp # control output
237
238type -ap date
239## STDOUT:
240/bin/date
241_tmp/date
242## END
243
244#### type -a -> builtin and file
245touch _tmp/pwd
246chmod +x _tmp/pwd
247PATH=/bin:_tmp # control output
248
249type -a pwd
250## STDOUT:
251pwd is a shell builtin
252pwd is /bin/pwd
253pwd is _tmp/pwd
254## END
255
256#### type -a -> builtin and file and shell function
257touch _tmp/pwd
258chmod +x _tmp/pwd
259PATH=/bin:_tmp # control output
260
261type -a pwd
262echo ---
263
264pwd() { echo function-too; }
265type -a pwd
266echo ---
267
268type -a -f pwd
269
270## STDOUT:
271pwd is a shell builtin
272pwd is /bin/pwd
273pwd is _tmp/pwd
274---
275pwd is a function
276pwd ()
277{
278 echo function-too
279}
280pwd is a shell builtin
281pwd is /bin/pwd
282pwd is _tmp/pwd
283---
284pwd is a shell builtin
285pwd is /bin/pwd
286pwd is _tmp/pwd
287## END
288## OK osh STDOUT:
289pwd is a shell builtin
290pwd is /bin/pwd
291pwd is _tmp/pwd
292---
293pwd is a shell function
294pwd is a shell builtin
295pwd is /bin/pwd
296pwd is _tmp/pwd
297---
298pwd is a shell builtin
299pwd is /bin/pwd
300pwd is _tmp/pwd
301## END
302
303#### type -ap -> builtin and file; doesn't print builtin or function
304touch _tmp/pwd
305chmod +x _tmp/pwd
306PATH=/bin:_tmp # control output
307
308# Function is also ignored
309pwd() { echo function-too; }
310
311type -ap pwd
312echo ---
313
314type -p pwd
315
316## STDOUT:
317/bin/pwd
318_tmp/pwd
319---
320## END
321
322#### type -a -> executable not in PATH
323touch _tmp/executable
324chmod +x _tmp/executable
325type -a executable
326## status: 1