Skip to content

Commit 745ef06

Browse files
committed
Merge branch 'fc/completion-tests'
By Felipe Contreras (4) and others * fc/completion-tests: completion: fix completion after 'git --option <TAB>' completion: avoid trailing space for --exec-path completion: add missing general options completion: simplify by using $prev completion: simplify __gitcomp_1 tests: add tests for the __gitcomp() completion helper function tests: add initial bash completion tests
2 parents 8939cfb + 911d5da commit 745ef06

File tree

2 files changed

+256
-9
lines changed

2 files changed

+256
-9
lines changed

contrib/completion/git-completion.bash

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,16 @@ __git_ps1 ()
304304
fi
305305
}
306306

307-
# __gitcomp_1 requires 2 arguments
308307
__gitcomp_1 ()
309308
{
310-
local c IFS=' '$'\t'$'\n'
309+
local c IFS=$' \t\n'
311310
for c in $1; do
312-
case "$c$2" in
313-
--*=*) printf %s$'\n' "$c$2" ;;
314-
*.) printf %s$'\n' "$c$2" ;;
315-
*) printf %s$'\n' "$c$2 " ;;
311+
c="$c$2"
312+
case $c in
313+
--*=*|*.) ;;
314+
*) c="$c " ;;
316315
esac
316+
printf '%s\n' "$c"
317317
done
318318
}
319319

@@ -1658,7 +1658,7 @@ _git_notes ()
16581658
__gitcomp '--ref'
16591659
;;
16601660
,*)
1661-
case "${words[cword-1]}" in
1661+
case "$prev" in
16621662
--ref)
16631663
__gitcomp_nl "$(__git_refs)"
16641664
;;
@@ -1684,7 +1684,7 @@ _git_notes ()
16841684
prune,*)
16851685
;;
16861686
*)
1687-
case "${words[cword-1]}" in
1687+
case "$prev" in
16881688
-m|-F)
16891689
;;
16901690
*)
@@ -2623,8 +2623,9 @@ _git ()
26232623
case "$i" in
26242624
--git-dir=*) __git_dir="${i#--git-dir=}" ;;
26252625
--bare) __git_dir="." ;;
2626-
--version|-p|--paginate) ;;
26272626
--help) command="help"; break ;;
2627+
-c) c=$((++c)) ;;
2628+
-*) ;;
26282629
*) command="$i"; break ;;
26292630
esac
26302631
((c++))
@@ -2639,9 +2640,12 @@ _git ()
26392640
--bare
26402641
--version
26412642
--exec-path
2643+
--exec-path=
26422644
--html-path
2645+
--info-path
26432646
--work-tree=
26442647
--namespace=
2648+
--no-replace-objects
26452649
--help
26462650
"
26472651
;;

