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 cd $REPO_ROOT
154 compgen -A directory p | sort
155 ## STDOUT:
156 pgen2
157 pylib
158 py-yajl
159 ## END
160
161 #### compgen -A file
162 cd $REPO_ROOT
163 compgen -A file b | sort
164 ## STDOUT:
165 benchmarks
166 bin
167 build
168 ## END
169
170 #### compgen -A user
171 # no assertion because this isn't hermetic
172 compgen -A user
173 ## status: 0
174
175 #### compgen -A command completes external commands
176 # NOTE: this test isn't hermetic
177 compgen -A command xarg | uniq
178 echo status=$?
179 ## STDOUT:
180 xargs
181 status=0
182 ## END
183
184 #### compgen -A command completes functions and aliases
185 my_func() { echo ; }
186 my_func2() { echo ; }
187 alias my_alias=foo
188 compgen -A command my_
189 echo status=$?
190 ## STDOUT:
191 my_alias
192 my_func
193 my_func2
194 status=0
195 ## END
196
197 #### compgen -A command completes builtins and keywords
198 compgen -A command eva
199 echo status=$?
200 compgen -A command whil
201 echo status=$?
202 ## STDOUT:
203 eval
204 status=0
205 while
206 status=0
207 ## END
208
209 #### complete with nonexistent function
210 complete -F invalidZZ -D
211 echo status=$?
212 ## stdout: status=2
213 ## BUG bash stdout: status=0
214
215 #### complete with no action
216 complete foo
217 echo status=$?
218 ## stdout: status=2
219 ## BUG bash stdout: status=0
220
221 #### -o filenames and -o nospace have no effect with compgen
222 # they are POSTPROCESSING.
223 compgen -o filenames -o nospace -W 'bin build'
224 ## STDOUT:
225 bin
226 build
227 ## END
228
229 #### -o plusdirs and -o dirnames with compgen
230 cd $REPO_ROOT
231 compgen -o plusdirs -W 'a b1 b2' b | sort
232 echo ---
233 compgen -o dirnames b | sort
234 ## STDOUT:
235 b1
236 b2
237 benchmarks
238 bin
239 build
240 ---
241 benchmarks
242 bin
243 build
244 ## END
245
246 #### compgen -o default completes files and dirs
247 cd $REPO_ROOT
248 compgen -o default spec/t | sort
249 ## STDOUT:
250 spec/testdata
251 spec/tilde.test.sh
252 spec/type-compat.test.sh
253 ## END
254
255 #### compgen doesn't respect -X for user-defined functions
256 # WORKAROUND: wrap in bash -i -c because non-interactive bash behaves
257 # differently!
258 case $SH in
259 *bash|*osh)
260 $SH --rcfile /dev/null -i -c '
261 shopt -s extglob
262 fun() {
263 COMPREPLY=(one two three bin)
264 }
265 compgen -X "@(two|bin)" -F fun
266 echo --
267 compgen -X "!@(two|bin)" -F fun
268 '
269 esac
270 ## STDOUT:
271 one
272 three
273 --
274 two
275 bin
276 ## END
277
278 #### compgen -W words -X filter
279 # WORKAROUND: wrap in bash -i -c because non-interactive bash behaves
280 # differently!
281 case $SH in
282 *bash|*osh)
283 $SH --rcfile /dev/null -i -c 'shopt -s extglob; compgen -X "@(two|bin)" -W "one two three bin"'
284 esac
285 ## STDOUT:
286 one
287 three
288 ## END
289
290 #### compgen -f -X filter -- $cur
291 cd $TMP
292 touch spam.py spam.sh
293 compgen -f -- sp | sort
294 echo --
295 # WORKAROUND: wrap in bash -i -c because non-interactive bash behaves
296 # differently!
297 case $SH in
298 *bash|*osh)
299 $SH --rcfile /dev/null -i -c 'shopt -s extglob; compgen -f -X "!*.@(py)" -- sp'
300 esac
301 ## STDOUT:
302 spam.py
303 spam.sh
304 --
305 spam.py
306 ## END
307
308 #### compgen doesn't need shell quoting
309 # There is an obsolete comment in bash_completion that claims the opposite.
310 cd $TMP
311 touch 'foo bar'
312 touch "foo'bar"
313 compgen -f "foo b"
314 compgen -f "foo'"
315 ## STDOUT:
316 foo bar
317 foo'bar
318 ## END
319
320 #### compgen -W 'one two three'
321 cd $REPO_ROOT
322 compgen -W 'one two three'
323 echo --
324 compgen -W 'w1 w2 three' -A directory w
325 echo --
326 compgen -A directory -W 'w1 w2 three' w # order doesn't matter
327 ## STDOUT:
328 one
329 two
330 three
331 --
332 web
333 w1
334 w2
335 --
336 web
337 w1
338 w2
339 ## END
340
341 #### compgen -W evaluates code in $()
342 IFS=':%'
343 compgen -W '$(echo "spam:eggs%ham cheese")'
344 ## STDOUT:
345 spam
346 eggs
347 ham cheese
348 ## END
349
350 #### compgen -W uses IFS, and delimiters are escaped with \
351 IFS=':%'
352 compgen -W 'spam:eggs%ham cheese\:colon'
353 ## STDOUT:
354 spam
355 eggs
356 ham cheese:colon
357 ## END
358
359 #### Parse errors for compgen -W and complete -W
360 # bash doesn't detect as many errors because it lacks static parsing.
361 compgen -W '${'
362 echo status=$?
363 complete -W '${' foo
364 echo status=$?
365 ## STDOUT:
366 status=2
367 status=2
368 ## END
369 ## BUG bash STDOUT:
370 status=1
371 status=0
372 ## END
373
374 #### Runtime errors for compgen -W
375 compgen -W 'foo $(( 1 / 0 )) bar'
376 echo status=$?
377 ## STDOUT:
378 status=1
379 ## END
380
381 #### Runtime errors for compgen -F func
382 _foo() {
383 COMPREPLY=( foo bar )
384 COMPREPLY+=( $(( 1 / 0 )) ) # FATAL, but we still have candidates
385 }
386 compgen -F _foo foo
387 echo status=$?
388 ## STDOUT:
389 status=1
390 ## END
391
392 #### compgen -W '' cmd is not a usage error
393 # Bug fix due to '' being falsey in Python
394 compgen -W '' -- foo
395 echo status=$?
396 ## stdout: status=1