Skip to content

Commit 5047822

Browse files
committed
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 <[email protected]>
1 parent b344bb1 commit 5047822

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

contrib/completion/git-completion.bash

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,19 @@ __git_complete_strategy ()
531531
return 1
532532
}
533533

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+
534543
__git_list_all_commands ()
535544
{
536545
local i IFS=" "$'\n'
537-
for i in $(git help -a|egrep '^ [a-zA-Z0-9]')
546+
for i in $(__git_commands)
538547
do
539548
case $i in
540549
*--*) : helper pattern;;

t/t9902-completion.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ complete ()
1313
return 0
1414
}
1515

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+
1635
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
1736

1837
# 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' '
196215
test_completion "git --paginate check" "checkout " &&
197216
test_completion "git --git-dir=foo check" "checkout " &&
198217
test_completion "git --bare check" "checkout " &&
199-
test_completion "git --help des" "describe " &&
200218
test_completion "git --exec-path=foo check" "checkout " &&
201219
test_completion "git --html-path check" "checkout " &&
202220
test_completion "git --no-pager check" "checkout " &&
@@ -207,6 +225,11 @@ test_expect_success 'general options plus command' '
207225
test_completion "git --no-replace-objects check" "checkout "
208226
'
209227

228+
test_expect_success 'git --help completion' '
229+
test_completion "git --help ad" "add " &&
230+
test_completion "git --help core" "core-tutorial "
231+
'
232+
210233
test_expect_success 'setup for ref completion' '
211234
echo content >file1 &&
212235
echo more >file2 &&

0 commit comments

Comments
 (0)