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

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