threads / patch / 30219

v2tests: add initial bash completion tests

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

## tl;dr

21 messages between Apr 11, 2012 and Apr 17, 2012. Diffs are folded; open one to read it.

replies: 20people: 5as markdown or json

Felipe Contreras· Apr 11, 2012, 21:57 UTC · lore
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
Show changes to t/t9902-completion.sh +115 −0
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· Apr 11, 2012, 23:48 UTC · re: Felipe Contreras · lore

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

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(-)
Show changes to t/t9902-completion.sh +17 −17
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· Apr 12, 2012, 16:15 UTC · re: Felipe Contreras · lore

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

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

Show 14 quoted lines
> +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

Show changes to diff +1 −1
@@ -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· Apr 12, 2012, 17:43 UTC · re: Felipe Contreras · lore

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

Felipe Contreras <felipe.contreras@gmail.com> writes:
Show 19 quoted lines
> 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...

Show 11 quoted lines
> --- 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· Apr 12, 2012, 23:18 UTC · re: Junio C Hamano · lore

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

On Thu, Apr 12, 2012 at 8:43 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 23 quoted lines
> 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· Apr 12, 2012, 23:26 UTC · re: Felipe Contreras · lore

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

Felipe Contreras <felipe.contreras@gmail.com> writes:
Show 28 quoted lines
> 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.
Junio C Hamano· Apr 13, 2012, 19:45 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <gitster@pobox.com> writes:
Show 7 quoted lines
> 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.
SZEDER Gábor· Apr 13, 2012, 09:12 UTC · re: Felipe Contreras · lore

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

Hi,
On Thu, Apr 12, 2012 at 12:57:03AM +0300, Felipe Contreras wrote:
Show 67 quoted lines
> 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?

Show 30 quoted lines
> +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· Apr 13, 2012, 09:45 UTC · re: SZEDER Gábor · lore

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

On Fri, Apr 13, 2012 at 11:12:36AM +0200, SZEDER Gábor wrote:
Show 26 quoted lines
> 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.

Show 14 quoted lines
> > +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· Apr 13, 2012, 10:48 UTC · re: SZEDER Gábor · lore

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

2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:
Show 39 quoted lines
> 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· Apr 13, 2012, 11:14 UTC · re: Felipe Contreras · lore

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

On Fri, Apr 13, 2012 at 01:48:51PM +0300, Felipe Contreras wrote:
Show 49 quoted lines
> 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):

Show changes to t/t9902-completion.sh +4 −25
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
Felipe Contreras· Apr 13, 2012, 11:56 UTC · re: SZEDER Gábor · lore

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

2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:
Show 5 quoted lines
> 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
Felipe Contreras· Apr 13, 2012, 10:34 UTC · re: SZEDER Gábor · lore

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

2012/4/13 SZEDER Gábor <szeder@ira.uka.de>:
Show 10 quoted lines
>> +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.
Show 5 quoted lines
>> +     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
SZEDER Gábor· Apr 13, 2012, 10:52 UTC · re: Felipe Contreras · lore

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

On Fri, Apr 13, 2012 at 01:34:46PM +0300, Felipe Contreras wrote:
Show 8 quoted lines
> >> +     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

Thomas Rast· Apr 13, 2012, 11:33 UTC · re: SZEDER Gábor · lore

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

SZEDER Gábor <szeder@ira.uka.de> writes:
Show 17 quoted lines
> 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
Junio C Hamano· Apr 13, 2012, 19:48 UTC · re: SZEDER Gábor · lore

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

SZEDER Gábor <szeder@ira.uka.de> writes:
Show 23 quoted lines
>> +_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.
Show 5 quoted lines
>> +	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· Apr 14, 2012, 02:06 UTC · re: Junio C Hamano · lore

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

2012/4/13 Junio C Hamano <gitster@pobox.com>:
Show 44 quoted lines
> 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.
Show changes to t/t9902-completion.sh +4 −1
--- 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· Apr 17, 2012, 00:31 UTC · re: Felipe Contreras · lore

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

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· Apr 17, 2012, 06:32 UTC · re: SZEDER Gábor · lore

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

On Tue, Apr 17, 2012 at 3:31 AM, SZEDER Gábor <szeder@fzi.de> wrote:
Show 35 quoted lines
> 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· Apr 17, 2012, 10:22 UTC · re: Felipe Contreras · lore

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

On Tue, Apr 17, 2012 at 09:32:29AM +0300, Felipe Contreras wrote:
Show 39 quoted lines
> 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· Apr 17, 2012, 10:27 UTC · re: SZEDER Gábor · lore

[PATCH] tests: add tests for the __gitcomp() completion helper function

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(+)
Show changes to t/t9902-completion.sh +85 −0
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

← back to recent threads