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