All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tests: add initial bash completion tests
@ 2012-04-11 21:57 Felipe Contreras
  2012-04-11 23:48 ` Junio C Hamano
                   ` (3 more replies)
  0 siblings, 4 replies; 21+ messages in thread
From: Felipe Contreras @ 2012-04-11 21:57 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, SZEDER Gábor, Junio C Hamano, Thomas Rast,
	Jeff King, Felipe Contreras

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-11 21:57 [PATCH v2] tests: add initial bash completion tests Felipe Contreras
@ 2012-04-11 23:48 ` Junio C Hamano
  2012-04-12 16:15 ` Felipe Contreras
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 21+ messages in thread
From: Junio C Hamano @ 2012-04-11 23:48 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, SZEDER Gábor, Thomas Rast, Jeff King

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 --"
 '

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-11 21:57 [PATCH v2] tests: add initial bash completion tests Felipe Contreras
  2012-04-11 23:48 ` Junio C Hamano
@ 2012-04-12 16:15 ` Felipe Contreras
  2012-04-12 17:43   ` Junio C Hamano
  2012-04-13  9:12 ` SZEDER Gábor
  2012-04-17  0:31 ` SZEDER Gábor
  3 siblings, 1 reply; 21+ messages in thread
From: Felipe Contreras @ 2012-04-12 16:15 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, SZEDER Gábor, Junio C Hamano, Thomas Rast,
	Jeff King, Felipe Contreras

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-12 16:15 ` Felipe Contreras
@ 2012-04-12 17:43   ` Junio C Hamano
  2012-04-12 23:18     ` Felipe Contreras
  0 siblings, 1 reply; 21+ messages in thread
From: Junio C Hamano @ 2012-04-12 17:43 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, SZEDER Gábor, Junio C Hamano,
	Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-12 17:43   ` Junio C Hamano
@ 2012-04-12 23:18     ` Felipe Contreras
  2012-04-12 23:26       ` Junio C Hamano
  0 siblings, 1 reply; 21+ messages in thread
From: Felipe Contreras @ 2012-04-12 23:18 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jonathan Nieder, SZEDER Gábor, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-12 23:18     ` Felipe Contreras
@ 2012-04-12 23:26       ` Junio C Hamano
  2012-04-13 19:45         ` Junio C Hamano
  0 siblings, 1 reply; 21+ messages in thread
From: Junio C Hamano @ 2012-04-12 23:26 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, SZEDER Gábor, Thomas Rast, Jeff King

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.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-11 21:57 [PATCH v2] tests: add initial bash completion tests Felipe Contreras
  2012-04-11 23:48 ` Junio C Hamano
  2012-04-12 16:15 ` Felipe Contreras
@ 2012-04-13  9:12 ` SZEDER Gábor
  2012-04-13  9:45   ` SZEDER Gábor
                     ` (2 more replies)
  2012-04-17  0:31 ` SZEDER Gábor
  3 siblings, 3 replies; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-13  9:12 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13  9:12 ` SZEDER Gábor
@ 2012-04-13  9:45   ` SZEDER Gábor
  2012-04-13 10:48     ` Felipe Contreras
  2012-04-13 10:34   ` Felipe Contreras
  2012-04-13 19:48   ` Junio C Hamano
  2 siblings, 1 reply; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-13  9:45 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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
> > +}

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13  9:12 ` SZEDER Gábor
  2012-04-13  9:45   ` SZEDER Gábor
@ 2012-04-13 10:34   ` Felipe Contreras
  2012-04-13 10:52     ` SZEDER Gábor
  2012-04-13 19:48   ` Junio C Hamano
  2 siblings, 1 reply; 21+ messages in thread
From: Felipe Contreras @ 2012-04-13 10:34 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13  9:45   ` SZEDER Gábor
@ 2012-04-13 10:48     ` Felipe Contreras
  2012-04-13 11:14       ` SZEDER Gábor
  0 siblings, 1 reply; 21+ messages in thread
From: Felipe Contreras @ 2012-04-13 10:48 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13 10:34   ` Felipe Contreras
@ 2012-04-13 10:52     ` SZEDER Gábor
  2012-04-13 11:33       ` Thomas Rast
  0 siblings, 1 reply; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-13 10:52 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13 10:48     ` Felipe Contreras
@ 2012-04-13 11:14       ` SZEDER Gábor
  2012-04-13 11:56         ` Felipe Contreras
  0 siblings, 1 reply; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-13 11:14 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13 10:52     ` SZEDER Gábor
