# [PATCH v2] tests: add initial bash completion tests

21 messages from 2012-04-11 to 2012-04-17. Participants: Felipe Contreras, Junio C Hamano, SZEDER Gábor, Thomas Rast.
Thread: https://gitlist.dev/t/30219

## Felipe Contreras, 2012-04-11 21:57

Subject: [PATCH v2] tests: add initial bash completion tests
Message-ID: <1334181423-4391-1-git-send-email-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/1334181423-4391-1-git-send-email-felipe.contreras%40gmail.com

```
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---

Since v1:

 * Check if we are running bash in posix mode
 * Don't check for all git porcelain commands

 t/t9902-completion.sh |  115 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100755 t/t9902-completion.sh

diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
new file mode 100755
index 0000000..cbda6b5
--- /dev/null
+++ b/t/t9902-completion.sh
@@ -0,0 +1,115 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Felipe Contreras
+#
+
+if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
+	# we are in full-on bash mode
+	true
+elif type bash >/dev/null 2>&1; then
+	# execute in full-on bash mode
+	unset POSIXLY_CORRECT
+	exec bash "$0" "$@"
+else
+	echo '1..0 #SKIP skipping bash completion tests; bash not available'
+	exit 0
+fi
+
+test_description='test bash completion'
+
+. ./test-lib.sh
+
+complete ()
+{
+	# do nothing
+	return 0
+}
+
+. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
+
+_get_comp_words_by_ref ()
+{
+	while [ $# -gt 0 ]; do
+		case "$1" in
+		cur)
+			cur=${_words[_cword]}
+			;;
+		prev)
+			prev=${_words[_cword-1]}
+			;;
+		words)
+			words=("${_words[@]}")
+			;;
+		cword)
+			cword=$_cword
+			;;
+		esac
+		shift
+	done
+}
+
+print_comp ()
+{
+	local IFS=$'\n'
+	echo "${COMPREPLY[*]}" > out
+}
+
+run_completion ()
+{
+	local -a COMPREPLY _words
+	local _cword
+	_words=( $1 )
+	(( _cword = ${#_words[@]} - 1 ))
+	_git && print_comp
+}
+
+test_completion ()
+{
+	test $# -gt 1 && echo "$2" > expected
+	run_completion "$@" &&
+	test_cmp expected out
+}
+
+test_expect_success 'basic' '
+	run_completion "git \"\"" &&
+	# built-in
+	grep -q "^add \$" out &&
+	# script
+	grep -q "^filter-branch \$" out &&
+	# plumbing
+	! grep -q "^ls-files \$" out
+
+	run_completion "git f" &&
+	! grep -q -v "^f" out
+'
+
+test_expect_success 'double dash' '
+	cat >expected <<-\EOF &&
+	--paginate 
+	--no-pager 
+	--git-dir=
+	--bare 
+	--version 
+	--exec-path 
+	--html-path 
+	--work-tree=
+	--namespace=
+	--help 
+	EOF
+	test_completion "git --"
+
+	cat >expected <<-\EOF &&
+	--quiet 
+	--ours 
+	--theirs 
+	--track 
+	--no-track 
+	--merge 
+	--conflict=
+	--orphan 
+	--patch 
+	EOF
+	test_completion "git checkout --"
+'
+
+test_done
-- 
1.7.10.1.g1f19b8.dirty

```

## Junio C Hamano, 2012-04-11 23:48

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <7v8vi1j0d1.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8vi1j0d1.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1334181423-4391-1-git-send-email-felipe.contreras@gmail.com>

```
The trailing whitespaces are not visible when reviewing tests, so I'll
squash this in, but otherwise looks good.

Thanks.

 t/t9902-completion.sh |   34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index cbda6b5..51227ac 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -84,30 +84,30 @@ test_expect_success 'basic' '
 '
 
 test_expect_success 'double dash' '
-	cat >expected <<-\EOF &&
-	--paginate 
-	--no-pager 
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	--paginate Z
+	--no-pager Z
 	--git-dir=
-	--bare 
-	--version 
-	--exec-path 
-	--html-path 
+	--bare Z
+	--version Z
+	--exec-path Z
+	--html-path Z
 	--work-tree=
 	--namespace=
-	--help 
+	--help Z
 	EOF
 	test_completion "git --"
 
-	cat >expected <<-\EOF &&
-	--quiet 
-	--ours 
-	--theirs 
-	--track 
-	--no-track 
-	--merge 
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	--quiet Z
+	--ours Z
+	--theirs Z
+	--track Z
+	--no-track Z
+	--merge Z
 	--conflict=
-	--orphan 
-	--patch 
+	--orphan Z
+	--patch Z
 	EOF
 	test_completion "git checkout --"
 '

```

## Felipe Contreras, 2012-04-12 16:15

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s2Ug91_sQ-XR3ck2k+x+eQRt9PNRRBOUnkrnNRhY6fEpw@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s2Ug91_sQ-XR3ck2k%2Bx%2BeQRt9PNRRBOUnkrnNRhY6fEpw%40mail.gmail.com
In-Reply-To: <1334181423-4391-1-git-send-email-felipe.contreras@gmail.com>

