-
Notifications
You must be signed in to change notification settings - Fork 4
/
comp-tests.bash
executable file
·298 lines (251 loc) · 13.4 KB
/
comp-tests.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env bash
echo "===================================================="
echo Running completions tests on $(uname) with bash $BASH_VERSION
echo "===================================================="
# Test logging using $BASH_COMP_DEBUG_FILE
verifyDebug() {
debugfile=/tmp/comptests.bash.debug
rm -f $debugfile
export BASH_COMP_DEBUG_FILE=$debugfile
_completionTests_verifyCompletion "testprog help comp" "completion" nofile
if ! test -s $debugfile; then
# File should not be empty
echo -e "${RED}ERROR: No debug logs were printed to ${debugfile}${NC}"
_completionTests_TEST_FAILED=1
else
echo -e "${GREEN}SUCCESS: Debug logs were printed to ${debugfile}${NC}"
fi
unset BASH_COMP_DEBUG_FILE
}
# Test completion with a redirection
# https://github.com/spf13/cobra/issues/1334
verifyRedirect() {
rm -f notexist
_completionTests_verifyCompletion "testprog completion bash > notexist" ""
if test -f notexist; then
# File should not exist
echo -e "${RED}ERROR: completion mistakenly created the file 'notexist'${NC}"
_completionTests_TEST_FAILED=1
rm -f notexist
else
echo -e "${GREEN}SUCCESS: No extra file created, as expected${NC}"
fi
}
ROOTDIR=$(pwd)
export PATH=$ROOTDIR/testprog/bin:$PATH
# Are we testing Cobra's bash completion v1 or v2?
BASHCOMP_VERSION=bash
if [ -n "$BASHCOMPV2" ]; then
BASHCOMP_VERSION=bash2
fi
# Source the testing logic
source tests/bash/comp-test-lib.bash
# Setup completion of testprog, disabling descriptions (which is important for v2)
# Don't use the new source <() form as it does not work with bash v3.
# Normally, compopt is a builtin, and the script checks that it is a
# builtin to disable it if we are in bash3 (where compopt does not exist).
# We replace 'builtin' with 'function' because we cannot use the native
# compopt since we are explicitely calling the completion code instead
# of from within a real completion environment.
source /dev/stdin <<- EOF
$(testprog completion --no-descriptions $BASHCOMP_VERSION | sed s/builtin/function/g)
EOF
cd testingdir
# Basic first level commands (static completion)
if [ "$BASHCOMP_VERSION" = bash2 ]; then
_completionTests_verifyCompletion "testprog comp" "completion" nofile
_completionTests_verifyCompletion "testprog completion " "bash bash2 fish powershell zsh" nofile
else
_completionTests_verifyCompletion "testprog comp" "completion"
_completionTests_verifyCompletion "testprog completion " "bash bash2 fish powershell zsh"
fi
_completionTests_verifyCompletion "testprog help comp" "completion" nofile
_completionTests_verifyCompletion "testprog completion bash " "" nofile
#################################################
# Completions are filtered by prefix by program
#################################################
# Test ShellCompDirectiveDefault => File completion when no other completions
_completionTests_verifyCompletion "testprog prefix default " "bear bearpaw dog unicorn"
_completionTests_verifyCompletion "testprog prefix default u" "unicorn"
_completionTests_verifyCompletion "testprog prefix default f" ""
_completionTests_verifyCompletion "testprog prefix default z" ""
# Test ShellCompDirectiveNoFileComp => No file completion even when there are no other completions
_completionTests_verifyCompletion "testprog prefix nofile " "bear bearpaw dog unicorn" nofile
_completionTests_verifyCompletion "testprog prefix nofile u" "unicorn" nofile
_completionTests_verifyCompletion "testprog prefix nofile f" "" nofile
_completionTests_verifyCompletion "testprog prefix nofile z" "" nofile
# Test ShellCompDirectiveNoSpace => No space even when there is a single completion
_completionTests_verifyCompletion "testprog prefix nospace " "bear bearpaw dog unicorn" nospace
_completionTests_verifyCompletion "testprog prefix nospace b" "bear bearpaw" nospace
_completionTests_verifyCompletion "testprog prefix nospace u" "unicorn" nospace
_completionTests_verifyCompletion "testprog prefix nospace f" "" nospace
_completionTests_verifyCompletion "testprog prefix nospace z" "" nospace
_completionTests_verifyCompletion "testprog prefix nofilenospace " "bear bearpaw dog unicorn" nofile nospace
_completionTests_verifyCompletion "testprog prefix nofilenospace b" "bear bearpaw" nofile nospace
_completionTests_verifyCompletion "testprog prefix nofilenospace u" "unicorn" nofile nospace
_completionTests_verifyCompletion "testprog prefix nofilenospace f" "" nofile nospace
_completionTests_verifyCompletion "testprog prefix nofilenospace z" "" nofile nospace
#################################################
# Completions are NOT filtered by prefix by the program
#################################################
# Test ShellCompDirectiveDefault => File completion when no other completions
_completionTests_verifyCompletion "testprog noprefix default u" "unicorn"
_completionTests_verifyCompletion "testprog noprefix default f" ""
_completionTests_verifyCompletion "testprog noprefix default z" ""
# Test ShellCompDirectiveNoFileComp => No file completion even when there are no other completions
_completionTests_verifyCompletion "testprog noprefix nofile u" "unicorn" nofile
_completionTests_verifyCompletion "testprog noprefix nofile f" "" nofile
_completionTests_verifyCompletion "testprog noprefix nofile z" "" nofile
# Test ShellCompDirectiveNoSpace => No space even when there is a single completion
_completionTests_verifyCompletion "testprog noprefix nospace b" "bear bearpaw" nospace
_completionTests_verifyCompletion "testprog noprefix nospace u" "unicorn" nospace
_completionTests_verifyCompletion "testprog noprefix nospace f" "" nospace
_completionTests_verifyCompletion "testprog noprefix nospace z" "" nospace
_completionTests_verifyCompletion "testprog noprefix nofilenospace b" "bear bearpaw" nofile nospace
_completionTests_verifyCompletion "testprog noprefix nofilenospace u" "unicorn" nofile nospace
_completionTests_verifyCompletion "testprog noprefix nofilenospace f" "" nofile nospace
_completionTests_verifyCompletion "testprog noprefix nofilenospace z" "" nofile nospace
#################################################
# Other directives
#################################################
# Test ShellCompDirectiveFilterFileExt
_completionTests_verifyCompletion "testprog fileext setup" "setup.json setup.yaml"
# Test ShellCompDirectiveFilterDirs
_completionTests_verifyCompletion "testprog dir di" "dir dir2"
_completionTests_verifyCompletion "testprog subdir " "jsondir txtdir yamldir"
_completionTests_verifyCompletion "testprog subdir j" "jsondir"
_completionTests_verifyCompletion "testprog --theme " "jsondir txtdir yamldir"
_completionTests_verifyCompletion "testprog --theme t" "txtdir"
_completionTests_verifyCompletion "testprog --theme=" "jsondir txtdir yamldir"
_completionTests_verifyCompletion "testprog --theme=t" "txtdir"
# Test ShellCompDirectiveError => File completion only
_completionTests_verifyCompletion "testprog error u" ""
#################################################
# Flags
#################################################
if [ "$BASHCOMP_VERSION" = bash2 ]; then
_completionTests_verifyCompletion "testprog --custom" "--customComp" nofile
else
_completionTests_verifyCompletion "testprog --custom" "--customComp --customComp=" nospace
fi
_completionTests_verifyCompletion "testprog --customComp " "firstComp secondComp forthComp" nofile
_completionTests_verifyCompletion "testprog --customComp f" "firstComp forthComp" nofile
_completionTests_verifyCompletion "testprog --customComp=" "firstComp secondComp forthComp" nofile
_completionTests_verifyCompletion "testprog --customComp=f" "firstComp forthComp" nofile
#################################################
# Special cases
#################################################
# Test when there is a space before the binary name
# https://github.com/spf13/cobra/issues/1303
_completionTests_verifyCompletion " testprog prefix default u" "unicorn"
# Test using env variable and ~
# https://github.com/spf13/cobra/issues/1306
OLD_HOME=$HOME
HOME=/tmp
cp $ROOTDIR/testprog/bin/testprog $HOME/
# Must use single quotes to keep the environment variable
_completionTests_verifyCompletion '$HOME/testprog prefix default u' "unicorn"
_completionTests_verifyCompletion "~/testprog prefix default u" "unicorn"
HOME=$OLD_HOME
# An argument starting with dashes
_completionTests_verifyCompletion "testprog dasharg " "--arg"
# Needs bash completion v2
#_completionTests_verifyCompletion "testprog dasharg -- --" "--arg"
# Test debug printouts
verifyDebug
# Test completion with a redirection
# https://github.com/spf13/cobra/issues/1334
if [ $BASH_VERSINFO != 3 ]; then
# We know and accept that this fails with bash 3
# https://github.com/spf13/cobra/issues/1334
verifyRedirect
fi
# Measure speed of execution without descriptions (for both v1 and v2)
_completionTests_timing "testprog manycomps " 0.2 "no descriptions"
# Test other bash completion types with descriptions disabled.
# There should be no change in behaviour when there are no descriptions.
# The types are: menu-complete/menu-complete-backward (COMP_TYPE == 37)
# and insert-completions (COMP_TYPE == 42)
COMP_TYPE=37
_completionTests_verifyCompletion "testprog prefix nospace b" "bear bearpaw" nospace
_completionTests_verifyCompletion "testprog prefix nofile b" "bear bearpaw" nofile
# Measure speed of execution with menu-complete without descriptions (for both v1 and v2)
_completionTests_timing "testprog manycomps " 0.2 "menu-complete no descs"
COMP_TYPE=42
_completionTests_verifyCompletion "testprog prefix nospace b" "bear bearpaw" nospace
_completionTests_verifyCompletion "testprog prefix nofile b" "bear bearpaw" nofile
# Measure speed of execution with insert-completions without descriptions (for both v1 and v2)
_completionTests_timing "testprog manycomps " 0.2 "insert-completions no descs"
unset COMP_TYPE
# Test descriptions of bash v2
if [ "$BASHCOMP_VERSION" = bash2 ]; then
# Setup completion of testprog, enabling descriptions for v2.
# Don't use the new source <() form as it does not work with bash v3.
# Normally, compopt is a builtin, and the script checks that it is a
# builtin to disable it if we are in bash3 (where compopt does not exist).
# We replace 'builtin' with 'function' because we cannot use the native
# compopt since we are explicitely calling the completion code instead
# of from within a real completion environment.
source /dev/stdin <<- EOF
$(testprog completion --no-descriptions=false $BASHCOMP_VERSION | sed s/builtin/function/g)
EOF
# Disable sorting of output because it would mix up the descriptions
BASH_COMP_NO_SORT=1
# When running docker without the --tty/-t flag, the COLUMNS variable is not set
# bash completion v2 needs it to handle descriptions, so we set it here if it is unset
COLUMNS=${COLUMNS-100}
# Test descriptions with ShellCompDirectiveDefault
_completionTests_verifyCompletion "testprog prefix default " "bear (an animal)
bearpaw (a dessert)
dog
unicorn (mythical)"
_completionTests_verifyCompletion "testprog prefix default b" "bear (an animal)
bearpaw (a dessert)"
_completionTests_verifyCompletion "testprog prefix default bearp" "bearpaw"
# Test descriptions with ShellCompDirectiveNoFileComp
_completionTests_verifyCompletion "testprog prefix nofile " "bear (an animal)
bearpaw (a dessert)
dog
unicorn (mythical)" nofile
_completionTests_verifyCompletion "testprog prefix nofile b" "bear (an animal)
bearpaw (a dessert)" nofile
_completionTests_verifyCompletion "testprog prefix nofile bearp" "bearpaw" nofile
# Test descriptions with ShellCompDirectiveNoSpace
_completionTests_verifyCompletion "testprog prefix nospace " "bear (an animal)
bearpaw (a dessert)
dog
unicorn (mythical)" nospace
_completionTests_verifyCompletion "testprog prefix nospace b" "bear (an animal)
bearpaw (a dessert)" nospace
_completionTests_verifyCompletion "testprog prefix nospace bearp" "bearpaw" nospace
# Test descriptions with completion of flag values
_completionTests_verifyCompletion "testprog --customComp " "firstComp (the first value)
secondComp (the second value)
forthComp" nofile
_completionTests_verifyCompletion "testprog --customComp f" "firstComp (the first value)
forthComp" nofile
_completionTests_verifyCompletion "testprog --customComp fi" "firstComp" nofile
# Measure speed of execution with descriptions
_completionTests_timing "testprog manycomps " 0.5 "with descriptions"
# Test descriptions are properly removed when using other bash completion types
# The types are: menu-complete/menu-complete-backward (COMP_TYPE == 37)
# and insert-completions (COMP_TYPE == 42)
COMP_TYPE=37
_completionTests_verifyCompletion "testprog prefix nospace b" "bear
bearpaw" nospace
_completionTests_verifyCompletion "testprog prefix nofile b" "bear
bearpaw" nofile
# Measure speed of execution with menu-complete with descriptions
_completionTests_timing "testprog manycomps " 0.2 "menu-complete with descs"
COMP_TYPE=42
_completionTests_verifyCompletion "testprog prefix nospace b" "bear
bearpaw" nospace
_completionTests_verifyCompletion "testprog prefix nofile b" "bear
bearpaw" nofile
# Measure speed of execution with insert-completions with descriptions
_completionTests_timing "testprog manycomps " 0.2 "insert-completions no descs"
unset COMP_TYPE
fi
# This must be the last call. It allows to exit with an exit code
# that reflects the final status of all the tests.
_completionTests_exit