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 if test_have_prereq MINGW
134 test_expect_success
'setup for __gitdir tests' '
135 mkdir -p subdir/subsubdir &&
139 test_expect_success
'__gitdir - from command line (through $__git_dir)' '
140 echo "$ROOT/otherrepo/.git" >expected &&
142 __git_dir="$ROOT/otherrepo/.git" &&
145 test_cmp expected "$actual"
148 test_expect_success
'__gitdir - repo as argument' '
149 echo "otherrepo/.git" >expected &&
150 __gitdir "otherrepo" >"$actual" &&
151 test_cmp expected "$actual"
154 test_expect_success
'__gitdir - remote as argument' '
155 echo "remote" >expected &&
156 __gitdir "remote" >"$actual" &&
157 test_cmp expected "$actual"
160 test_expect_success
'__gitdir - .git directory in cwd' '
161 echo ".git" >expected &&
162 __gitdir >"$actual" &&
163 test_cmp expected "$actual"
166 test_expect_success
'__gitdir - .git directory in parent' '
167 echo "$ROOT/.git" >expected &&
169 cd subdir/subsubdir &&
172 test_cmp expected "$actual"
175 test_expect_success
'__gitdir - cwd is a .git directory' '
176 echo "." >expected &&
181 test_cmp expected "$actual"
184 test_expect_success
'__gitdir - parent is a .git directory' '
185 echo "$ROOT/.git" >expected &&
187 cd .git/refs/heads &&
190 test_cmp expected "$actual"
193 test_expect_success
'__gitdir - $GIT_DIR set while .git directory in cwd' '
194 echo "$ROOT/otherrepo/.git" >expected &&
196 GIT_DIR="$ROOT/otherrepo/.git" &&
200 test_cmp expected "$actual"
203 test_expect_success
'__gitdir - $GIT_DIR set while .git directory in parent' '
204 echo "$ROOT/otherrepo/.git" >expected &&
206 GIT_DIR="$ROOT/otherrepo/.git" &&
211 test_cmp expected "$actual"
214 test_expect_success
'__gitdir - non-existing $GIT_DIR' '
216 GIT_DIR="$ROOT/non-existing" &&
218 test_must_fail __gitdir
222 test_expect_success
'__gitdir - gitfile in cwd' '
223 echo "$ROOT/otherrepo/.git" >expected &&
224 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
225 test_when_finished "rm -f subdir/.git" &&
230 test_cmp expected "$actual"
233 test_expect_success
'__gitdir - gitfile in parent' '
234 echo "$ROOT/otherrepo/.git" >expected &&
235 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
236 test_when_finished "rm -f subdir/.git" &&
238 cd subdir/subsubdir &&
241 test_cmp expected "$actual"
244 test_expect_success SYMLINKS
'__gitdir - resulting path avoids symlinks' '
245 echo "$ROOT/otherrepo/.git" >expected &&
246 mkdir otherrepo/dir &&
247 test_when_finished "rm -rf otherrepo/dir" &&
248 ln -s otherrepo/dir link &&
249 test_when_finished "rm -f link" &&
254 test_cmp expected "$actual"
257 test_expect_success
'__gitdir - not a git repository' '
258 nongit test_must_fail __gitdir
261 test_expect_success
'__gitcomp - trailing space - options' '
262 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
263 --reset-author" <<-EOF
270 test_expect_success
'__gitcomp - trailing space - config keys' '
271 test_gitcomp "br" "branch. branch.autosetupmerge
272 branch.autosetuprebase browser." <<-\EOF
274 branch.autosetupmerge Z
275 branch.autosetuprebase Z
280 test_expect_success
'__gitcomp - option parameter' '
281 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
288 test_expect_success
'__gitcomp - prefix' '
289 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
290 "branch.maint." "me" <<-\EOF
292 branch.maint.mergeoptions Z
296 test_expect_success
'__gitcomp - suffix' '
297 test_gitcomp "branch.me" "master maint next pu" "branch." \
304 test_expect_success
'__gitcomp - doesnt fail because of invalid variable name' '
305 __gitcomp "$invalid_variable_name"
308 read -r -d "" refs
<<-\EOF
315 test_expect_success '__gitcomp_nl - trailing space
' '
316 test_gitcomp_nl
"m" "$refs" <<-EOF
322 test_expect_success '__gitcomp_nl
- prefix
' '
323 test_gitcomp_nl
"--fixup=m" "$refs" "--fixup=" "m" <<-EOF
329 test_expect_success '__gitcomp_nl
- suffix
' '
330 test_gitcomp_nl
"branch.ma" "$refs" "branch." "ma" "." <<-\EOF
336 test_expect_success '__gitcomp_nl - no suffix
' '
337 test_gitcomp_nl
"ma" "$refs" "" "ma" "" <<-\EOF
343 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name
' '
344 __gitcomp_nl
"$invalid_variable_name"
347 test_expect_success '__git_remotes
- list remotes from
$GIT_DIR/remotes and from config
file' '
348 cat >expect
<<-EOF &&
354 test_when_finished
"rm -rf .git/remotes" &&
355 mkdir
-p .git
/remotes
&&
356 >.git
/remotes
/remote_from_file_1
&&
357 >.git
/remotes
/remote_from_file_2
&&
358 test_when_finished
"git remote remove remote_in_config_1" &&
359 git remote add remote_in_config_1 git
://remote_1
&&
360 test_when_finished
"git remote remove remote_in_config_2" &&
361 git remote add remote_in_config_2 git
://remote_2
&&
362 __git_remotes
>actual
&&
363 test_cmp expect actual
366 test_expect_success '__git_get_config_variables
' '
367 cat >expect
<<-EOF &&
371 test_config interesting.name-1 good
&&
372 test_config interesting.name-2 good
&&
373 test_config subsection.interesting.name-3 bad
&&
374 __git_get_config_variables interesting
>actual
&&
375 test_cmp expect actual
378 test_expect_success '__git_pretty_aliases
' '
379 cat >expect
<<-EOF &&
383 test_config pretty.author
"%an %ae" &&
384 test_config pretty.
hash %H
&&
385 __git_pretty_aliases
>actual
&&
386 test_cmp expect actual
389 test_expect_success '__git_aliases
' '
390 cat >expect
<<-EOF &&
394 test_config
alias.ci commit
&&
395 test_config
alias.co checkout
&&
396 __git_aliases
>actual
&&
397 test_cmp expect actual
400 test_expect_success 'basic
' '
401 run_completion
"git " &&
403 grep -q "^add \$" out
&&
405 grep -q "^filter-branch \$" out
&&
407 ! grep -q "^ls-files \$" out
&&
409 run_completion
"git f" &&
410 ! grep -q -v "^f" out
413 test_expect_success 'double dash
"git" itself
' '
414 test_completion
"git --" <<-\EOF
427 --no-replace-objects Z
432 test_expect_success 'double dash "git checkout"' '
433 test_completion "git checkout --" <<-\
EOF
446 test_expect_success 'general options' '
447 test_completion "git --ver" "--version " &&
448 test_completion "git --hel" "--help " &&
449 test_completion "git --exe" <<-\EOF &&
453 test_completion "git --htm" "--html-path " &&
454 test_completion "git --pag" "--paginate " &&
455 test_completion "git --no-p" "--no-pager " &&
456 test_completion "git --git" "--git-dir=" &&
457 test_completion "git --wor" "--work-tree=" &&
458 test_completion "git --nam" "--namespace=" &&
459 test_completion "git --bar" "--bare " &&
460 test_completion "git --inf" "--info-path " &&
461 test_completion "git --no-r" "--no-replace-objects "
464 test_expect_success 'general options plus command' '
465 test_completion "git --version check" "checkout " &&
466 test_completion "git --paginate check" "checkout " &&
467 test_completion "git --git-dir=foo check" "checkout " &&
468 test_completion "git --bare check" "checkout " &&
469 test_completion "git --exec-path=foo check" "checkout " &&
470 test_completion "git --html-path check" "checkout " &&
471 test_completion "git --no-pager check" "checkout " &&
472 test_completion "git --work-tree=foo check" "checkout " &&
473 test_completion "git --namespace=foo check" "checkout " &&
474 test_completion "git --paginate check" "checkout " &&
475 test_completion "git --info-path check" "checkout " &&
476 test_completion "git --no-replace-objects check" "checkout "
479 test_expect_success 'git --help completion' '
480 test_completion "git --help ad" "add " &&
481 test_completion "git --help core" "core-tutorial "
484 test_expect_success 'setup for ref completion' '
485 echo content >file1 &&
487 git add file1 file2 &&
489 git branch mybranch &&
493 test_expect_success 'checkout completes ref names' '
494 test_completion "git checkout m" <<-\EOF
501 test_expect_success 'show completes all refs' '
502 test_completion "git show m" <<-\EOF
509 test_expect_success '<ref>: completes paths' '
510 test_completion "git show mytag:f" <<-\EOF
516 test_expect_success 'complete tree filename with spaces' '
517 echo content >"name with spaces" &&
518 git add "name with spaces" &&
519 git commit -m spaces &&
520 test_completion "git show HEAD:nam" <<-\EOF
525 test_expect_success 'complete tree filename with metacharacters' '
526 echo content >"name with \${meta}" &&
527 git add "name with \${meta}" &&
528 git commit -m meta &&
529 test_completion "git show HEAD:nam" <<-\EOF
535 test_expect_success 'send-email' '
536 test_completion "git send-email --cov" "--cover-letter " &&
537 test_completion "git send-email ma" "master "
540 test_expect_success 'complete files' '
541 git init tmp && cd tmp &&
542 test_when_finished "cd .. && rm -rf tmp" &&
544 echo "expected" > .gitignore &&
545 echo "out" >> .gitignore &&
547 git add .gitignore &&
548 test_completion "git commit " ".gitignore" &&
550 git commit -m ignore &&
553 test_completion "git add " "new" &&
556 git commit -a -m new &&
557 test_completion "git add " "" &&
559 git mv new modified &&
560 echo modify > modified &&
561 test_completion "git add " "modified" &&
565 : TODO .gitignore should not be here &&
566 test_completion "git rm " <<-\EOF &&
571 test_completion "git clean " "untracked" &&
573 : TODO .gitignore should not be here &&
574 test_completion "git mv " <<-\EOF &&
580 touch dir/file-in-dir &&
581 git add dir/file-in-dir &&
584 mkdir untracked-dir &&
586 : TODO .gitignore should not be here &&
587 test_completion "git mv modified " <<-\EOF &&
595 test_completion "git commit " "modified" &&
597 : TODO .gitignore should not be here &&
598 test_completion "git ls-files " <<-\EOF &&
605 test_completion "git add mom" "momified"
608 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
609 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
610 test_completion "git co m" <<-\EOF
617 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
618 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
619 test_completion "git co m" <<-\EOF
626 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
627 test_config alias.co "!f() { : git checkout ; if ... } f" &&
628 test_completion "git co m" <<-\EOF
635 test_expect_failure 'complete with tilde expansion' '
636 git init tmp && cd tmp &&
637 test_when_finished "cd .. && rm -rf tmp" &&
641 test_completion "git add ~/tmp/" "~/tmp/file"