]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9902-completion.sh
completion: add new __gitcompadd helper
[thirdparty/git.git] / t / t9902-completion.sh
CommitLineData
5c293a6b
FC
1#!/bin/sh
2#
3# Copyright (c) 2012 Felipe Contreras
4#
5
5c293a6b
FC
6test_description='test bash completion'
7
f8891cfa 8. ./lib-bash.sh
5c293a6b
FC
9
10complete ()
11{
12 # do nothing
13 return 0
14}
15
50478223
JH
16# Be careful when updating this list:
17#
18# (1) The build tree may have build artifact from different branch, or
19# the user's $PATH may have a random executable that may begin
20# with "git-check" that are not part of the subcommands this build
21# will ship, e.g. "check-ignore". The tests for completion for
22# subcommand names tests how "check" is expanded; we limit the
23# possible candidates to "checkout" and "check-attr" to make sure
24# "check-attr", which is known by the filter function as a
25# subcommand to be thrown out, while excluding other random files
26# that happen to begin with "check" to avoid letting them get in
27# the way.
28#
29# (2) A test makes sure that common subcommands are included in the
30# completion for "git <TAB>", and a plumbing is excluded. "add",
31# "filter-branch" and "ls-files" are listed for this.
32
33GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
34
5c293a6b
FC
35. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
36
37# We don't need this function to actually join words or do anything special.
38# Also, it's cleaner to avoid touching bash's internal completion variables.
39# So let's override it with a minimal version for testing purposes.
40_get_comp_words_by_ref ()
41{
42 while [ $# -gt 0 ]; do
43 case "$1" in
44 cur)
45 cur=${_words[_cword]}
46 ;;
47 prev)
48 prev=${_words[_cword-1]}
49 ;;
50 words)
51 words=("${_words[@]}")
52 ;;
53 cword)
54 cword=$_cword
55 ;;
56 esac
57 shift
58 done
59}
60
61print_comp ()
62{
63 local IFS=$'\n'
64 echo "${COMPREPLY[*]}" > out
65}
66
67run_completion ()
68{
69 local -a COMPREPLY _words
70 local _cword
71 _words=( $1 )
cdbff7d6 72 test "${1: -1}" == ' ' && _words+=('')
5c293a6b 73 (( _cword = ${#_words[@]} - 1 ))
93b291e0 74 __git_wrap__git_main && print_comp
5c293a6b
FC
75}
76
701ecdf1
FC
77# Test high-level completion
78# Arguments are:
79# 1: typed text so far (cur)
80# 2: expected completion
5c293a6b
FC
81test_completion ()
82{
2fbaf813
FC
83 if test $# -gt 1
84 then
85 printf '%s\n' "$2" >expected
86 else
87 sed -e 's/Z$//' >expected
88 fi &&
701ecdf1 89 run_completion "$1" &&
5c293a6b
FC
90 test_cmp expected out
91}
92
e4615238
FC
93# Test __gitcomp.
94# The first argument is the typed text so far (cur); the rest are
95# passed to __gitcomp. Expected output comes is read from the
96# standard input, like test_completion().
97test_gitcomp ()
49ba92b4 98{
17393033 99 local -a COMPREPLY &&
e4615238 100 sed -e 's/Z$//' >expected &&
17393033
FC
101 cur="$1" &&
102 shift &&
103 __gitcomp "$@" &&
104 print_comp &&
74a8c849 105 test_cmp expected out
49ba92b4
JK
106}
107
74a8c849 108test_expect_success '__gitcomp - trailing space - options' '
e4615238
FC
109 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
110 --reset-author" <<-EOF
74a8c849
SG
111 --reuse-message=Z
112 --reedit-message=Z
113 --reset-author Z
114 EOF
74a8c849
SG
115'
116
117test_expect_success '__gitcomp - trailing space - config keys' '
e4615238
FC
118 test_gitcomp "br" "branch. branch.autosetupmerge
119 branch.autosetuprebase browser." <<-\EOF
74a8c849
SG
120 branch.Z
121 branch.autosetupmerge Z
122 branch.autosetuprebase Z
123 browser.Z
124 EOF
74a8c849
SG
125'
126
127test_expect_success '__gitcomp - option parameter' '
e4615238
FC
128 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
129 "" "re" <<-\EOF
74a8c849
SG
130 recursive Z
131 resolve Z
132 EOF
74a8c849
SG
133'
134
135test_expect_success '__gitcomp - prefix' '
e4615238
FC
136 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
137 "branch.maint." "me" <<-\EOF
74a8c849
SG
138 branch.maint.merge Z
139 branch.maint.mergeoptions Z
140 EOF
74a8c849
SG
141'
142
143test_expect_success '__gitcomp - suffix' '
e4615238
FC
144 test_gitcomp "branch.me" "master maint next pu" "branch." \
145 "ma" "." <<-\EOF
74a8c849
SG
146 branch.master.Z
147 branch.maint.Z
148 EOF
74a8c849
SG
149'
150
5c293a6b 151test_expect_success 'basic' '
cdbff7d6 152 run_completion "git " &&
5c293a6b
FC
153 # built-in
154 grep -q "^add \$" out &&
155 # script
156 grep -q "^filter-branch \$" out &&
157 # plumbing
158 ! grep -q "^ls-files \$" out &&
159
160 run_completion "git f" &&
161 ! grep -q -v "^f" out
162'
163
164test_expect_success 'double dash "git" itself' '
2fbaf813 165 test_completion "git --" <<-\EOF
5c293a6b
FC
166 --paginate Z
167 --no-pager Z
168 --git-dir=
169 --bare Z
170 --version Z
171 --exec-path Z
3ffcd086 172 --exec-path=
5c293a6b 173 --html-path Z
69ef3c02 174 --info-path Z
5c293a6b
FC
175 --work-tree=
176 --namespace=
69ef3c02 177 --no-replace-objects Z
5c293a6b
FC
178 --help Z
179 EOF
5c293a6b
FC
180'
181
182test_expect_success 'double dash "git checkout"' '
2fbaf813 183 test_completion "git checkout --" <<-\EOF
5c293a6b
FC
184 --quiet Z
185 --ours Z
186 --theirs Z
187 --track Z
188 --no-track Z
189 --merge Z
190 --conflict=
191 --orphan Z
192 --patch Z
193 EOF
5c293a6b
FC
194'
195
69ef3c02
FC
196test_expect_success 'general options' '
197 test_completion "git --ver" "--version " &&
198 test_completion "git --hel" "--help " &&
2fbaf813 199 test_completion "git --exe" <<-\EOF &&
3ffcd086
JN
200 --exec-path Z
201 --exec-path=
202 EOF
69ef3c02
FC
203 test_completion "git --htm" "--html-path " &&
204 test_completion "git --pag" "--paginate " &&
205 test_completion "git --no-p" "--no-pager " &&
206 test_completion "git --git" "--git-dir=" &&
207 test_completion "git --wor" "--work-tree=" &&
208 test_completion "git --nam" "--namespace=" &&
209 test_completion "git --bar" "--bare " &&
210 test_completion "git --inf" "--info-path " &&
211 test_completion "git --no-r" "--no-replace-objects "
212'
911d5da6
SG
213
214test_expect_success 'general options plus command' '
215 test_completion "git --version check" "checkout " &&
216 test_completion "git --paginate check" "checkout " &&
217 test_completion "git --git-dir=foo check" "checkout " &&
218 test_completion "git --bare check" "checkout " &&
911d5da6
SG
219 test_completion "git --exec-path=foo check" "checkout " &&
220 test_completion "git --html-path check" "checkout " &&
221 test_completion "git --no-pager check" "checkout " &&
222 test_completion "git --work-tree=foo check" "checkout " &&
223 test_completion "git --namespace=foo check" "checkout " &&
224 test_completion "git --paginate check" "checkout " &&
225 test_completion "git --info-path check" "checkout " &&
226 test_completion "git --no-replace-objects check" "checkout "
227'
228
50478223
JH
229test_expect_success 'git --help completion' '
230 test_completion "git --help ad" "add " &&
231 test_completion "git --help core" "core-tutorial "
232'
233
49ba92b4
JK
234test_expect_success 'setup for ref completion' '
235 echo content >file1 &&
236 echo more >file2 &&
237 git add . &&
238 git commit -m one &&
239 git branch mybranch &&
240 git tag mytag
241'
242
243test_expect_success 'checkout completes ref names' '
2fbaf813 244 test_completion "git checkout m" <<-\EOF
43ea0812
FC
245 master Z
246 mybranch Z
247 mytag Z
49ba92b4
JK
248 EOF
249'
250
251test_expect_success 'show completes all refs' '
2fbaf813 252 test_completion "git show m" <<-\EOF
43ea0812
FC
253 master Z
254 mybranch Z
255 mytag Z
49ba92b4
JK
256 EOF
257'
258
259test_expect_success '<ref>: completes paths' '
2fbaf813 260 test_completion "git show mytag:f" <<-\EOF
43ea0812
FC
261 file1 Z
262 file2 Z
49ba92b4
JK
263 EOF
264'
265
bafed0df
JK
266test_expect_success 'complete tree filename with spaces' '
267 echo content >"name with spaces" &&
268 git add . &&
269 git commit -m spaces &&
2fbaf813 270 test_completion "git show HEAD:nam" <<-\EOF
43ea0812 271 name with spaces Z
bafed0df
JK
272 EOF
273'
274
275test_expect_failure 'complete tree filename with metacharacters' '
276 echo content >"name with \${meta}" &&
277 git add . &&
278 git commit -m meta &&
2fbaf813 279 test_completion "git show HEAD:nam" <<-\EOF
43ea0812
FC
280 name with ${meta} Z
281 name with spaces Z
bafed0df
JK
282 EOF
283'
284
2f65494d
FC
285test_expect_success 'send-email' '
286 test_completion "git send-email --cov" "--cover-letter " &&
287 test_completion "git send-email ma" "master "
288'
289
5c293a6b 290test_done