```
On Thu, Apr 12, 2012 at 12:57 AM, Felipe Contreras
<felipe.contreras@gmail.com> wrote:

> +test_expect_success 'double dash' '
> +       cat >expected <<-\EOF &&
> +       --paginate
> +       --no-pager
> +       --git-dir=
> +       --bare
> +       --version
> +       --exec-path
> +       --html-path
> +       --work-tree=
> +       --namespace=
> +       --help
> +       EOF
> +       test_completion "git --"

There's a mistake here ^.

--- b/t/t9902-completion.sh
+++ a/t/t9902-completion.sh
@@ -96,7 +96,7 @@ test_expect_success 'double dash' '
        --namespace=
        --help Z
        EOF
-       test_completion "git --"
+       test_completion "git --" &&

        sed -e "s/Z$//" >expected <<-\EOF &&
        --quiet Z

-- 
Felipe Contreras

```

## Junio C Hamano, 2012-04-12 17:43

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <7vmx6g6e2i.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vmx6g6e2i.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CAMP44s2Ug91_sQ-XR3ck2k+x+eQRt9PNRRBOUnkrnNRhY6fEpw@mail.gmail.com>

```
Felipe Contreras <felipe.contreras@gmail.com> writes:

> On Thu, Apr 12, 2012 at 12:57 AM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
>
>> +test_expect_success 'double dash' '
>> +       cat >expected <<-\EOF &&
>> +       --paginate
>> +       --no-pager
>> +       --git-dir=
>> +       --bare
>> +       --version
>> +       --exec-path
>> +       --html-path
>> +       --work-tree=
>> +       --namespace=
>> +       --help
>> +       EOF
>> +       test_completion "git --"
>
> There's a mistake here ^.

Yeah, good eyes!  ... ah, wait, it is your bug ;-)

Thanks.

I wonder if it may make more sense to have this as two separate tests,
though...

> --- b/t/t9902-completion.sh
> +++ a/t/t9902-completion.sh
> @@ -96,7 +96,7 @@ test_expect_success 'double dash' '
>         --namespace=
>         --help Z
>         EOF
> -       test_completion "git --"
> +       test_completion "git --" &&
>
>         sed -e "s/Z$//" >expected <<-\EOF &&
>         --quiet Z

```

## Felipe Contreras, 2012-04-12 23:18

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s37znvnw+n3NsFj+ZVuxwPhrordJA0ko0LV9udPS+i3aQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s37znvnw%2Bn3NsFj%2BZVuxwPhrordJA0ko0LV9udPS%2Bi3aQ%40mail.gmail.com
In-Reply-To: <7vmx6g6e2i.fsf@alter.siamese.dyndns.org>

```
On Thu, Apr 12, 2012 at 8:43 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Felipe Contreras <felipe.contreras@gmail.com> writes:
>
>> On Thu, Apr 12, 2012 at 12:57 AM, Felipe Contreras
>> <felipe.contreras@gmail.com> wrote:
>>
>>> +test_expect_success 'double dash' '
>>> +       cat >expected <<-\EOF &&
>>> +       --paginate
>>> +       --no-pager
>>> +       --git-dir=
>>> +       --bare
>>> +       --version
>>> +       --exec-path
>>> +       --html-path
>>> +       --work-tree=
>>> +       --namespace=
>>> +       --help
>>> +       EOF
>>> +       test_completion "git --"
>>
>> There's a mistake here ^.
>
> Yeah, good eyes!  ... ah, wait, it is your bug ;-)

Not my eyes, further tests =/

Do I need to resend the patch?

-- 
Felipe Contreras

```

## Junio C Hamano, 2012-04-12 23:26

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <7vzkag3528.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vzkag3528.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CAMP44s37znvnw+n3NsFj+ZVuxwPhrordJA0ko0LV9udPS+i3aQ@mail.gmail.com>

```
Felipe Contreras <felipe.contreras@gmail.com> writes:

> On Thu, Apr 12, 2012 at 8:43 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Felipe Contreras <felipe.contreras@gmail.com> writes:
>>
>>> On Thu, Apr 12, 2012 at 12:57 AM, Felipe Contreras
>>> <felipe.contreras@gmail.com> wrote:
>>>
>>>> +test_expect_success 'double dash' '
>>>> +       cat >expected <<-\EOF &&
>>>> +       --paginate
>>>> +       --no-pager
>>>> +       --git-dir=
>>>> +       --bare
>>>> +       --version
>>>> +       --exec-path
>>>> +       --html-path
>>>> +       --work-tree=
>>>> +       --namespace=
>>>> +       --help
>>>> +       EOF
>>>> +       test_completion "git --"
>>>
>>> There's a mistake here ^.
>>
>> Yeah, good eyes!  ... ah, wait, it is your bug ;-)
>
> Not my eyes, further tests =/
>
> Do I need to resend the patch?

Not necessarily.  If you can eyeball what is queued on 'pu' and gave me an
Ack (or send a replacement with "You stupid, you amended it wrong!") that
should be sufficient.

Thanks.

```

