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