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 -W 'one two three'
155 compgen -W 'one two three'
156 echo --
157 compgen -W 'w1 w2 three' -A directory w
158 echo --
159 compgen -A directory -W 'w1 w2 three' w # order doesn't matter
160 ## STDOUT:
161 one
162 two
163 three
164 --
165 web
166 w1
167 w2
168 --
169 web
170 w1
171 w2
172 ## END
173
174 #### complete with nonexistent function
175 complete -F invalidZZ -D
176 echo status=$?
177 ## stdout: status=2
178 ## BUG bash stdout: status=0
179
180 #### complete with no action
181 complete foo
182 echo status=$?
183 ## stdout: status=2
184 ## BUG bash stdout: status=0