## SZEDER Gábor, 2012-04-13 09:12

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <20120413091236.GC2164@goldbirke>
URL: https://gitlist.dev/e/20120413091236.GC2164%40goldbirke
In-Reply-To: <1334181423-4391-1-git-send-email-felipe.contreras@gmail.com>

```
Hi,


On Thu, Apr 12, 2012 at 12:57:03AM +0300, Felipe Contreras wrote:
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
> 
> Since v1:
> 
>  * Check if we are running bash in posix mode
>  * Don't check for all git porcelain commands
> 
>  t/t9902-completion.sh |  115 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 115 insertions(+)
>  create mode 100755 t/t9902-completion.sh
> 
> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> new file mode 100755
> index 0000000..cbda6b5
> --- /dev/null
> +++ b/t/t9902-completion.sh
> @@ -0,0 +1,115 @@
> +#!/bin/sh
> +#
> +# Copyright (c) 2012 Felipe Contreras
> +#
> +
> +if test -n "$BASH" && test -z "$POSIXLY_CORRECT"; then
> +	# we are in full-on bash mode
> +	true
> +elif type bash >/dev/null 2>&1; then
> +	# execute in full-on bash mode
> +	unset POSIXLY_CORRECT
> +	exec bash "$0" "$@"
> +else
> +	echo '1..0 #SKIP skipping bash completion tests; bash not available'
> +	exit 0
> +fi
> +
> +test_description='test bash completion'
> +
> +. ./test-lib.sh
> +
> +complete ()
> +{
> +	# do nothing
> +	return 0
> +}
> +
> +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
> +
> +_get_comp_words_by_ref ()
> +{
> +	while [ $# -gt 0 ]; do
> +		case "$1" in
> +		cur)
> +			cur=${_words[_cword]}
> +			;;
> +		prev)
> +			prev=${_words[_cword-1]}
> +			;;
> +		words)
> +			words=("${_words[@]}")
> +			;;
> +		cword)
> +			cword=$_cword
> +			;;
> +		esac
> +		shift
> +	done
> +}

Git's completion script already implements this function.  Why
override it here?

> +print_comp ()
> +{
> +	local IFS=$'\n'
> +	echo "${COMPREPLY[*]}" > out
> +}
> +
> +run_completion ()
> +{
> +	local -a COMPREPLY _words
> +	local _cword
> +	_words=( $1 )
> +	(( _cword = ${#_words[@]} - 1 ))
> +	_git && print_comp
> +}
> +
> +test_completion ()
> +{
> +	test $# -gt 1 && echo "$2" > expected
> +	run_completion "$@" &&
> +	test_cmp expected out
> +}
> +
> +test_expect_success 'basic' '
> +	run_completion "git \"\"" &&
> +	# built-in
> +	grep -q "^add \$" out &&
> +	# script
> +	grep -q "^filter-branch \$" out &&
> +	# plumbing
> +	! grep -q "^ls-files \$" out

The && is missing here at the end of the line.

> +	run_completion "git f" &&
> +	! grep -q -v "^f" out

grep is not a git command, so I'm not sure, but shouldn't these use
'test_must_fail grep' instead of '! grep'?


Anyway, thanks for pushing this forward.  I have a bunch of tests for
my __git_ps1() optimizations, but, being a bash function, I could
never figure out how to integrate it with the test framework.


Best,
Gábor

```

## SZEDER Gábor, 2012-04-13 09:45

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <20120413094500.GD2164@goldbirke>
URL: https://gitlist.dev/e/20120413094500.GD2164%40goldbirke
In-Reply-To: <20120413091236.GC2164@goldbirke>

```
On Fri, Apr 13, 2012 at 11:12:36AM +0200, SZEDER Gábor wrote:
> On Thu, Apr 12, 2012 at 12:57:03AM +0300, Felipe Contreras wrote:
> > +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
> > +
> > +_get_comp_words_by_ref ()
> > +{
> > +	while [ $# -gt 0 ]; do
> > +		case "$1" in
> > +		cur)
> > +			cur=${_words[_cword]}
> > +			;;
> > +		prev)
> > +			prev=${_words[_cword-1]}
> > +			;;
> > +		words)
> > +			words=("${_words[@]}")
> > +			;;
> > +		cword)
> > +			cword=$_cword
> > +			;;
> > +		esac
> > +		shift
> > +	done
> > +}
> 
> Git's completion script already implements this function.  Why
> override it here?

Ah, ok, I think I got it.

Of course, the words on the command line must be specified somehow to
test completion functions.  But the two implementations of
_get_comp_words_by_ref() for bash and zsh in the completion script
take the words on the command line from different variables, so we
need a common implementation to test completion functions both on bash
and zsh.  Hence the _get_comp_words_by_ref() above, which takes the
words on the command line and their count from $_words and $_cword,
respectively, and run_completion() below, which fills those variables
with its arguments.


> > +print_comp ()
> > +{
> > +	local IFS=$'\n'
> > +	echo "${COMPREPLY[*]}" > out
> > +}
> > +
> > +run_completion ()
> > +{
> > +	local -a COMPREPLY _words
> > +	local _cword
> > +	_words=( $1 )
> > +	(( _cword = ${#_words[@]} - 1 ))
> > +	_git && print_comp
> > +}

```