t/t9902-completion.sh

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2012 Felipe Contreras
4+
#
5+
6+
if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
7+
# we are in full-on bash mode
8+
true
9+
elif type bash >/dev/null 2>&1; then
10+
# execute in full-on bash mode
11+
unset POSIXLY_CORRECT
12+
exec bash "$0" "$@"
13+
else
14+
echo '1..0 #SKIP skipping bash completion tests; bash not available'
15+
exit 0
16+
fi
17+
18+
test_description='test bash completion'
19+
20+
. ./test-lib.sh
21+
22+
complete ()
23+
{
24+
# do nothing
25+
return 0
26+
}
27+
28+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
29+
30+
# We don't need this function to actually join words or do anything special.
31+
# Also, it's cleaner to avoid touching bash's internal completion variables.
32+
# So let's override it with a minimal version for testing purposes.
33+
_get_comp_words_by_ref ()
34+
{
35+
while [ $# -gt 0 ]; do
36+
case "$1" in
37+
cur)
38+
cur=${_words[_cword]}
39+
;;
40+
prev)
41+
prev=${_words[_cword-1]}
42+
;;
43+
words)
44+
words=("${_words[@]}")
45+
;;
46+
cword)
47+
cword=$_cword
48+
;;
49+
esac
50+
shift
51+
done
52+
}
53+
54+
print_comp ()
55+
{
56+
local IFS=$'\n'
57+
echo "${COMPREPLY[*]}" > out
58+
}
59+
60+
run_completion ()
61+
{
62+
local -a COMPREPLY _words
63+
local _cword
64+
_words=( $1 )
65+
(( _cword = ${#_words[@]} - 1 ))
66+
_git && print_comp
67+
}
68+
69+
test_completion ()
70+
{
71+
test $# -gt 1 && echo "$2" > expected
72+
run_completion "$@" &&
73+
test_cmp expected out
74+
}
75+
76+
newline=$'\n'
77+
78+
test_expect_success '__gitcomp - trailing space - options' '
79+
sed -e "s/Z$//" >expected <<-\EOF &&
80+
--reuse-message=Z
81+
--reedit-message=Z
82+
--reset-author Z
83+
EOF
84+
(
85+
local -a COMPREPLY &&
86+
cur="--re" &&
87+
__gitcomp "--dry-run --reuse-message= --reedit-message=
88+
--reset-author" &&
89+
IFS="$newline" &&
90+
echo "${COMPREPLY[*]}" > out
91+
) &&
92+
test_cmp expected out
93+
'
94+
95+
test_expect_success '__gitcomp - trailing space - config keys' '
96+
sed -e "s/Z$//" >expected <<-\EOF &&
97+
branch.Z
98+
branch.autosetupmerge Z
99+
branch.autosetuprebase Z
100+
browser.Z
101+
EOF
102+
(
103+
local -a COMPREPLY &&
104+
cur="br" &&
105+
__gitcomp "branch. branch.autosetupmerge
106+
branch.autosetuprebase browser." &&
107+
IFS="$newline" &&
108+
echo "${COMPREPLY[*]}" > out
109+
) &&
110+
test_cmp expected out
111+
'
112+
113+
test_expect_success '__gitcomp - option parameter' '
114+
sed -e "s/Z$//" >expected <<-\EOF &&
115+
recursive Z
116+
resolve Z
117+
EOF
118+
(
119+
local -a COMPREPLY &&
120+
cur="--strategy=re" &&
121+
__gitcomp "octopus ours recursive resolve subtree
122+
" "" "re" &&
123+
IFS="$newline" &&
124+
echo "${COMPREPLY[*]}" > out
125+
) &&
126+
test_cmp expected out
127+
'
128+
129+
test_expect_success '__gitcomp - prefix' '
130+
sed -e "s/Z$//" >expected <<-\EOF &&
131+
branch.maint.merge Z
132+
branch.maint.mergeoptions Z
133+
EOF
134+
(
135+
local -a COMPREPLY &&
136+
cur="branch.me" &&
137+
__gitcomp "remote merge mergeoptions rebase
138+
" "branch.maint." "me" &&
139+
IFS="$newline" &&
140+
echo "${COMPREPLY[*]}" > out
141+
) &&
142+
test_cmp expected out
143+
'
144+
145+
test_expect_success '__gitcomp - suffix' '
146+
sed -e "s/Z$//" >expected <<-\EOF &&
147+
branch.master.Z
148+
branch.maint.Z
149+
EOF
150+
(
151+
local -a COMPREPLY &&
152+
cur="branch.me" &&
153+
__gitcomp "master maint next pu
154+
" "branch." "ma" "." &&
155+
IFS="$newline" &&
156+
echo "${COMPREPLY[*]}" > out
157+
) &&
158+
test_cmp expected out
159+
'
160+
161+
test_expect_success 'basic' '
162+
run_completion "git \"\"" &&
163+
# built-in
164+
grep -q "^add \$" out &&
165+
# script
166+
grep -q "^filter-branch \$" out &&
167+
# plumbing
168+
! grep -q "^ls-files \$" out &&
169+
170+
run_completion "git f" &&
171+
! grep -q -v "^f" out
172+
'
173+
174+
test_expect_success 'double dash "git" itself' '
175+
sed -e "s/Z$//" >expected <<-\EOF &&
176+
--paginate Z
177+
--no-pager Z
178+
--git-dir=
179+
--bare Z
180+
--version Z
181+
--exec-path Z
182+
--exec-path=
183+
--html-path Z
184+
--info-path Z
185+
--work-tree=
186+
--namespace=
187+
--no-replace-objects Z
188+
--help Z
189+
EOF
190+
test_completion "git --"
191+
'
192+
193+
test_expect_success 'double dash "git checkout"' '
194+
sed -e "s/Z$//" >expected <<-\EOF &&
195+
--quiet Z
196+
--ours Z
197+
--theirs Z
198+
--track Z
199+
--no-track Z
200+
--merge Z
201+
--conflict=
202+
--orphan Z
203+
--patch Z
204+
EOF
205+
test_completion "git checkout --"
206+
'
207+
208+
test_expect_success 'general options' '
209+
test_completion "git --ver" "--version " &&
210+
test_completion "git --hel" "--help " &&
211+
sed -e "s/Z$//" >expected <<-\EOF &&
212+
--exec-path Z
213+
--exec-path=
214+
EOF
215+
test_completion "git --exe" &&
216+
test_completion "git --htm" "--html-path " &&
217+
test_completion "git --pag" "--paginate " &&
218+
test_completion "git --no-p" "--no-pager " &&
219+
test_completion "git --git" "--git-dir=" &&
220+
test_completion "git --wor" "--work-tree=" &&
221+
test_completion "git --nam" "--namespace=" &&
222+
test_completion "git --bar" "--bare " &&
223+
test_completion "git --inf" "--info-path " &&
224+
test_completion "git --no-r" "--no-replace-objects "
225+
'
226+
227+
test_expect_success 'general options plus command' '
228+
test_completion "git --version check" "checkout " &&
229+
test_completion "git --paginate check" "checkout " &&
230+
test_completion "git --git-dir=foo check" "checkout " &&
231+
test_completion "git --bare check" "checkout " &&
232+
test_completion "git --help des" "describe " &&
233+
test_completion "git --exec-path=foo check" "checkout " &&
234+
test_completion "git --html-path check" "checkout " &&
235+
test_completion "git --no-pager check" "checkout " &&
236+
test_completion "git --work-tree=foo check" "checkout " &&
237+
test_completion "git --namespace=foo check" "checkout " &&
238+
test_completion "git --paginate check" "checkout " &&
239+
test_completion "git --info-path check" "checkout " &&
240+
test_completion "git --no-replace-objects check" "checkout "
241+
'
242+
243+
test_done

0 commit comments

Comments
 (0)