t9902: protect test from stray build artifacts

When you have random build artifacts in your build directory, left behind by running "make" while on another branch, the "git help -a" command run by __git_list_all_commands in the completion script that is being tested does not have a way to know that they are not part of the subcommands this build will ship. Such extra subcommands may come from the user's $PATH. They will interfere with the tests that expect a certain prefix to uniquely expand to a known completion. Instrument the completion script and give it a way for us to tell what (subset of) subcommands we are going to ship. Also add a test to "git --help <prefix><TAB>" expansion. It needs to show not just commands but some selected documentation pages. Based on an idea by Jeff King. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 24, 2013 at 15:08 UTC 5047822347f8d7ad453ad0ea5cbff542569fb7a6
2 files changed +34 -2
contrib/completion/git-completion.bash
+10 -1
@@ -531,10 +531,19 @@ __git_complete_strategy ()
531 return 1
532 }
533
534 +__git_commands () {
535 + if test -n "${GIT_TESTING_COMMAND_COMPLETION:-}"
536 + then
537 + printf "%s" "${GIT_TESTING_COMMAND_COMPLETION}"
538 + else
539 + git help -a|egrep '^ [a-zA-Z0-9]'
540 + fi
541 +}
542 +
543 __git_list_all_commands ()
544 {
545 local i IFS=" "$'\n'
537 - for i in $(git help -a|egrep '^ [a-zA-Z0-9]')
546 + for i in $(__git_commands)
547 do
548 case $i in
549 *--*) : helper pattern;;
t/t9902-completion.sh
+24 -1
@@ -13,6 +13,25 @@ complete ()
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.
@@ -196,7 +215,6 @@ test_expect_success 'general options plus command' '
215 test_completion "git --paginate check" "checkout " &&
216 test_completion "git --git-dir=foo check" "checkout " &&
217 test_completion "git --bare check" "checkout " &&
199 - test_completion "git --help des" "describe " &&
218 test_completion "git --exec-path=foo check" "checkout " &&
219 test_completion "git --html-path check" "checkout " &&
220 test_completion "git --no-pager check" "checkout " &&
@@ -207,6 +225,11 @@ test_expect_success 'general options plus command' '
225 test_completion "git --no-replace-objects check" "checkout "
226 '
227
228 +test_expect_success 'git --help completion' '
229 + test_completion "git --help ad" "add " &&
230 + test_completion "git --help core" "core-tutorial "
231 +'
232 +
233 test_expect_success 'setup for ref completion' '
234 echo content >file1 &&
235 echo more >file2 &&