## Felipe Contreras, 2012-04-13 10:34

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s0_WQNpkVi0yMfaDgzBaUWOhRiS-2qxLtCHFzx71yKrSw@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s0_WQNpkVi0yMfaDgzBaUWOhRiS-2qxLtCHFzx71yKrSw%40mail.gmail.com
In-Reply-To: <20120413091236.GC2164@goldbirke>

```
2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:

>> +test_expect_success 'basic' '
>> +     run_completion "git \"\"" &&
>> +     # built-in
>> +     grep -q "^add \$" out &&
>> +     # script
>> +     grep -q "^filter-branch \$" out &&
>> +     # plumbing
>> +     ! grep -q "^ls-files \$" out
>
> The && is missing here at the end of the line.

Right.

>> +     run_completion "git f" &&
>> +     ! grep -q -v "^f" out
>
> grep is not a git command, so I'm not sure, but shouldn't these use
> 'test_must_fail grep' instead of '! grep'?

I'm not sure. Junio has already queued this, maybe you should send a
patch on top of that.

Cheers.

-- 
Felipe Contreras

```

## Felipe Contreras, 2012-04-13 10:48

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s3g8acV4fjaSvnUo_jnhj40-TWR0az6zOwRNfv9_Qa23g@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s3g8acV4fjaSvnUo_jnhj40-TWR0az6zOwRNfv9_Qa23g%40mail.gmail.com
In-Reply-To: <20120413094500.GD2164@goldbirke>

```
2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:
> On Fri, Apr 13, 2012 at 11:12:36AM +0200, SZEDER Gábor wrote:
>> On Thu, Apr 12, 2012 at 12:57:03AM +0300, Felipe Contreras wrote:
>> > +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
>> > +
>> > +_get_comp_words_by_ref ()
>> > +{
>> > +   while [ $# -gt 0 ]; do
>> > +           case "$1" in
>> > +           cur)
>> > +                   cur=${_words[_cword]}
>> > +                   ;;
>> > +           prev)
>> > +                   prev=${_words[_cword-1]}
>> > +                   ;;
>> > +           words)
>> > +                   words=("${_words[@]}")
>> > +                   ;;
>> > +           cword)
>> > +                   cword=$_cword
>> > +                   ;;
>> > +           esac
>> > +           shift
>> > +   done
>> > +}
>>
>> Git's completion script already implements this function.  Why
>> override it here?
>
> Ah, ok, I think I got it.
>
> Of course, the words on the command line must be specified somehow to
> test completion functions.  But the two implementations of
> _get_comp_words_by_ref() for bash and zsh in the completion script
> take the words on the command line from different variables, so we
> need a common implementation to test completion functions both on bash
> and zsh.  Hence the _get_comp_words_by_ref() above, which takes the
> words on the command line and their count from $_words and $_cword,
> respectively, and run_completion() below, which fills those variables
> with its arguments.

Well, yeah, that's one reason, but also I don't see the point in
trying to fill the internal bash completion variables, maybe there
would be some conflicts? Plus, the bash version of
_get_comp_words_by_ref is rather complicated, so I decided to start
with something simple that I could understand and see exactly what's
going on. And for zsh I would definitely prefer to override
_get_comp_words_by_ref than to mess with the internal variables,
although I haven't found a way to test completion for zsh.

Cheers.

-- 
Felipe Contreras

```

## SZEDER Gábor, 2012-04-13 10:52

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <20120413105238.GE2164@goldbirke>
URL: https://gitlist.dev/e/20120413105238.GE2164%40goldbirke
In-Reply-To: <CAMP44s0_WQNpkVi0yMfaDgzBaUWOhRiS-2qxLtCHFzx71yKrSw@mail.gmail.com>

```
On Fri, Apr 13, 2012 at 01:34:46PM +0300, Felipe Contreras wrote:
> >> +     run_completion "git f" &&
> >> +     ! grep -q -v "^f" out
> >
> > grep is not a git command, so I'm not sure, but shouldn't these use
> > 'test_must_fail grep' instead of '! grep'?
> 
> I'm not sure. Junio has already queued this, maybe you should send a
> patch on top of that.

It seems that both are used in the test suite, but '! grep' is more
common, so perhaps it's good as it is.

$ git grep '! grep' -- t |wc -l
136
$ git grep 'test_must_fail grep' -- t |wc -l
17

```

## SZEDER Gábor, 2012-04-13 11:14

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <20120413111437.GF2164@goldbirke>
URL: https://gitlist.dev/e/20120413111437.GF2164%40goldbirke
In-Reply-To: <CAMP44s3g8acV4fjaSvnUo_jnhj40-TWR0az6zOwRNfv9_Qa23g@mail.gmail.com>