@ 2012-04-13 11:33       ` Thomas Rast
  0 siblings, 0 replies; 21+ messages in thread
From: Thomas Rast @ 2012-04-13 11:33 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: Felipe Contreras, git, Jonathan Nieder, Junio C Hamano,
	Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13 11:14       ` SZEDER Gábor
@ 2012-04-13 11:56         ` Felipe Contreras
  0 siblings, 0 replies; 21+ messages in thread
From: Felipe Contreras @ 2012-04-13 11:56 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-12 23:26       ` Junio C Hamano
@ 2012-04-13 19:45         ` Junio C Hamano
  0 siblings, 0 replies; 21+ messages in thread
From: Junio C Hamano @ 2012-04-13 19:45 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, SZEDER Gábor, Thomas Rast, Jeff King

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.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13  9:12 ` SZEDER Gábor
  2012-04-13  9:45   ` SZEDER Gábor
  2012-04-13 10:34   ` Felipe Contreras
@ 2012-04-13 19:48   ` Junio C Hamano
  2012-04-14  2:06     ` Felipe Contreras
  2 siblings, 1 reply; 21+ messages in thread
From: Junio C Hamano @ 2012-04-13 19:48 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: Felipe Contreras, git, Jonathan Nieder, Thomas Rast, Jeff King

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.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-13 19:48   ` Junio C Hamano
@ 2012-04-14  2:06     ` Felipe Contreras
  0 siblings, 0 replies; 21+ messages in thread
From: Felipe Contreras @ 2012-04-14  2:06 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: SZEDER Gábor, git, Jonathan Nieder, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-11 21:57 [PATCH v2] tests: add initial bash completion tests Felipe Contreras
                   ` (2 preceding siblings ...)
  2012-04-13  9:12 ` SZEDER Gábor
@ 2012-04-17  0:31 ` SZEDER Gábor
  2012-04-17  6:32   ` Felipe Contreras
  3 siblings, 1 reply; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-17  0:31 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, SZEDER Gábor, Junio C Hamano,
	Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-17  0:31 ` SZEDER Gábor
@ 2012-04-17  6:32   ` Felipe Contreras
  2012-04-17 10:22     ` SZEDER Gábor
  0 siblings, 1 reply; 21+ messages in thread
From: Felipe Contreras @ 2012-04-17  6:32 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: git, Jonathan Nieder, SZEDER Gábor, Junio C Hamano,
	Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] tests: add initial bash completion tests
  2012-04-17  6:32   ` Felipe Contreras
@ 2012-04-17 10:22     ` SZEDER Gábor
  2012-04-17 10:27       ` [PATCH] tests: add tests for the __gitcomp() completion helper function SZEDER Gábor
  0 siblings, 1 reply; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-17 10:22 UTC (permalink / raw)
  To: Felipe Contreras
  Cc: git, Jonathan Nieder, Junio C Hamano, Thomas Rast, Jeff King

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH] tests: add tests for the __gitcomp() completion helper function
  2012-04-17 10:22     ` SZEDER Gábor
@ 2012-04-17 10:27       ` SZEDER Gábor
  0 siblings, 0 replies; 21+ messages in thread
From: SZEDER Gábor @ 2012-04-17 10:27 UTC (permalink / raw)
  To: git
  Cc: Felipe Contreras, Jonathan Nieder, Junio C Hamano, Thomas Rast,
	Jeff King, SZEDER Gábor

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2012-04-17 10:27 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-11 21:57 [PATCH v2] tests: add initial bash completion tests Felipe Contreras
2012-04-11 23:48 ` Junio C Hamano
2012-04-12 16:15 ` Felipe Contreras
2012-04-12 17:43   ` Junio C Hamano
2012-04-12 23:18     ` Felipe Contreras
2012-04-12 23:26       ` Junio C Hamano
2012-04-13 19:45         ` Junio C Hamano
2012-04-13  9:12 ` SZEDER Gábor
2012-04-13  9:45   ` SZEDER Gábor
2012-04-13 10:48     ` Felipe Contreras
2012-04-13 11:14       ` SZEDER Gábor
2012-04-13 11:56         ` Felipe Contreras
2012-04-13 10:34   ` Felipe Contreras
2012-04-13 10:52     ` SZEDER Gábor
2012-04-13 11:33       ` Thomas Rast
2012-04-13 19:48   ` Junio C Hamano
2012-04-14  2:06     ` Felipe Contreras
2012-04-17  0:31 ` SZEDER Gábor
2012-04-17  6:32   ` Felipe Contreras
2012-04-17 10:22     ` SZEDER Gábor
2012-04-17 10:27       ` [PATCH] tests: add tests for the __gitcomp() completion helper function SZEDER Gábor

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.