3 # Copyright (c) 2012 Felipe Contreras
6 test_description
='test bash completion'
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
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
48 prev
=${_words[_cword-1]}
51 words
=("${_words[@]}")
64 echo "${COMPREPLY[*]}" > out
69 local -a COMPREPLY _words
72 test "${1: -1}" = ' ' && _words
[${#_words[@]}+1]=''
73 (( _cword
= ${#_words[@]} - 1 ))
74 __git_wrap__git_main
&& print_comp
77 # Test high-level completion
79 # 1: typed text so far (cur)
80 # 2: expected completion
85 printf '%s\n' "$2" >expected
87 sed -e 's/Z$//' >expected
89 run_completion
"$1" &&
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().
100 sed -e 's/Z$//' >expected
&&
105 test_cmp expected out
110 # 1: current word (cur)
111 # -: the rest are passed to __gitcomp_nl
114 local -a COMPREPLY
&&
115 sed -e 's/Z$//' >expected
&&
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 &&
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" &&
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 &&
165 test_cmp expected "$actual"
168 test_expect_success
'__gitdir - cwd is a .git directory' '
169 echo "." >expected &&
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 &&
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" &&
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" &&
204 test_cmp expected "$actual"
207 test_expect_success
'__gitdir - non-existing $GIT_DIR' '
209 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
211 test_must_fail __gitdir
215 function pwd_P_W
() {
216 if test_have_prereq MINGW
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" &&
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 &&
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" &&
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
277 test_expect_success
'__gitcomp - trailing space - config keys' '
278 test_gitcomp "br" "branch. branch.autosetupmerge
279 branch.autosetuprebase browser." <<-\EOF
281 branch.autosetupmerge Z
282 branch.autosetuprebase Z
287 test_expect_success
'__gitcomp - option parameter' '
288 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
295 test_expect_success
'__gitcomp - prefix' '
296 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
297 "branch.maint." "me" <<-\EOF
299 branch.maint.mergeoptions Z
303 test_expect_success
'__gitcomp - suffix' '
304 test_gitcomp "branch.me" "master maint next pu" "branch." \
311 test_expect_success
'__gitcomp - doesnt fail because of invalid variable name' '
312 __gitcomp "$invalid_variable_name"
315 read -r -d "" refs
<<-\EOF
322 test_expect_success '__gitcomp_nl - trailing space
' '
323 test_gitcomp_nl
"m" "$refs" <<-EOF
329 test_expect_success '__gitcomp_nl
- prefix
' '
330 test_gitcomp_nl
"--fixup=m" "$refs" "--fixup=" "m" <<-EOF
336 test_expect_success '__gitcomp_nl
- suffix
' '
337 test_gitcomp_nl
"branch.ma" "$refs" "branch." "ma" "." <<-\EOF
343 test_expect_success '__gitcomp_nl - no suffix
' '
344 test_gitcomp_nl
"ma" "$refs" "" "ma" "" <<-\EOF
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 &&
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 '__git_get_config_variables
' '
374 cat >expect
<<-EOF &&
378 test_config interesting.name-1 good
&&
379 test_config interesting.name-2 good
&&
380 test_config subsection.interesting.name-3 bad
&&
381 __git_get_config_variables interesting
>actual
&&
382 test_cmp expect actual
385 test_expect_success '__git_pretty_aliases
' '
386 cat >expect
<<-EOF &&
390 test_config pretty.author
"%an %ae" &&
391 test_config pretty.
hash %H
&&
392 __git_pretty_aliases
>actual
&&
393 test_cmp expect actual
396 test_expect_success '__git_aliases
' '
397 cat >expect
<<-EOF &&
401 test_config
alias.ci commit
&&
402 test_config
alias.co checkout
&&
403 __git_aliases
>actual
&&
404 test_cmp expect actual
407 test_expect_success 'basic
' '
408 run_completion
"git " &&
410 grep -q "^add \$" out
&&
412 grep -q "^filter-branch \$" out
&&
414 ! grep -q "^ls-files \$" out
&&
416 run_completion
"git f" &&
417 ! grep -q -v "^f" out
420 test_expect_success 'double dash
"git" itself
' '
421 test_completion
"git --" <<-\EOF
434 --no-replace-objects Z
439 test_expect_success 'double dash "git checkout"' '
440 test_completion "git checkout --" <<-\
EOF
453 test_expect_success 'general options' '
454 test_completion "git --ver" "--version " &&
455 test_completion "git --hel" "--help " &&
456 test_completion "git --exe" <<-\EOF &&
460 test_completion "git --htm" "--html-path " &&
461 test_completion "git --pag" "--paginate " &&
462 test_completion "git --no-p" "--no-pager " &&
463 test_completion "git --git" "--git-dir=" &&
464 test_completion "git --wor" "--work-tree=" &&
465 test_completion "git --nam" "--namespace=" &&
466 test_completion "git --bar" "--bare " &&
467 test_completion "git --inf" "--info-path " &&
468 test_completion "git --no-r" "--no-replace-objects "
471 test_expect_success 'general options plus command' '
472 test_completion "git --version check" "checkout " &&
473 test_completion "git --paginate check" "checkout " &&
474 test_completion "git --git-dir=foo check" "checkout " &&
475 test_completion "git --bare check" "checkout " &&
476 test_completion "git --exec-path=foo check" "checkout " &&
477 test_completion "git --html-path check" "checkout " &&
478 test_completion "git --no-pager check" "checkout " &&
479 test_completion "git --work-tree=foo check" "checkout " &&
480 test_completion "git --namespace=foo check" "checkout " &&
481 test_completion "git --paginate check" "checkout " &&
482 test_completion "git --info-path check" "checkout " &&
483 test_completion "git --no-replace-objects check" "checkout "
486 test_expect_success 'git --help completion' '
487 test_completion "git --help ad" "add " &&
488 test_completion "git --help core" "core-tutorial "
491 test_expect_success 'setup for ref completion' '
492 echo content >file1 &&
496 git branch mybranch &&
500 test_expect_success 'checkout completes ref names' '
501 test_completion "git checkout m" <<-\EOF
508 test_expect_success 'show completes all refs' '
509 test_completion "git show m" <<-\EOF
516 test_expect_success '<ref>: completes paths' '
517 test_completion "git show mytag:f" <<-\EOF
523 test_expect_success 'complete tree filename with spaces' '
524 echo content >"name with spaces" &&
526 git commit -m spaces &&
527 test_completion "git show HEAD:nam" <<-\EOF
532 test_expect_success 'complete tree filename with metacharacters' '
533 echo content >"name with \${meta}" &&
535 git commit -m meta &&
536 test_completion "git show HEAD:nam" <<-\EOF
542 test_expect_success 'send-email' '
543 test_completion "git send-email --cov" "--cover-letter " &&
544 test_completion "git send-email ma" "master "
547 test_expect_success 'complete files' '
548 git init tmp && cd tmp &&
549 test_when_finished "cd .. && rm -rf tmp" &&
551 echo "expected" > .gitignore &&
552 echo "out" >> .gitignore &&
554 git add .gitignore &&
555 test_completion "git commit " ".gitignore" &&
557 git commit -m ignore &&
560 test_completion "git add " "new" &&
563 git commit -a -m new &&
564 test_completion "git add " "" &&
566 git mv new modified &&
567 echo modify > modified &&
568 test_completion "git add " "modified" &&
572 : TODO .gitignore should not be here &&
573 test_completion "git rm " <<-\EOF &&
578 test_completion "git clean " "untracked" &&
580 : TODO .gitignore should not be here &&
581 test_completion "git mv " <<-\EOF &&
587 touch dir/file-in-dir &&
588 git add dir/file-in-dir &&
591 mkdir untracked-dir &&
593 : TODO .gitignore should not be here &&
594 test_completion "git mv modified " <<-\EOF &&
602 test_completion "git commit " "modified" &&
604 : TODO .gitignore should not be here &&
605 test_completion "git ls-files " <<-\EOF &&
612 test_completion "git add mom" "momified"
615 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
616 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
617 test_completion "git co m" <<-\EOF
624 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
625 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
626 test_completion "git co m" <<-\EOF
633 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
634 test_config alias.co "!f() { : git checkout ; if ... } f" &&
635 test_completion "git co m" <<-\EOF
642 test_expect_failure 'complete with tilde expansion' '
643 git init tmp && cd tmp &&
644 test_when_finished "cd .. && rm -rf tmp" &&
648 test_completion "git add ~/tmp/" "~/tmp/file"