-
Notifications
You must be signed in to change notification settings - Fork 4
/
comp-test-lib.bash
executable file
·229 lines (202 loc) · 6.54 KB
/
comp-test-lib.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
#!bash
# COLOR codes
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
_compTests_nofile=/tmp/comptests.bash.nofile
_compTests_nospace=/tmp/comptests.bash.nospace
# Global variable to keep track of if a test has failed.
_completionTests_TEST_FAILED=0
# Setup bash_completion package
bashCompletionScript="/usr/share/bash-completion/bash_completion"
if [ $(uname) = "Darwin" ]; then
bashCompletionScript="/usr/local/etc/profile.d/bash_completion.sh"
# Trick bash-completion into thinking this is an interactive shell
# See the if statement at the beginning of /usr/local/etc/profile.d/bash_completion.sh
PS1="%"
fi
source ${bashCompletionScript}
# compopt does not exist for bash 3, so we don't
# define it and if it is called by mistake it will
# cause an error.
if [ $BASH_VERSINFO -gt 3 ]; then
# compopt cannot be used outside of real shell
# completion. What we do instead is keep track
# of what options are chosen, to check them.
compopt() {
if [ "$*" = "+o default" ]; then
touch $_compTests_nofile
elif [ "$*" = "-o default" ]; then
rm -f $_compTests_nofile
elif [ "$*" = "-o nospace" ]; then
touch $_compTests_nospace
elif [ "$*" = "+o nospace" ]; then
rm -f $_compTests_nospace
fi
}
fi
_completionTests_reset() {
rm -f $_compTests_nofile
rm -f $_compTests_nospace
}
# Enable aliases to work even though we are in a script (non-interactive shell).
# This allows to test completion with aliases.
# Only needed for bash; zsh does this automatically.
shopt -s expand_aliases
# Run completion and indicate success or failure.
# $1 is the command line that should be completed
# $2 is the expected result of the completion
_completionTests_verifyCompletion() {
_completionTests_reset
local cmdLine=$1
local expected=$2
local currentFailure=0
local nofile=0
local nospace=0
case "$3" in
"")
;;
nofile)
nofile=1
case "$4" in
"")
;;
nospace)
nospace=1
;;
*)
echo "Invalid directive: $4"
exit 1
;;
esac
;;
nospace)
nospace=1
case "$4" in
"")
;;
nofile)
nofile=1
;;
*)
echo "Invalid directive: $4"
exit 1
;;
esac
;;
*)
echo "Invalid directive: $3"
exit 1
;;
esac
result=$(_completionTests_complete "${cmdLine}")
result=$(_completionTests_sort "$result")
expected=$(_completionTests_sort "$expected")
if [ "$result" = "$expected" ]; then
if ! _completionTests_checkDirective $nofile $nospace "$cmdLine"; then
_completionTests_TEST_FAILED=1
return 1
fi
# Truncate result to save space
resultOut="$result"
if [ "${#result}" -gt 50 ]; then
resultOut="${result:0:50} <truncated>"
fi
echo -e "${GREEN}SUCCESS: \"$cmdLine\" completes to \"$resultOut\"$NC"
return 0
fi
_completionTests_TEST_FAILED=1
echo -e "${RED}ERROR: \"$cmdLine\" should complete to \"$expected\" but we got \"$result\"$NC"
return 1
}
_completionTests_sort() {
# We use printf instead of echo as the $1 could be -n which would be
# interpreted as an argument to echo
if [ -n "$BASH_COMP_NO_SORT" ]; then
# Don't sort, just print back what we received
printf "%s\n" "$1"
else
# sort the output
printf "%s\n" "$1" | sed -e 's/^ *//' -e 's/ *$//' | tr ' ' '\n' | sort -n | tr '\n' ' ' | sed -e 's/^ *//' -e 's/ *$//'
fi
}
# $1 - The completion to measure
# $2 - The maximum time to consider it an error
# $3 - An output prefix
_completionTests_timing() {
TIMEFORMAT=%R
timing=$({ time { _completionTests_complete "$1" > /dev/null; } } 2>&1)
if (( $(echo "$timing > ${2}" | bc -l) )); then
_completionTests_TEST_FAILED=1
echo -e "${RED}<= TIMING => ${3}: 1000 completions took ${timing} seconds > ${2-0.1} seconds limit$NC"
return 1
else
echo -e "${GREEN}<= TIMING => ${3}: 1000 completions took ${timing} seconds < ${2-0.1} seconds limit$NC"
return 0
fi
}
# Find the completion function associated with the binary.
# $1 is the first argument of the line to complete which allows
# us to find the existing completion function name.
_completionTests_findCompletionFunction() {
binary=$(basename $1)
local out=($(complete -p ${binary}))
local returnNext=0
for i in ${out[@]}; do
if [ $returnNext -eq 1 ]; then
echo "$i"
return
fi
[ "$i" = "-F" ] && returnNext=1
done
}
_completionTests_complete() {
local cmdLine=$1
# Set the bash completion variables which are
# used for both bash and zsh completion
COMP_LINE=${cmdLine}
COMP_POINT=${#COMP_LINE}
COMP_TYPE=${COMP_TYPE-9} # 9 is TAB, but we allow to override for some tests
COMP_KEY=9 # 9 is TAB
COMP_WORDS=($(echo ${cmdLine}))
COMP_CWORD=$((${#COMP_WORDS[@]}-1))
# We must check for a space as the last character which will tell us
# that the previous word is complete and the cursor is on the next word.
[ "${cmdLine: -1}" = " " ] && COMP_CWORD=${#COMP_WORDS[@]}
# Call the completion function associated with the binary being called.
# Also redirect stderr to stdout so that the tests fail if anything is printed
# to stderr.
eval $(_completionTests_findCompletionFunction ${COMP_WORDS[0]}) 2>&1
# Return the result of the completion.
# We use printf instead of echo as the first completion could be -n which
# would be interpreted as an argument to echo
printf "%s\n" "${COMPREPLY[@]}"
}
_completionTests_exit() {
# Return the global result each time. This allows for the very last call to
# this method to return the correct success or failure code for the entire script
return $_completionTests_TEST_FAILED
}
_completionTests_checkDirective() {
# compopt does not exist for bash 3 so shell directives
# don't work. Don't fail in this case.
if [ $BASH_VERSINFO -eq 3 ]; then
return 0
fi
local requestnofile=$1
local requestnospace=$2
local cmdLine=$3
local realnofile=0
[ -f $_compTests_nofile ] && realnofile=1
local realnospace=0
[ -f $_compTests_nospace ] && realnospace=1
if [ $requestnofile -ne $realnofile ]; then
echo -e "${RED}ERROR: \"$cmdLine\" expected nofile=$requestnofile but got nofile=$realnofile$NC"
return 1
fi
if [ $requestnospace -ne $realnospace ]; then
echo -e "${RED}ERROR: \"$cmdLine\" expected nospace=$requestnospace but got nospace=$realnospace$NC"
return 1
fi
return 0
}