t3904-stash-patch: fix test description
[git.git] / t / t9902-completion.sh
blob7a883d1a674dbed174611470c93ffe32f4f8a4bc
1 #!/bin/sh
3 # Copyright (c) 2012 Felipe Contreras
6 test_description='test bash completion'
8 . ./lib-bash.sh
10 complete ()
12 # do nothing
13 return 0
16 # Be careful when updating this list:
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.
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.
33 GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
35 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
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 ()
42 while [ $# -gt 0 ]; do
43 case "$1" in
44 cur)
45 cur=${_words[_cword]}
47 prev)
48 prev=${_words[_cword-1]}
50 words)
51 words=("${_words[@]}")
53 cword)
54 cword=$_cword
56 esac
57 shift
58 done
61 print_comp ()
63 local IFS=$'\n'
64 echo "${COMPREPLY[*]}" > out
67 run_completion ()
69 local -a COMPREPLY _words
70 local _cword
71 _words=( $1 )
72 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
73 (( _cword = ${#_words[@]} - 1 ))
74 __git_wrap__git_main && print_comp
77 # Test high-level completion
78 # Arguments are:
79 # 1: typed text so far (cur)
80 # 2: expected completion
81 test_completion ()
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
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 ()
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
108 # Test __gitcomp_nl
109 # Arguments are:
110 # 1: current word (cur)
111 # -: the rest are passed to __gitcomp_nl
112 test_gitcomp_nl ()
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
123 invalid_variable_name='${foo.bar}'
125 actual="$TRASH_DIRECTORY/actual"
127 test_expect_success 'setup for __gitdir tests' '
128 mkdir -p subdir/subsubdir &&
129 git init otherrepo
132 test_expect_success '__gitdir - from command line (through $__git_dir)' '
133 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
135 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
136 __gitdir >"$actual"
137 ) &&
138 test_cmp expected "$actual"
141 test_expect_success '__gitdir - repo as argument' '
142 echo "otherrepo/.git" >expected &&
143 __gitdir "otherrepo" >"$actual" &&
144 test_cmp expected "$actual"
147 test_expect_success '__gitdir - remote as argument' '
148 echo "remote" >expected &&
149 __gitdir "remote" >"$actual" &&
150 test_cmp expected "$actual"
153 test_expect_success '__gitdir - .git directory in cwd' '
154 echo ".git" >expected &&
155 __gitdir >"$actual" &&
156 test_cmp expected "$actual"
159 test_expect_success '__gitdir - .git directory in parent' '
160 echo "$(pwd -P)/.git" >expected &&
162 cd subdir/subsubdir &&
163 __gitdir >"$actual"
164 ) &&
165 test_cmp expected "$actual"
168 test_expect_success '__gitdir - cwd is a .git directory' '
169 echo "." >expected &&
171 cd .git &&
172 __gitdir >"$actual"
173 ) &&
174 test_cmp expected "$actual"
177 test_expect_success '__gitdir - parent is a .git directory' '
178 echo "$(pwd -P)/.git" >expected &&
180 cd .git/refs/heads &&
181 __gitdir >"$actual"
182 ) &&
183 test_cmp expected "$actual"
186 test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
187 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
189 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
190 export GIT_DIR &&
191 __gitdir >"$actual"
192 ) &&
193 test_cmp expected "$actual"
196 test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
197 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
199 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
200 export GIT_DIR &&
201 cd subdir &&
202 __gitdir >"$actual"
203 ) &&
204 test_cmp expected "$actual"
207 test_expect_success '__gitdir - non-existing $GIT_DIR' '
209 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
210 export GIT_DIR &&
211 test_must_fail __gitdir
215 function pwd_P_W () {
216 if test_have_prereq MINGW
217 then
218 pwd -W
219 else
220 pwd -P
224 test_expect_success '__gitdir - gitfile in cwd' '
225 echo "$(pwd_P_W)/otherrepo/.git" >expected &&
226 echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
227 test_when_finished "rm -f subdir/.git" &&
229 cd subdir &&
230 __gitdir >"$actual"
231 ) &&
232 test_cmp expected "$actual"
235 test_expect_success '__gitdir - gitfile in parent' '
236 echo "$(pwd_P_W)/otherrepo/.git" >expected &&
237 echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
238 test_when_finished "rm -f subdir/.git" &&
240 cd subdir/subsubdir &&
241 __gitdir >"$actual"
242 ) &&
243 test_cmp expected "$actual"
246 test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
247 echo "$(pwd -P)/otherrepo/.git" >expected &&
248 mkdir otherrepo/dir &&
249 test_when_finished "rm -rf otherrepo/dir" &&
250 ln -s otherrepo/dir link &&
251 test_when_finished "rm -f link" &&
253 cd link &&
254 __gitdir >"$actual"
255 ) &&
256 test_cmp expected "$actual"
259 test_expect_success '__gitdir - not a git repository' '
261 cd subdir/subsubdir &&
262 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
263 export GIT_CEILING_DIRECTORIES &&
264 test_must_fail __gitdir
268 test_expect_success '__gitcomp - trailing space - options' '
269 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
270 --reset-author" <<-EOF
271 --reuse-message=Z
272 --reedit-message=Z
273 --reset-author Z
277 test_expect_success '__gitcomp - trailing space - config keys' '
278 test_gitcomp "br" "branch. branch.autosetupmerge
279 branch.autosetuprebase browser." <<-\EOF
280 branch.Z
281 branch.autosetupmerge Z
282 branch.autosetuprebase Z
283 browser.Z
287 test_expect_success '__gitcomp - option parameter' '
288 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
289 "" "re" <<-\EOF
290 recursive Z
291 resolve Z
295 test_expect_success '__gitcomp - prefix' '
296 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
297 "branch.maint." "me" <<-\EOF
298 branch.maint.merge Z
299 branch.maint.mergeoptions Z
303 test_expect_success '__gitcomp - suffix' '
304 test_gitcomp "branch.me" "master maint next pu" "branch." \
305 "ma" "." <<-\EOF
306 branch.master.Z
307 branch.maint.Z
311 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
312 __gitcomp "$invalid_variable_name"
315 read -r -d "" refs <<-\EOF
316 maint
317 master
318 next
322 test_expect_success '__gitcomp_nl - trailing space' '
323 test_gitcomp_nl "m" "$refs" <<-EOF
324 maint Z
325 master Z
329 test_expect_success '__gitcomp_nl - prefix' '
330 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
331 --fixup=maint Z
332 --fixup=master Z
336 test_expect_success '__gitcomp_nl - suffix' '
337 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
338 branch.maint.Z
339 branch.master.Z
343 test_expect_success '__gitcomp_nl - no suffix' '
344 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
345 maintZ
346 masterZ
350 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
351 __gitcomp_nl "$invalid_variable_name"
354 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
355 cat >expect <<-EOF &&
356 remote_from_file_1
357 remote_from_file_2
358 remote_in_config_1
359 remote_in_config_2
361 test_when_finished "rm -rf .git/remotes" &&
362 mkdir -p .git/remotes &&
363 >.git/remotes/remote_from_file_1 &&
364 >.git/remotes/remote_from_file_2 &&
365 test_when_finished "git remote remove remote_in_config_1" &&
366 git remote add remote_in_config_1 git://remote_1 &&
367 test_when_finished "git remote remove remote_in_config_2" &&
368 git remote add remote_in_config_2 git://remote_2 &&
369 __git_remotes >actual &&
370 test_cmp expect actual
373 test_expect_success 'basic' '
374 run_completion "git " &&
375 # built-in
376 grep -q "^add \$" out &&
377 # script
378 grep -q "^filter-branch \$" out &&
379 # plumbing
380 ! grep -q "^ls-files \$" out &&
382 run_completion "git f" &&
383 ! grep -q -v "^f" out
386 test_expect_success 'double dash "git" itself' '
387 test_completion "git --" <<-\EOF
388 --paginate Z
389 --no-pager Z
390 --git-dir=
391 --bare Z
392 --version Z
393 --exec-path Z
394 --exec-path=
395 --html-path Z
396 --man-path Z
397 --info-path Z
398 --work-tree=
399 --namespace=
400 --no-replace-objects Z
401 --help Z
405 test_expect_success 'double dash "git checkout"' '
406 test_completion "git checkout --" <<-\EOF
407 --quiet Z
408 --ours Z
409 --theirs Z
410 --track Z
411 --no-track Z
412 --merge Z
413 --conflict=
414 --orphan Z
415 --patch Z
419 test_expect_success 'general options' '
420 test_completion "git --ver" "--version " &&
421 test_completion "git --hel" "--help " &&
422 test_completion "git --exe" <<-\EOF &&
423 --exec-path Z
424 --exec-path=
426 test_completion "git --htm" "--html-path " &&
427 test_completion "git --pag" "--paginate " &&
428 test_completion "git --no-p" "--no-pager " &&
429 test_completion "git --git" "--git-dir=" &&
430 test_completion "git --wor" "--work-tree=" &&
431 test_completion "git --nam" "--namespace=" &&
432 test_completion "git --bar" "--bare " &&
433 test_completion "git --inf" "--info-path " &&
434 test_completion "git --no-r" "--no-replace-objects "
437 test_expect_success 'general options plus command' '
438 test_completion "git --version check" "checkout " &&
439 test_completion "git --paginate check" "checkout " &&
440 test_completion "git --git-dir=foo check" "checkout " &&
441 test_completion "git --bare check" "checkout " &&
442 test_completion "git --exec-path=foo check" "checkout " &&
443 test_completion "git --html-path check" "checkout " &&
444 test_completion "git --no-pager check" "checkout " &&
445 test_completion "git --work-tree=foo check" "checkout " &&
446 test_completion "git --namespace=foo check" "checkout " &&
447 test_completion "git --paginate check" "checkout " &&
448 test_completion "git --info-path check" "checkout " &&
449 test_completion "git --no-replace-objects check" "checkout "
452 test_expect_success 'git --help completion' '
453 test_completion "git --help ad" "add " &&
454 test_completion "git --help core" "core-tutorial "
457 test_expect_success 'setup for ref completion' '
458 echo content >file1 &&
459 echo more >file2 &&
460 git add . &&
461 git commit -m one &&
462 git branch mybranch &&
463 git tag mytag
466 test_expect_success 'checkout completes ref names' '
467 test_completion "git checkout m" <<-\EOF
468 master Z
469 mybranch Z
470 mytag Z
474 test_expect_success 'show completes all refs' '
475 test_completion "git show m" <<-\EOF
476 master Z
477 mybranch Z
478 mytag Z
482 test_expect_success '<ref>: completes paths' '
483 test_completion "git show mytag:f" <<-\EOF
484 file1 Z
485 file2 Z
489 test_expect_success 'complete tree filename with spaces' '
490 echo content >"name with spaces" &&
491 git add . &&
492 git commit -m spaces &&
493 test_completion "git show HEAD:nam" <<-\EOF
494 name with spaces Z
498 test_expect_success 'complete tree filename with metacharacters' '
499 echo content >"name with \${meta}" &&
500 git add . &&
501 git commit -m meta &&
502 test_completion "git show HEAD:nam" <<-\EOF
503 name with ${meta} Z
504 name with spaces Z
508 test_expect_success 'send-email' '
509 test_completion "git send-email --cov" "--cover-letter " &&
510 test_completion "git send-email ma" "master "
513 test_expect_success 'complete files' '
514 git init tmp && cd tmp &&
515 test_when_finished "cd .. && rm -rf tmp" &&
517 echo "expected" > .gitignore &&
518 echo "out" >> .gitignore &&
520 git add .gitignore &&
521 test_completion "git commit " ".gitignore" &&
523 git commit -m ignore &&
525 touch new &&
526 test_completion "git add " "new" &&
528 git add new &&
529 git commit -a -m new &&
530 test_completion "git add " "" &&
532 git mv new modified &&
533 echo modify > modified &&
534 test_completion "git add " "modified" &&
536 touch untracked &&
538 : TODO .gitignore should not be here &&
539 test_completion "git rm " <<-\EOF &&
540 .gitignore
541 modified
544 test_completion "git clean " "untracked" &&
546 : TODO .gitignore should not be here &&
547 test_completion "git mv " <<-\EOF &&
548 .gitignore
549 modified
552 mkdir dir &&
553 touch dir/file-in-dir &&
554 git add dir/file-in-dir &&
555 git commit -m dir &&
557 mkdir untracked-dir &&
559 : TODO .gitignore should not be here &&
560 test_completion "git mv modified " <<-\EOF &&
561 .gitignore
563 modified
564 untracked
565 untracked-dir
568 test_completion "git commit " "modified" &&
570 : TODO .gitignore should not be here &&
571 test_completion "git ls-files " <<-\EOF
572 .gitignore
574 modified
577 touch momified &&
578 test_completion "git add mom" "momified"
581 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
582 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
583 test_completion "git co m" <<-\EOF
584 master Z
585 mybranch Z
586 mytag Z
590 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
591 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
592 test_completion "git co m" <<-\EOF
593 master Z
594 mybranch Z
595 mytag Z
599 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
600 test_config alias.co "!f() { : git checkout ; if ... } f" &&
601 test_completion "git co m" <<-\EOF
602 master Z
603 mybranch Z
604 mytag Z
608 test_expect_failure 'complete with tilde expansion' '
609 git init tmp && cd tmp &&
610 test_when_finished "cd .. && rm -rf tmp" &&
612 touch ~/tmp/file &&
614 test_completion "git add ~/tmp/" "~/tmp/file"
617 test_done