```
On Fri, Apr 13, 2012 at 01:48:51PM +0300, Felipe Contreras wrote:
> 2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:
> > On Fri, Apr 13, 2012 at 11:12:36AM +0200, SZEDER Gábor wrote:
> >> On Thu, Apr 12, 2012 at 12:57:03AM +0300, Felipe Contreras wrote:
> >> > +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
> >> > +
> >> > +_get_comp_words_by_ref ()
> >> > +{
> >> > +   while [ $# -gt 0 ]; do
> >> > +           case "$1" in
> >> > +           cur)
> >> > +                   cur=${_words[_cword]}
> >> > +                   ;;
> >> > +           prev)
> >> > +                   prev=${_words[_cword-1]}
> >> > +                   ;;
> >> > +           words)
> >> > +                   words=("${_words[@]}")
> >> > +                   ;;
> >> > +           cword)
> >> > +                   cword=$_cword
> >> > +                   ;;
> >> > +           esac
> >> > +           shift
> >> > +   done
> >> > +}
> >>
> >> Git's completion script already implements this function.  Why
> >> override it here?
> >
> > Ah, ok, I think I got it.
> >
> > Of course, the words on the command line must be specified somehow to
> > test completion functions.  But the two implementations of
> > _get_comp_words_by_ref() for bash and zsh in the completion script
> > take the words on the command line from different variables, so we
> > need a common implementation to test completion functions both on bash
> > and zsh.  Hence the _get_comp_words_by_ref() above, which takes the
> > words on the command line and their count from $_words and $_cword,
> > respectively, and run_completion() below, which fills those variables
> > with its arguments.
> 
> Well, yeah, that's one reason, but also I don't see the point in
> trying to fill the internal bash completion variables, maybe there
> would be some conflicts? Plus, the bash version of
> _get_comp_words_by_ref is rather complicated, so I decided to start
> with something simple that I could understand and see exactly what's
> going on. And for zsh I would definitely prefer to override
> _get_comp_words_by_ref than to mess with the internal variables,
> although I haven't found a way to test completion for zsh.

The tests are run in a non-interactive shell, which by default doesn't
load bash completion with its complicated _get_comp_words_by_ref().
So these tests use _get_comp_words_by_ref() from git's completion
script.


Anyway, out of curiosity I quickly tried this on top of b8574ba7 (i.e.
your patch from today's pu):

diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 3bbec79b..6c1ea956 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -27,27 +27,6 @@ complete ()
 
 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
 
-_get_comp_words_by_ref ()
-{
-	while [ $# -gt 0 ]; do
-		case "$1" in
-		cur)
-			cur=${_words[_cword]}
-			;;
-		prev)
-			prev=${_words[_cword-1]}
-			;;
-		words)
-			words=("${_words[@]}")
-			;;
-		cword)
-			cword=$_cword
-			;;
-		esac
-		shift
-	done
-}
-
 print_comp ()
 {
 	local IFS=$'\n'
@@ -56,10 +35,10 @@ print_comp ()
 
 run_completion ()
 {
-	local -a COMPREPLY _words
-	local _cword
-	_words=( $1 )
-	(( _cword = ${#_words[@]} - 1 ))
+	local -a COMPREPLY COMP_WORDS
+	local COMP_CWORD
+	COMP_WORDS=( $1 )
+	(( COMP_CWORD = ${#COMP_WORDS[@]} - 1 ))
 	_git && print_comp
 }

i.e. to set COMP_WORDS and COMP_CWORD in run_completion() and it
worked.  However, I agree that it feels iffy to mess with a
shell-specific variable, and I'm afraid that this just happened to
work on my system, but it might be broken in previous or future bash
versions.


Best,
Gábor

```

## Thomas Rast, 2012-04-13 11:33

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <871unrzx19.fsf@thomas.inf.ethz.ch>
URL: https://gitlist.dev/e/871unrzx19.fsf%40thomas.inf.ethz.ch
In-Reply-To: <20120413105238.GE2164@goldbirke>

```
SZEDER Gábor <szeder@ira.uka.de> writes:

> On Fri, Apr 13, 2012 at 01:34:46PM +0300, Felipe Contreras wrote:
>> >> +     run_completion "git f" &&
>> >> +     ! grep -q -v "^f" out
>> >
>> > grep is not a git command, so I'm not sure, but shouldn't these use
>> > 'test_must_fail grep' instead of '! grep'?
>> 
>> I'm not sure. Junio has already queued this, maybe you should send a
>> patch on top of that.
>
> It seems that both are used in the test suite, but '! grep' is more
> common, so perhaps it's good as it is.
>
> $ git grep '! grep' -- t |wc -l
> 136
> $ git grep 'test_must_fail grep' -- t |wc -l
> 17

test_must_fail catches a segfault or other signal exit as "bad", unlike
! which would accept this.

Since we trust the platform tools to work (on the grounds that you have
bigger problems if they don't), ! grep is fine.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

```

