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 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 -o (git)
58 # NOTE: Have to be executing a completion function
59 compopt -o filenames +o nospace
60 ## status: 1
61
62 #### compgen -f on invalid dir
63 compgen -f /non-existing-dir/
64 ## status: 1
65 ## stdout-json: ""
66
67 #### compgen -f
68 mkdir -p $TMP/compgen
69 touch $TMP/compgen/{one,two,three}
70 cd $TMP/compgen
71 compgen -f | sort
72 echo --
73 compgen -f t | sort
74 ## STDOUT:
75 one
76 three
77 two
78 --
79 three
80 two
81 ## END
82
83 #### compgen -v with local vars
84 v1_global=0
85 f() {
86 local v2_local=0
87 compgen -v v
88 }
89 f
90 ## STDOUT:
91 v1_global
92 v2_local
93 ## END
94
95 #### compgen -v on unknown var
96 compgen -v __nonexistent__
97 ## status: 1
98 ## stdout-json: ""
99
100 #### compgen -v P
101 cd > /dev/null # for some reason in bash, this makes PIPESTATUS appear!
102 compgen -v P | grep -E '^PATH|PWD' | sort
103 ## STDOUT:
104 PATH
105 PWD
106 ## END
107
108 #### compgen with actions: function / variable / file
109 mkdir -p $TMP/compgen2
110 touch $TMP/compgen2/PA_FILE_{1,2}
111 cd $TMP/compgen2 # depends on previous test above!
112 PA_FUNC() { echo P; }
113 Q_FUNC() { echo Q; }
114 compgen -A function -A variable -A file PA
115 ## STDOUT:
116 PA_FUNC
117 PATH
118 PA_FILE_1
119 PA_FILE_2
120 ## END
121
122 #### compgen with actions: alias, setopt
123 alias v_alias='ls'
124 alias v_alias2='ls'
125 alias a1='ls'
126 compgen -A alias -A setopt v
127 ## STDOUT:
128 v_alias
129 v_alias2
130 verbose
131 vi
132 ## END
133
134 #### compgen with actions: shopt
135 compgen -A shopt -P [ -S ] nu
136 ## STDOUT:
137 [nullglob]
138 ## END
139
140 #### compgen with action and suffix: helptopic
141 compgen -A helptopic -S ___ fa
142 ## STDOUT:
143 false___
144 ## END
145
146 #### compgen -A directory
147 compgen -A directory b
148 ## STDOUT:
149 bin
150 benchmarks
151 build
152 ## END
153
154 #### compgen -A user
155 # no assertion because this isn't hermetic
156 compgen -A user
157 ## status: 0
158
159 #### compgen -W 'one two three'
160 compgen -W 'one two three'
161 echo --
162 compgen -W 'w1 w2 three' -A directory w
163 echo --
164 compgen -A directory -W 'w1 w2 three' w # order doesn't matter
165 ## STDOUT:
166 one
167 two
168 three
169 --
170 web
171 w1
172 w2
173 --
174 web
175 w1
176 w2
177 ## END
178
179 #### compgen -A command completes external commands
180 # NOTE: this test isn't hermetic
181 compgen -A command xarg
182 echo status=$?
183 ## STDOUT:
184 xargs
185 status=0
186 ## END
187
188 #### compgen -A command completes functions and aliases
189 myfunc() { echo ; }
190 myfunc2() { echo ; }
191 alias myalias=foo
192 compgen -A command my
193 echo status=$?
194 ## STDOUT:
195 myalias
196 myfunc
197 myfunc2
198 status=0
199 ## END
200
201 #### compgen -A command completes builtins and keywords
202 compgen -A command eva
203 echo status=$?
204 compgen -A command whil
205 echo status=$?
206 ## STDOUT:
207 eval
208 status=0
209 while
210 status=0
211 ## END
212
213 #### complete with nonexistent function
214 complete -F invalidZZ -D
215 echo status=$?
216 ## stdout: status=2
217 ## BUG bash stdout: status=0
218
219 #### complete with no action
220 complete foo
221 echo status=$?
222 ## stdout: status=2
223 ## BUG bash stdout: status=0