1 #!/usr/bin/env bash
2
3 #### -A function prints functions
4 add () { expr 4 + 4; }
5 div () { expr 6 / 2; }
6 ek () { echo hello; }
7 __ec () { echo hi; }
8 _ab () { expr 10 % 3; }
9 compgen -A function
10 echo --
11 compgen -A function _
12 ## status: 0
13 ## STDOUT:
14 __ec
15 _ab
16 add
17 div
18 ek
19 --
20 __ec
21 _ab
22 ## END
23
24 #### Invalid syntax
25 compgen -A foo
26 echo status=$?
27 ## stdout: status=2
28
29 #### how compgen calls completion functions
30 foo_complete() {
31 # first, cur, prev
32 argv.py argv "$@"
33 argv.py COMP_WORDS "${COMP_WORDS[@]}"
34 argv.py COMP_CWORD "${COMP_CWORD}"
35 argv.py COMP_LINE "${COMP_LINE}"
36 argv.py COMP_POINT "${COMP_POINT}"
37 #return 124
38 COMPREPLY=(one two three)
39 }
40 compgen -F foo_complete foo a b c
41 ## STDOUT:
42 ['argv', 'compgen', 'foo', '']
43 ['COMP_WORDS']
44 ['COMP_CWORD', '-1']
45 ['COMP_LINE', '']
46 ['COMP_POINT', '0']
47 one
48 two
49 three
50 ## END
51
52 #### complete -o -F (git)
53 foo() { echo foo; }
54 wrapper=foo
55 complete -o default -o nospace -F $wrapper git
56 ## status: 0
57
58 #### compopt with invalid syntax
59 compopt -o invalid
60 echo status=$?
61 ## stdout: status=2
62
63 #### compopt fails when not in completion function
64 # NOTE: Have to be executing a completion function
65 compopt -o filenames +o nospace
66 ## status: 1
67
68 #### compgen -f on invalid dir
69 compgen -f /non-existing-dir/
70 ## status: 1
71 ## stdout-json: ""
72
73 #### compgen -f
74 mkdir -p $TMP/compgen
75 touch $TMP/compgen/{one,two,three}
76 cd $TMP/compgen
77 compgen -f | sort
78 echo --
79 compgen -f t | sort
80 ## STDOUT:
81 one
82 three
83 two
84 --
85 three
86 two
87 ## END
88
89 #### compgen -v with local vars
90 v1_global=0
91 f() {
92 local v2_local=0
93 compgen -v v
94 }
95 f
96 ## STDOUT:
97 v1_global
98 v2_local
99 ## END
100
101 #### compgen -v on unknown var
102 compgen -v __nonexistent__
103 ## status: 1
104 ## stdout-json: ""
105
106 #### compgen -v P
107 cd > /dev/null # for some reason in bash, this makes PIPESTATUS appear!
108 compgen -v P | grep -E '^PATH|PWD' | sort
109 ## STDOUT:
110 PATH
111 PWD
112 ## END
113
114 #### compgen with actions: function / variable / file
115 mkdir -p $TMP/compgen2
116 touch $TMP/compgen2/{PA,Q}_FILE
117 cd $TMP/compgen2 # depends on previous test above!
118 PA_FUNC() { echo P; }
119 Q_FUNC() { echo Q; }
120 compgen -A function -A variable -A file PA
121 ## STDOUT:
122 PA_FUNC
123 PATH
124 PA_FILE
125 ## END
126
127 #### compgen with actions: alias, setopt
128 alias v_alias='ls'
129 alias v_alias2='ls'
130 alias a1='ls'
131 compgen -A alias -A setopt v
132 ## STDOUT:
133 v_alias
134 v_alias2
135 verbose
136 vi
137 ## END
138
139 #### compgen with actions: shopt
140 compgen -A shopt -P [ -S ] nu
141 ## STDOUT:
142 [nullglob]
143 ## END
144
145 #### compgen with action and suffix: helptopic
146 compgen -A helptopic -S ___ fa
147 ## STDOUT:
148 false___
149 ## END
150
151 #### compgen -A directory
152 # omit portable-files.mk
153 compgen -A directory p | sort
154 ## STDOUT:
155 pgen2
156 pylib
157 py-yajl
158 ## END
159
160 #### compgen -A file
161 compgen -A file b | sort
162 ## STDOUT:
163 benchmarks
164 bin
165 build
166 ## END
167
168 #### compgen -A user
169 # no assertion because this isn't hermetic
170 compgen -A user
171 ## status: 0
172
173 #### compgen -A command completes external commands
174 # NOTE: this test isn't hermetic
175 compgen -A command xarg | uniq
176 echo status=$?
177 ## STDOUT:
178 xargs
179 status=0
180 ## END
181
182 #### compgen -A command completes functions and aliases
183 my_func() { echo ; }
184 my_func2() { echo ; }
185 alias my_alias=foo
186 compgen -A command my_
187 echo status=$?
188 ## STDOUT:
189 my_alias
190 my_func
191 my_func2
192 status=0
193 ## END
194
195 #### compgen -A command completes builtins and keywords
196 compgen -A command eva
197 echo status=$?
198 compgen -A command whil
199 echo status=$?
200 ## STDOUT:
201 eval
202 status=0
203 while
204 status=0
205 ## END
206
207 #### complete with nonexistent function
208 complete -F invalidZZ -D
209 echo status=$?
210 ## stdout: status=2
211 ## BUG bash stdout: status=0
212
213 #### complete with no action
214 complete foo
215 echo status=$?
216 ## stdout: status=2
217 ## BUG bash stdout: status=0
218
219 #### -o filenames and -o nospace have no effect with compgen
220 # they are POSTPROCESSING.
221 compgen -o filenames -o nospace -W 'bin build'
222 ## STDOUT:
223 bin
224 build
225 ## END
226
227 #### -o plusdirs and -o dirnames with compgen
228 compgen -o plusdirs -W 'a b1 b2' b | sort
229 echo ---
230 compgen -o dirnames b | sort
231 ## STDOUT:
232 b1
233 b2
234 benchmarks
235 bin
236 build
237 ---
238 benchmarks
239 bin
240 build
241 ## END
242
243 #### compgen -o default completes files and dirs
244 compgen -o default spec/t | sort
245 ## STDOUT:
246 spec/testdata
247 spec/tilde.test.sh
248 spec/type-compat.test.sh
249 ## END
250
251 #### compgen doesn't respect -X for user-defined functions
252 # WORKAROUND: wrap in bash -i -c because non-interactive bash behaves
253 # differently!
254 case $SH in
255 *bash|*osh)
256 $SH --rcfile /dev/null -i -c '
257 shopt -s extglob
258 fun() {
259 COMPREPLY=(one two three bin)
260 }
261 compgen -X "@(two|bin)" -F fun
262 echo --
263 compgen -X "!@(two|bin)" -F fun
264 '
265 esac
266 ## STDOUT:
267 one
268 three
269 --
270 two
271 bin
272 ## END
273
274 #### compgen -W words -X filter
275 # WORKAROUND: wrap in bash -i -c because non-interactive bash behaves
276 # differently!
277 case $SH in
278 *bash|*osh)
279 $SH --rcfile /dev/null -i -c 'shopt -s extglob; compgen -X "@(two|bin)" -W "one two three bin"'
280 esac
281 ## STDOUT:
282 one
283 three
284 ## END
285
286 #### compgen -f -X filter -- $cur
287 cd $TMP
288 touch spam.py spam.sh
289 compgen -f -- sp | sort
290 echo --
291 # WORKAROUND: wrap in bash -i -c because non-interactive bash behaves
292 # differently!
293 case $SH in
294 *bash|*osh)
295 $SH --rcfile /dev/null -i -c 'shopt -s extglob; compgen -f -X "!*.@(py)" -- sp'
296 esac
297 ## STDOUT:
298 spam.py
299 spam.sh
300 --
301 spam.py
302 ## END
303
304 #### compgen doesn't need shell quoting
305 # There is an obsolete comment in bash_completion that claims the opposite.
306 cd $TMP
307 touch 'foo bar'
308 touch "foo'bar"
309 compgen -f "foo b"
310 compgen -f "foo'"
311 ## STDOUT:
312 foo bar
313 foo'bar
314 ## END
315
316 #### compgen -W 'one two three'
317 compgen -W 'one two three'
318 echo --
319 compgen -W 'w1 w2 three' -A directory w
320 echo --
321 compgen -A directory -W 'w1 w2 three' w # order doesn't matter
322 ## STDOUT:
323 one
324 two
325 three
326 --
327 web
328 w1
329 w2
330 --
331 web
332 w1
333 w2
334 ## END
335
336 #### compgen -W evaluates code in $()
337 IFS=':%'
338 compgen -W '$(echo "spam:eggs%ham cheese")'
339 ## STDOUT:
340 spam
341 eggs
342 ham cheese
343 ## END
344
345 #### compgen -W uses IFS, and delimiters are escaped with \
346 IFS=':%'
347 compgen -W 'spam:eggs%ham cheese\:colon'
348 ## STDOUT:
349 spam
350 eggs
351 ham cheese:colon
352 ## END
353
354 #### Parse errors for compgen -W and complete -W
355 # bash doesn't detect as many errors because it lacks static parsing.
356 compgen -W '${'
357 echo status=$?
358 complete -W '${' foo
359 echo status=$?
360 ## STDOUT:
361 status=2
362 status=2
363 ## END
364 ## BUG bash STDOUT:
365 status=1
366 status=0
367 ## END
368
369 #### Runtime errors for compgen -W
370 compgen -W 'foo $(( 1 / 0 )) bar'
371 echo status=$?
372 ## STDOUT:
373 status=1
374 ## END
375
376 #### Runtime errors for compgen -F func
377 _foo() {
378 COMPREPLY=( foo bar )
379 COMPREPLY+=( $(( 1 / 0 )) ) # FATAL, but we still have candidates
380 }
381 compgen -F _foo foo
382 echo status=$?
383 ## STDOUT:
384 status=1
385 ## END
386
387 #### compgen -W '' cmd is not a usage error
388 # Bug fix due to '' being falsey in Python
389 compgen -W '' -- foo
390 echo status=$?
391 ## stdout: status=1