userdiff: fix HTML hunk header regexp
[git/debian.git] / t / t9902-completion.sh
bloba34e55f874ca06da4cae6ab5d46bc3263c58d521
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' '
260 nongit test_must_fail __gitdir
263 test_expect_success '__gitcomp - trailing space - options' '
264 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
265 --reset-author" <<-EOF
266 --reuse-message=Z
267 --reedit-message=Z
268 --reset-author Z
272 test_expect_success '__gitcomp - trailing space - config keys' '
273 test_gitcomp "br" "branch. branch.autosetupmerge
274 branch.autosetuprebase browser." <<-\EOF
275 branch.Z
276 branch.autosetupmerge Z
277 branch.autosetuprebase Z
278 browser.Z
282 test_expect_success '__gitcomp - option parameter' '
283 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
284 "" "re" <<-\EOF
285 recursive Z
286 resolve Z
290 test_expect_success '__gitcomp - prefix' '
291 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
292 "branch.maint." "me" <<-\EOF
293 branch.maint.merge Z
294 branch.maint.mergeoptions Z
298 test_expect_success '__gitcomp - suffix' '
299 test_gitcomp "branch.me" "master maint next pu" "branch." \
300 "ma" "." <<-\EOF
301 branch.master.Z
302 branch.maint.Z
306 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
307 __gitcomp "$invalid_variable_name"
310 read -r -d "" refs <<-\EOF
311 maint
312 master
313 next
317 test_expect_success '__gitcomp_nl - trailing space' '
318 test_gitcomp_nl "m" "$refs" <<-EOF
319 maint Z
320 master Z
324 test_expect_success '__gitcomp_nl - prefix' '
325 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
326 --fixup=maint Z
327 --fixup=master Z
331 test_expect_success '__gitcomp_nl - suffix' '
332 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
333 branch.maint.Z
334 branch.master.Z
338 test_expect_success '__gitcomp_nl - no suffix' '
339 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
340 maintZ
341 masterZ
345 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
346 __gitcomp_nl "$invalid_variable_name"
349 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
350 cat >expect <<-EOF &&
351 remote_from_file_1
352 remote_from_file_2
353 remote_in_config_1
354 remote_in_config_2
356 test_when_finished "rm -rf .git/remotes" &&
357 mkdir -p .git/remotes &&
358 >.git/remotes/remote_from_file_1 &&
359 >.git/remotes/remote_from_file_2 &&
360 test_when_finished "git remote remove remote_in_config_1" &&
361 git remote add remote_in_config_1 git://remote_1 &&
362 test_when_finished "git remote remove remote_in_config_2" &&
363 git remote add remote_in_config_2 git://remote_2 &&
364 __git_remotes >actual &&
365 test_cmp expect actual
368 test_expect_success '__git_get_config_variables' '
369 cat >expect <<-EOF &&
370 name-1
371 name-2
373 test_config interesting.name-1 good &&
374 test_config interesting.name-2 good &&
375 test_config subsection.interesting.name-3 bad &&
376 __git_get_config_variables interesting >actual &&
377 test_cmp expect actual
380 test_expect_success '__git_pretty_aliases' '
381 cat >expect <<-EOF &&
382 author
383 hash
385 test_config pretty.author "%an %ae" &&
386 test_config pretty.hash %H &&
387 __git_pretty_aliases >actual &&
388 test_cmp expect actual
391 test_expect_success '__git_aliases' '
392 cat >expect <<-EOF &&
396 test_config alias.ci commit &&
397 test_config alias.co checkout &&
398 __git_aliases >actual &&
399 test_cmp expect actual
402 test_expect_success 'basic' '
403 run_completion "git " &&
404 # built-in
405 grep -q "^add \$" out &&
406 # script
407 grep -q "^filter-branch \$" out &&
408 # plumbing
409 ! grep -q "^ls-files \$" out &&
411 run_completion "git f" &&
412 ! grep -q -v "^f" out
415 test_expect_success 'double dash "git" itself' '
416 test_completion "git --" <<-\EOF
417 --paginate Z
418 --no-pager Z
419 --git-dir=
420 --bare Z
421 --version Z
422 --exec-path Z
423 --exec-path=
424 --html-path Z
425 --man-path Z
426 --info-path Z
427 --work-tree=
428 --namespace=
429 --no-replace-objects Z
430 --help Z
434 test_expect_success 'double dash "git checkout"' '
435 test_completion "git checkout --" <<-\EOF
436 --quiet Z
437 --ours Z
438 --theirs Z
439 --track Z
440 --no-track Z
441 --merge Z
442 --conflict=
443 --orphan Z
444 --patch Z
448 test_expect_success 'general options' '
449 test_completion "git --ver" "--version " &&
450 test_completion "git --hel" "--help " &&
451 test_completion "git --exe" <<-\EOF &&
452 --exec-path Z
453 --exec-path=
455 test_completion "git --htm" "--html-path " &&
456 test_completion "git --pag" "--paginate " &&
457 test_completion "git --no-p" "--no-pager " &&
458 test_completion "git --git" "--git-dir=" &&
459 test_completion "git --wor" "--work-tree=" &&
460 test_completion "git --nam" "--namespace=" &&
461 test_completion "git --bar" "--bare " &&
462 test_completion "git --inf" "--info-path " &&
463 test_completion "git --no-r" "--no-replace-objects "
466 test_expect_success 'general options plus command' '
467 test_completion "git --version check" "checkout " &&
468 test_completion "git --paginate check" "checkout " &&
469 test_completion "git --git-dir=foo check" "checkout " &&
470 test_completion "git --bare check" "checkout " &&
471 test_completion "git --exec-path=foo check" "checkout " &&
472 test_completion "git --html-path check" "checkout " &&
473 test_completion "git --no-pager check" "checkout " &&
474 test_completion "git --work-tree=foo check" "checkout " &&
475 test_completion "git --namespace=foo check" "checkout " &&
476 test_completion "git --paginate check" "checkout " &&
477 test_completion "git --info-path check" "checkout " &&
478 test_completion "git --no-replace-objects check" "checkout "
481 test_expect_success 'git --help completion' '
482 test_completion "git --help ad" "add " &&
483 test_completion "git --help core" "core-tutorial "
486 test_expect_success 'setup for ref completion' '
487 echo content >file1 &&
488 echo more >file2 &&
489 git add . &&
490 git commit -m one &&
491 git branch mybranch &&
492 git tag mytag
495 test_expect_success 'checkout completes ref names' '
496 test_completion "git checkout m" <<-\EOF
497 master Z
498 mybranch Z
499 mytag Z
503 test_expect_success 'show completes all refs' '
504 test_completion "git show m" <<-\EOF
505 master Z
506 mybranch Z
507 mytag Z
511 test_expect_success '<ref>: completes paths' '
512 test_completion "git show mytag:f" <<-\EOF
513 file1 Z
514 file2 Z
518 test_expect_success 'complete tree filename with spaces' '
519 echo content >"name with spaces" &&
520 git add . &&
521 git commit -m spaces &&
522 test_completion "git show HEAD:nam" <<-\EOF
523 name with spaces Z
527 test_expect_success 'complete tree filename with metacharacters' '
528 echo content >"name with \${meta}" &&
529 git add . &&
530 git commit -m meta &&
531 test_completion "git show HEAD:nam" <<-\EOF
532 name with ${meta} Z
533 name with spaces Z
537 test_expect_success 'send-email' '
538 test_completion "git send-email --cov" "--cover-letter " &&
539 test_completion "git send-email ma" "master "
542 test_expect_success 'complete files' '
543 git init tmp && cd tmp &&
544 test_when_finished "cd .. && rm -rf tmp" &&
546 echo "expected" > .gitignore &&
547 echo "out" >> .gitignore &&
549 git add .gitignore &&
550 test_completion "git commit " ".gitignore" &&
552 git commit -m ignore &&
554 touch new &&
555 test_completion "git add " "new" &&
557 git add new &&
558 git commit -a -m new &&
559 test_completion "git add " "" &&
561 git mv new modified &&
562 echo modify > modified &&
563 test_completion "git add " "modified" &&
565 touch untracked &&
567 : TODO .gitignore should not be here &&
568 test_completion "git rm " <<-\EOF &&
569 .gitignore
570 modified
573 test_completion "git clean " "untracked" &&
575 : TODO .gitignore should not be here &&
576 test_completion "git mv " <<-\EOF &&
577 .gitignore
578 modified
581 mkdir dir &&
582 touch dir/file-in-dir &&
583 git add dir/file-in-dir &&
584 git commit -m dir &&
586 mkdir untracked-dir &&
588 : TODO .gitignore should not be here &&
589 test_completion "git mv modified " <<-\EOF &&
590 .gitignore
592 modified
593 untracked
594 untracked-dir
597 test_completion "git commit " "modified" &&
599 : TODO .gitignore should not be here &&
600 test_completion "git ls-files " <<-\EOF &&
601 .gitignore
603 modified
606 touch momified &&
607 test_completion "git add mom" "momified"
610 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
611 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
612 test_completion "git co m" <<-\EOF
613 master Z
614 mybranch Z
615 mytag Z
619 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
620 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
621 test_completion "git co m" <<-\EOF
622 master Z
623 mybranch Z
624 mytag Z
628 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
629 test_config alias.co "!f() { : git checkout ; if ... } f" &&
630 test_completion "git co m" <<-\EOF
631 master Z
632 mybranch Z
633 mytag Z
637 test_expect_failure 'complete with tilde expansion' '
638 git init tmp && cd tmp &&
639 test_when_finished "cd .. && rm -rf tmp" &&
641 touch ~/tmp/file &&
643 test_completion "git add ~/tmp/" "~/tmp/file"
646 test_done