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

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