## Felipe Contreras, 2012-04-13 11:56

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s0C=MV_gxsH9Mk9V6Q27E2E=o1nr3HShBFSTyTYC0Tg4w@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s0C%3DMV_gxsH9Mk9V6Q27E2E%3Do1nr3HShBFSTyTYC0Tg4w%40mail.gmail.com
In-Reply-To: <20120413111437.GF2164@goldbirke>

```
2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:

> i.e. to set COMP_WORDS and COMP_CWORD in run_completion() and it
> worked.  However, I agree that it feels iffy to mess with a
> shell-specific variable, and I'm afraid that this just happened to
> work on my system, but it might be broken in previous or future bash
> versions.

Yeah, we could explore that possibility later, as
_get_comp_words_by_ref is part of the completion, and should be tested
as well, otherwise we might be missing some bugs.

However, I wonder if _get_comp_words_by_ref is needed at all. From
what I can see it has to do with '--foo=bar' and 'foo:bar'
completions, which in fact don't work correctly in zsh (I have patches
for zsh to fix this though), but by modifying the code that checks for
'--*=*' stuff we might be able to get rid of it, or at least the call
to __git_reassemble_comp_words_by_ref. Right?

Cheers.

-- 
Felipe Contreras

```

## Junio C Hamano, 2012-04-13 19:45

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <7vaa2f2z62.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vaa2f2z62.fsf%40alter.siamese.dyndns.org
In-Reply-To: <7vzkag3528.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> Felipe Contreras <felipe.contreras@gmail.com> writes:
>
>> Do I need to resend the patch?
>
> Not necessarily.  If you can eyeball what is queued on 'pu' and gave me an
> Ack (or send a replacement with "You stupid, you amended it wrong!") that
> should be sufficient.

Actually, can we have a follow-up patch to add in-code comment before the
_get_comp_words_by_ref shell function in t9902 to explain why the test
overrides this implementation detail?  The script being tested will need
to maintain _some_ invariants that is expected by this hack in order to
keep the test working (or this hack needs to be updated when the updates
to the completion script need to break the invariants), but without any
explanation on what the invariants are, it is making it hard to others to
update the completion script.

Thanks.

```

## Junio C Hamano, 2012-04-13 19:48

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <7v62d32z1a.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v62d32z1a.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20120413091236.GC2164@goldbirke>

```
SZEDER Gábor <szeder@ira.uka.de> writes:

>> +_get_comp_words_by_ref ()
>> +{
>> +	while [ $# -gt 0 ]; do
>> +		case "$1" in
>> +		cur)
>> +			cur=${_words[_cword]}
>> +			;;
>> +		prev)
>> +			prev=${_words[_cword-1]}
>> +			;;
>> +		words)
>> +			words=("${_words[@]}")
>> +			;;
>> +		cword)
>> +			cword=$_cword
>> +			;;
>> +		esac
>> +		shift
>> +	done
>> +}
>
> Git's completion script already implements this function.  Why
> override it here?

It is not "already implements" that I am worried about, but it implements
it differently without explaining why, which is worrying.  I agree it
needs to be explained before the function.
>> +	# plumbing
>> +	! grep -q "^ls-files \$" out
>
> The && is missing here at the end of the line.

True.

>> +	run_completion "git f" &&
>> +	! grep -q -v "^f" out
>
> grep is not a git command, so I'm not sure, but shouldn't these use
> 'test_must_fail grep' instead of '! grep'?

"! grep" is fine.  We are not trying to catch the case where we break the
implementation of "grep" to cause it to segfault.

```

## Felipe Contreras, 2012-04-14 02:06

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s2zEsvChCVOaCKYoOtU-ny5=zkHwHg8tCjWjL27UX4n8A@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s2zEsvChCVOaCKYoOtU-ny5%3DzkHwHg8tCjWjL27UX4n8A%40mail.gmail.com
In-Reply-To: <7v62d32z1a.fsf@alter.siamese.dyndns.org>

```
2012/4/13 Junio C Hamano <gitster@pobox.com>:
> SZEDER Gábor <szeder@ira.uka.de> writes:
>
>>> +_get_comp_words_by_ref ()
>>> +{
>>> +    while [ $# -gt 0 ]; do
>>> +            case "$1" in
>>> +            cur)
>>> +                    cur=${_words[_cword]}
>>> +                    ;;
>>> +            prev)
>>> +                    prev=${_words[_cword-1]}
>>> +                    ;;
>>> +            words)
>>> +                    words=("${_words[@]}")
>>> +                    ;;
>>> +            cword)
>>> +                    cword=$_cword
>>> +                    ;;
>>> +            esac
>>> +            shift
>>> +    done
>>> +}
>>
>> Git's completion script already implements this function.  Why
>> override it here?
>
> It is not "already implements" that I am worried about, but it implements
> it differently without explaining why, which is worrying.  I agree it
> needs to be explained before the function.
>>> +    # plumbing
>>> +    ! grep -q "^ls-files \$" out
>>
>> The && is missing here at the end of the line.
>
> True.
>
>>> +    run_completion "git f" &&
>>> +    ! grep -q -v "^f" out
>>
>> grep is not a git command, so I'm not sure, but shouldn't these use
>> 'test_must_fail grep' instead of '! grep'?
>
> "! grep" is fine.  We are not trying to catch the case where we break the
> implementation of "grep" to cause it to segfault.

