]> git.ipfire.org Git - thirdparty/git.git/blob - t/t9902-completion.sh
completion: learn about --man-path
[thirdparty/git.git] / t / t9902-completion.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 Felipe Contreras
4 #
5
6 test_description='test bash completion'
7
8 . ./lib-bash.sh
9
10 complete ()
11 {
12 # do nothing
13 return 0
14 }
15
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
33 GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
34
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
61 print_comp ()
62 {
63 local IFS=$'\n'
64 echo "${COMPREPLY[*]}" > out
65 }
66
67 run_completion ()
68 {
69 local -a COMPREPLY _words
70 local _cword
71 _words=( $1 )
72 test "${1: -1}" == ' ' && _words+=('')
73 (( _cword = ${#_words[@]} - 1 ))
74 __git_wrap__git_main && print_comp
75 }
76
77 # Test high-level completion
78 # Arguments are:
79 # 1: typed text so far (cur)
80 # 2: expected completion
81 test_completion ()
82 {
83 if test $# -gt 1
84 then
85 printf '%s\n' "$2" >expected
86 else
87 sed -e 's/Z$//' >expected
88 fi &&
89 run_completion "$1" &&
90 test_cmp expected out
91 }
92
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().
97 test_gitcomp ()
98 {
99 local -a COMPREPLY &&
100 sed -e 's/Z$//' >expected &&
101 cur="$1" &&
102 shift &&
103 __gitcomp "$@" &&
104 print_comp &&
105 test_cmp expected out
106 }
107
108 # Test __gitcomp_nl
109 # Arguments are:
110 # 1: current word (cur)
111 # -: the rest are passed to __gitcomp_nl
112 test_gitcomp_nl ()
113 {
114 local -a COMPREPLY &&
115 sed -e 's/Z$//' >expected &&
116 cur="$1" &&
117 shift &&
118 __gitcomp_nl "$@" &&
119 print_comp &&
120 test_cmp expected out
121 }
122
123 invalid_variable_name='${foo.bar}'
124
125 test_expect_success '__gitcomp - trailing space - options' '
126 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
127 --reset-author" <<-EOF
128 --reuse-message=Z
129 --reedit-message=Z
130 --reset-author Z
131 EOF
132 '
133
134 test_expect_success '__gitcomp - trailing space - config keys' '
135 test_gitcomp "br" "branch. branch.autosetupmerge
136 branch.autosetuprebase browser." <<-\EOF
137 branch.Z
138 branch.autosetupmerge Z
139 branch.autosetuprebase Z
140 browser.Z
141 EOF
142 '
143
144 test_expect_success '__gitcomp - option parameter' '
145 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
146 "" "re" <<-\EOF
147 recursive Z
148 resolve Z
149 EOF
150 '
151
152 test_expect_success '__gitcomp - prefix' '
153 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
154 "branch.maint." "me" <<-\EOF
155 branch.maint.merge Z
156 branch.maint.mergeoptions Z
157 EOF
158 '
159
160 test_expect_success '__gitcomp - suffix' '
161 test_gitcomp "branch.me" "master maint next pu" "branch." \
162 "ma" "." <<-\EOF
163 branch.master.Z
164 branch.maint.Z
165 EOF
166 '
167
168 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
169 __gitcomp "$invalid_variable_name"
170 '
171
172 read -r -d "" refs <<-\EOF
173 maint
174 master
175 next
176 pu
177 EOF
178
179 test_expect_success '__gitcomp_nl - trailing space' '
180 test_gitcomp_nl "m" "$refs" <<-EOF
181 maint Z
182 master Z
183 EOF
184 '
185
186 test_expect_success '__gitcomp_nl - prefix' '
187 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
188 --fixup=maint Z
189 --fixup=master Z
190 EOF
191 '
192
193 test_expect_success '__gitcomp_nl - suffix' '
194 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
195 branch.maint.Z
196 branch.master.Z
197 EOF
198 '
199
200 test_expect_success '__gitcomp_nl - no suffix' '
201 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
202 maintZ
203 masterZ
204 EOF
205 '
206
207 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
208 __gitcomp_nl "$invalid_variable_name"
209 '
210
211 test_expect_success 'basic' '
212 run_completion "git " &&
213 # built-in
214 grep -q "^add \$" out &&
215 # script
216 grep -q "^filter-branch \$" out &&
217 # plumbing
218 ! grep -q "^ls-files \$" out &&
219
220 run_completion "git f" &&
221 ! grep -q -v "^f" out
222 '
223
224 test_expect_success 'double dash "git" itself' '
225 test_completion "git --" <<-\EOF
226 --paginate Z
227 --no-pager Z
228 --git-dir=
229 --bare Z
230 --version Z
231 --exec-path Z
232 --exec-path=
233 --html-path Z
234 --man-path Z
235 --info-path Z
236 --work-tree=
237 --namespace=
238 --no-replace-objects Z
239 --help Z
240 EOF
241 '
242
243 test_expect_success 'double dash "git checkout"' '
244 test_completion "git checkout --" <<-\EOF
245 --quiet Z
246 --ours Z
247 --theirs Z
248 --track Z
249 --no-track Z
250 --merge Z
251 --conflict=
252 --orphan Z
253 --patch Z
254 EOF
255 '
256
257 test_expect_success 'general options' '
258 test_completion "git --ver" "--version " &&
259 test_completion "git --hel" "--help " &&
260 test_completion "git --exe" <<-\EOF &&
261 --exec-path Z
262 --exec-path=
263 EOF
264 test_completion "git --htm" "--html-path " &&
265 test_completion "git --pag" "--paginate " &&
266 test_completion "git --no-p" "--no-pager " &&
267 test_completion "git --git" "--git-dir=" &&
268 test_completion "git --wor" "--work-tree=" &&
269 test_completion "git --nam" "--namespace=" &&
270 test_completion "git --bar" "--bare " &&
271 test_completion "git --inf" "--info-path " &&
272 test_completion "git --no-r" "--no-replace-objects "
273 '
274
275 test_expect_success 'general options plus command' '
276 test_completion "git --version check" "checkout " &&
277 test_completion "git --paginate check" "checkout " &&
278 test_completion "git --git-dir=foo check" "checkout " &&
279 test_completion "git --bare check" "checkout " &&
280 test_completion "git --exec-path=foo check" "checkout " &&
281 test_completion "git --html-path check" "checkout " &&
282 test_completion "git --no-pager check" "checkout " &&
283 test_completion "git --work-tree=foo check" "checkout " &&
284 test_completion "git --namespace=foo check" "checkout " &&
285 test_completion "git --paginate check" "checkout " &&
286 test_completion "git --info-path check" "checkout " &&
287 test_completion "git --no-replace-objects check" "checkout "
288 '
289
290 test_expect_success 'git --help completion' '
291 test_completion "git --help ad" "add " &&
292 test_completion "git --help core" "core-tutorial "
293 '
294
295 test_expect_success 'setup for ref completion' '
296 echo content >file1 &&
297 echo more >file2 &&
298 git add . &&
299 git commit -m one &&
300 git branch mybranch &&
301 git tag mytag
302 '
303
304 test_expect_success 'checkout completes ref names' '
305 test_completion "git checkout m" <<-\EOF
306 master Z
307 mybranch Z
308 mytag Z
309 EOF
310 '
311
312 test_expect_success 'show completes all refs' '
313 test_completion "git show m" <<-\EOF
314 master Z
315 mybranch Z
316 mytag Z
317 EOF
318 '
319
320 test_expect_success '<ref>: completes paths' '
321 test_completion "git show mytag:f" <<-\EOF
322 file1 Z
323 file2 Z
324 EOF
325 '
326
327 test_expect_success 'complete tree filename with spaces' '
328 echo content >"name with spaces" &&
329 git add . &&
330 git commit -m spaces &&
331 test_completion "git show HEAD:nam" <<-\EOF
332 name with spaces Z
333 EOF
334 '
335
336 test_expect_success 'complete tree filename with metacharacters' '
337 echo content >"name with \${meta}" &&
338 git add . &&
339 git commit -m meta &&
340 test_completion "git show HEAD:nam" <<-\EOF
341 name with ${meta} Z
342 name with spaces Z
343 EOF
344 '
345
346 test_expect_success 'send-email' '
347 test_completion "git send-email --cov" "--cover-letter " &&
348 test_completion "git send-email ma" "master "
349 '
350
351 test_expect_success 'complete files' '
352 git init tmp && cd tmp &&
353 test_when_finished "cd .. && rm -rf tmp" &&
354
355 echo "expected" > .gitignore &&
356 echo "out" >> .gitignore &&
357
358 git add .gitignore &&
359 test_completion "git commit " ".gitignore" &&
360
361 git commit -m ignore &&
362
363 touch new &&
364 test_completion "git add " "new" &&
365
366 git add new &&
367 git commit -a -m new &&
368 test_completion "git add " "" &&
369
370 git mv new modified &&
371 echo modify > modified &&
372 test_completion "git add " "modified" &&
373
374 touch untracked &&
375
376 : TODO .gitignore should not be here &&
377 test_completion "git rm " <<-\EOF &&
378 .gitignore
379 modified
380 EOF
381
382 test_completion "git clean " "untracked" &&
383
384 : TODO .gitignore should not be here &&
385 test_completion "git mv " <<-\EOF &&
386 .gitignore
387 modified
388 EOF
389
390 mkdir dir &&
391 touch dir/file-in-dir &&
392 git add dir/file-in-dir &&
393 git commit -m dir &&
394
395 mkdir untracked-dir &&
396
397 : TODO .gitignore should not be here &&
398 test_completion "git mv modified " <<-\EOF &&
399 .gitignore
400 dir
401 modified
402 untracked
403 untracked-dir
404 EOF
405
406 test_completion "git commit " "modified" &&
407
408 : TODO .gitignore should not be here &&
409 test_completion "git ls-files " <<-\EOF
410 .gitignore
411 dir
412 modified
413 EOF
414
415 touch momified &&
416 test_completion "git add mom" "momified"
417 '
418
419 test_expect_failure 'complete with tilde expansion' '
420 git init tmp && cd tmp &&
421 test_when_finished "cd .. && rm -rf tmp" &&
422
423 touch ~/tmp/file &&
424
425 test_completion "git add ~/tmp/" "~/tmp/file"
426 '
427
428 test_done