--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -27,6 +27,9 @@ complete ()

 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"

+# We don't need this function to actually join words or do anything special.
+# Also, it's more clean to avoid touching bash's internal completion variables.
+# So let's override it with a minimal version for testing purposes.
 _get_comp_words_by_ref ()
 {
        while [ $# -gt 0 ]; do
@@ -77,7 +80,7 @@ test_expect_success 'basic' '
        # script
        grep -q "^filter-branch \$" out &&
        # plumbing
-       ! grep -q "^ls-files \$" out
+       ! grep -q "^ls-files \$" out &&

        run_completion "git f" &&
        ! grep -q -v "^f" out

-- 
Felipe Contreras

```

## SZEDER Gábor, 2012-04-17 00:31

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <20120417003100.GB2299@goldbirke>
URL: https://gitlist.dev/e/20120417003100.GB2299%40goldbirke
In-Reply-To: <1334181423-4391-1-git-send-email-felipe.contreras@gmail.com>

```
Hi,


I picked up Stephen Boyd's two-patch series[1] to use parse-options to
generate options for git commands, and the following test promply
failed (taken from 5c293a6b (tests: add initial bash completion tests,
2012-04-12)):

test_expect_success 'double dash "git checkout"' '
        sed -e "s/Z$//" >expected <<-\EOF &&
        --quiet Z
        --ours Z
        --theirs Z
        --track Z
        --no-track Z
        --merge Z
        --conflict=
        --orphan Z
        --patch Z
        EOF
        test_completion "git checkout --"
'

Not surprising, the completion script doesn't know about many 'git
checkout' long options.  So whenever 'git checkout' learns a new long
option, this list must be updated.  This won't be more work than the
update of the completion script, so this is probably OK.

But it got me thinking about what do we actually want to test here?
Whether the completion script returns the right long options in a
specific order upon 'git checkout --<TAB>'?  Or whether _git() works
properly and invokes the right command-specific completion function?
Or whether regular options get a trailing space while options
expecting an argument don't?  Or is this sort of an integration test
and basically all of the above?


[1] - http://thread.gmane.org/gmane.comp.version-control.git/195158/focus=195158

Best,
Gábor

```

## Felipe Contreras, 2012-04-17 06:32

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <CAMP44s1CTCPThri6mq0NTvD27WTEiwLTfhHCw+nD+8YwApwL=g@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s1CTCPThri6mq0NTvD27WTEiwLTfhHCw%2BnD%2B8YwApwL%3Dg%40mail.gmail.com
In-Reply-To: <20120417003100.GB2299@goldbirke>

```
On Tue, Apr 17, 2012 at 3:31 AM, SZEDER Gábor <szeder@fzi.de> wrote:
> Hi,
>
>
> I picked up Stephen Boyd's two-patch series[1] to use parse-options to
> generate options for git commands, and the following test promply
> failed (taken from 5c293a6b (tests: add initial bash completion tests,
> 2012-04-12)):
>
> test_expect_success 'double dash "git checkout"' '
>        sed -e "s/Z$//" >expected <<-\EOF &&
>        --quiet Z
>        --ours Z
>        --theirs Z
>        --track Z
>        --no-track Z
>        --merge Z
>        --conflict=
>        --orphan Z
>        --patch Z
>        EOF
>        test_completion "git checkout --"
> '
>
> Not surprising, the completion script doesn't know about many 'git
> checkout' long options.  So whenever 'git checkout' learns a new long
> option, this list must be updated.  This won't be more work than the
> update of the completion script, so this is probably OK.
>
> But it got me thinking about what do we actually want to test here?
> Whether the completion script returns the right long options in a
> specific order upon 'git checkout --<TAB>'?  Or whether _git() works
> properly and invokes the right command-specific completion function?
> Or whether regular options get a trailing space while options
> expecting an argument don't?  Or is this sort of an integration test
> and basically all of the above?

I don't think the order is relevant, just that all the options are
there, and the ones with arguments have a = in there, and the ones
that don't, a space.

-- 
Felipe Contreras

```

## SZEDER Gábor, 2012-04-17 10:22

Subject: Re: [PATCH v2] tests: add initial bash completion tests
Message-ID: <20120417102215.GA22778@goldbirke>
URL: https://gitlist.dev/e/20120417102215.GA22778%40goldbirke
In-Reply-To: <CAMP44s1CTCPThri6mq0NTvD27WTEiwLTfhHCw+nD+8YwApwL=g@mail.gmail.com>

```
On Tue, Apr 17, 2012 at 09:32:29AM +0300, Felipe Contreras wrote:
> On Tue, Apr 17, 2012 at 3:31 AM, SZEDER Gábor <szeder@fzi.de> wrote:
> > Hi,
> >
> >
> > I picked up Stephen Boyd's two-patch series[1] to use parse-options to
> > generate options for git commands, and the following test promply
> > failed (taken from 5c293a6b (tests: add initial bash completion tests,
> > 2012-04-12)):
> >
> > test_expect_success 'double dash "git checkout"' '
> >        sed -e "s/Z$//" >expected <<-\EOF &&
> >        --quiet Z
> >        --ours Z
> >        --theirs Z
> >        --track Z
> >        --no-track Z
> >        --merge Z
> >        --conflict=
> >        --orphan Z
> >        --patch Z
> >        EOF
> >        test_completion "git checkout --"
> > '
> >
> > Not surprising, the completion script doesn't know about many 'git
> > checkout' long options.  So whenever 'git checkout' learns a new long
> > option, this list must be updated.  This won't be more work than the
> > update of the completion script, so this is probably OK.
> >
> > But it got me thinking about what do we actually want to test here?
> > Whether the completion script returns the right long options in a
> > specific order upon 'git checkout --<TAB>'?  Or whether _git() works
> > properly and invokes the right command-specific completion function?
> > Or whether regular options get a trailing space while options
> > expecting an argument don't?  Or is this sort of an integration test
> > and basically all of the above?
> 
> I don't think the order is relevant, just that all the options are
> there, 

The order of options is not relevant in the completion script, because
Bash will sort them alphabetically anyway.  But it is relevant in the
test: it fails if the order is changed either in the completion script
or in the test.

> and the ones with arguments have a = in there, and the ones
> that don't, a space.

Couldn't we check that better with a test or two for __gitcomp()?

If a test for __gitcomp() fails, we would immediately have a fairly
good idea where to look for the cause of the breakage.  However, if
this 'double dash "git checkout"' test fails, there are a bunch of
other things that can possibly cause the failure.

Patch comes in a minute.

Best,
Gábor

```

## SZEDER Gábor, 2012-04-17 10:27

Subject: [PATCH] tests: add tests for the __gitcomp() completion helper function
Message-ID: <1334658435-30769-1-git-send-email-szeder@ira.uka.de>
URL: https://gitlist.dev/e/1334658435-30769-1-git-send-email-szeder%40ira.uka.de
In-Reply-To: <20120417102215.GA22778@goldbirke>

```
These tests check that trailing space, prefix, and suffix are added
correctly.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 t/t9902-completion.sh |   85 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index cc127320..5bda6b6e 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -73,6 +73,91 @@ test_completion ()
 	test_cmp expected out
 }
 
+newline=$'\n'
+
+test_expect_success '__gitcomp - trailing space - options' '
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	--reuse-message=Z
+	--reedit-message=Z
+	--reset-author Z
+	EOF
+	(
+		local -a COMPREPLY &&
+		cur="--re" &&
+		__gitcomp "--dry-run --reuse-message= --reedit-message=
+				--reset-author" &&
+		IFS="$newline" &&
+		echo "${COMPREPLY[*]}" > out
+	) &&
+	test_cmp expected out
+'
+
+test_expect_success '__gitcomp - trailing space - config keys' '
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	branch.Z
+	branch.autosetupmerge Z
+	branch.autosetuprebase Z
+	browser.Z
+	EOF
+	(
+		local -a COMPREPLY &&
+		cur="br" &&
+		__gitcomp "branch. branch.autosetupmerge
+				branch.autosetuprebase browser." &&
+		IFS="$newline" &&
+		echo "${COMPREPLY[*]}" > out
+	) &&
+	test_cmp expected out
+'
+
+test_expect_success '__gitcomp - option parameter' '
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	recursive Z
+	resolve Z
+	EOF
+	(
+		local -a COMPREPLY &&
+		cur="--strategy=re" &&
+		__gitcomp "octopus ours recursive resolve subtree
+			" "" "re" &&
+		IFS="$newline" &&
+		echo "${COMPREPLY[*]}" > out
+	) &&
+	test_cmp expected out
+'
+
+test_expect_success '__gitcomp - prefix' '
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	branch.maint.merge Z
+	branch.maint.mergeoptions Z
+	EOF
+	(
+		local -a COMPREPLY &&
+		cur="branch.me" &&
+		__gitcomp "remote merge mergeoptions rebase
+			" "branch.maint." "me" &&
+		IFS="$newline" &&
+		echo "${COMPREPLY[*]}" > out
+	) &&
+	test_cmp expected out
+'
+
+test_expect_success '__gitcomp - suffix' '
+	sed -e "s/Z$//" >expected <<-\EOF &&
+	branch.master.Z
+	branch.maint.Z
+	EOF
+	(
+		local -a COMPREPLY &&
+		cur="branch.me" &&
+		__gitcomp "master maint next pu
+			" "branch." "ma" "." &&
+		IFS="$newline" &&
+		echo "${COMPREPLY[*]}" > out
+	) &&
+	test_cmp expected out
+'
+
 test_expect_success 'basic' '
 	run_completion "git \"\"" &&
 	# built-in
-- 
1.7.10.216.gb52c0

```
