diff mbox series

t/test_lib: avoid naked bash arrays in file_lineno

Message ID 20200507055118.69971-1-carenas@gmail.com (mailing list archive)
State New, archived
Headers show
Series t/test_lib: avoid naked bash arrays in file_lineno | expand

Commit Message

Carlo Marcelo Arenas Belón May 7, 2020, 5:51 a.m. UTC
662f9cf154 (tests: when run in Bash, annotate test failures with file
name/line number, 2020-04-11), introduces a way to report the location
(file:lineno) of a failed test case by traversing the bash callstack.

The implementation requires bash and is therefore protected by a guard
but NetBSD sh will still have to parse the function and therefore will
result in:

  ** t0000-basic.sh ***
  ./test-lib.sh: 681: Syntax error: Bad substitution

Enclose the bash specific code inside an eval to avoid parsing errors
and while at it, simplify the logic so that instead of traversing the
callstack just pop the two topmost entries that are required.

Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 t/test-lib.sh | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Comments

Junio C Hamano May 7, 2020, 2:53 p.m. UTC | #1
Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:

> 662f9cf154 (tests: when run in Bash, annotate test failures with file
> name/line number, 2020-04-11), introduces a way to report the location
> (file:lineno) of a failed test case by traversing the bash callstack.
>
> The implementation requires bash and is therefore protected by a guard
> but NetBSD sh will still have to parse the function and therefore will
> result in:
>
>   ** t0000-basic.sh ***
>   ./test-lib.sh: 681: Syntax error: Bad substitution
>
> Enclose the bash specific code inside an eval to avoid parsing errors
> and while at it, simplify the logic so that instead of traversing the
> callstack just pop the two topmost entries that are required.
>
> Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

Is the rewritten bash-snippet in the evaled text what Dscho
suggested us to use, or is it totally yours?  I know Dscho helped to
shoot down some "simpler" reimplementations you came up with,
pointing out how they were broken, but it is unclear how we ended up
with this version.

I wish you didn't do the "while at it" rewrite in the same patch.
If it were only "put bash-only stuff in an evaled string", it would
have been a lot more comfortable applying it and merging quickly
down, as it would be clear that we won't be breaking bash codepath
and we'd be helping other shells.  With the "while at it", you made
it quite unclear.

> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
> ---
>  t/test-lib.sh | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index 1b221951a8..60b8e70678 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -677,14 +677,13 @@ die () {
>  
>  file_lineno () {
>  	test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0
> -	local i
> -	for i in ${!BASH_SOURCE[*]}
> -	do
> -		case $i,"${BASH_SOURCE[$i]##*/}" in
> -		0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;;
> -		*,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;;
> -		esac
> -	done
> +	eval '
> +		local n=$(echo ${#BASH_SOURCE[*]})
> +		if test $n -ge 2
> +		then
> +			echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: "
> +		fi
> +	'
>  }
>  
>  GIT_EXIT_OK=
Carlo Marcelo Arenas Belón May 7, 2020, 5:33 p.m. UTC | #2
On Thu, May 7, 2020 at 7:53 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:
>
> > 662f9cf154 (tests: when run in Bash, annotate test failures with file
> > name/line number, 2020-04-11), introduces a way to report the location
> > (file:lineno) of a failed test case by traversing the bash callstack.
> >
> > The implementation requires bash and is therefore protected by a guard
> > but NetBSD sh will still have to parse the function and therefore will
> > result in:
> >
> >   ** t0000-basic.sh ***
> >   ./test-lib.sh: 681: Syntax error: Bad substitution
> >
> > Enclose the bash specific code inside an eval to avoid parsing errors
> > and while at it, simplify the logic so that instead of traversing the
> > callstack just pop the two topmost entries that are required.
> >
> > Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
>
> Is the rewritten bash-snippet in the evaled text what Dscho
> suggested us to use, or is it totally yours?

It is all mine, but Dscho's detailed description of the implementation it
is replacing allowed me to validate its correctness (under the documented
use cases)

my hope was that Dscho's and Danh would quickly agree as it is simpler
and faster, as well as providing some more description of its operation in
the commit message for future reviewers.

> I know Dscho helped to
> shoot down some "simpler" reimplementations you came up with,
> pointing out how they were broken, but it is unclear how we ended up
> with this version.

I made the wrong assumptions while focusing on translating the code
to posix sh and ended up with a broken version.

Another reason why I did the "while at it" was to avoid someone else
having the same problem by simplifying it and making sure that uncommon
syntax (like ${1+$1..}, or the use of $LINENO which would never apply
to our code)
weren't needed.

> I wish you didn't do the "while at it" rewrite in the same patch.
> If it were only "put bash-only stuff in an evaled string", it would
> have been a lot more comfortable applying it and merging quickly
> down, as it would be clear that we won't be breaking bash codepath
> and we'd be helping other shells.  With the "while at it", you made
> it quite unclear.

fair enough, and considering that "fixing other shell" has higher priority
will send a v2 to do so.

will do any "simplification" on a follow up and try to monitor these issues
early

Carlo
Johannes Schindelin May 7, 2020, 7:52 p.m. UTC | #3
Hi Carlo,

On Wed, 6 May 2020, Carlo Marcelo Arenas Belón wrote:

> 662f9cf154 (tests: when run in Bash, annotate test failures with file
> name/line number, 2020-04-11), introduces a way to report the location
> (file:lineno) of a failed test case by traversing the bash callstack.
>
> The implementation requires bash and is therefore protected by a guard
> but NetBSD sh will still have to parse the function and therefore will
> result in:
>
>   ** t0000-basic.sh ***
>   ./test-lib.sh: 681: Syntax error: Bad substitution
>
> Enclose the bash specific code inside an eval to avoid parsing errors
> and while at it, simplify the logic so that instead of traversing the
> callstack just pop the two topmost entries that are required.

I would be okay with that, but that's not what the patch does:

> Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
> ---
>  t/test-lib.sh | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index 1b221951a8..60b8e70678 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -677,14 +677,13 @@ die () {
>
>  file_lineno () {
>  	test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0
> -	local i
> -	for i in ${!BASH_SOURCE[*]}
> -	do
> -		case $i,"${BASH_SOURCE[$i]##*/}" in
> -		0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;;
> -		*,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;;
> -		esac
> -	done

Note how this `for` loop stops as soon as it finds a `t/[0-9]*.sh` in the
backtrace?

> +	eval '
> +		local n=$(echo ${#BASH_SOURCE[*]})
> +		if test $n -ge 2
> +		then
> +			echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: "
> +		fi

This loop is completely lost here.

Now, you could argue that your version is better because it avoids the
loop and always shows the ultimate caller in the backtrace.

However, I would then immediately point you to `t/t0027-auto-crlf.sh` and
ask whether it is all that useful to the `commit_chk_wrnNNO` call rather
than one of the five `test_expect_success` calls contained in that
function.

Besides, you simply replaces `${1+$1: }` by `$1: `.

Again, you could argue that your version is better because there is now
only one caller, and it always passes `error` as first parameter.

I would not be _too_ happy about losing the logic that allows calling
`file_lineno` in other circumstances (I found it valuable for debugging),
but _in the least_ you need to talk about this change in the commit
message. Just changing the behavior without even describing, let alone
justifying, it in the commit message is a bad idea.

Ciao,
Dscho

> +	'
>  }
>
>  GIT_EXIT_OK=
> --
> 2.26.2.686.gfaf46a9ccd
>
>
Carlo Marcelo Arenas Belón May 8, 2020, 12:58 a.m. UTC | #4
On Thu, May 07, 2020 at 09:52:12PM +0200, Johannes Schindelin wrote:
> On Wed, 6 May 2020, Carlo Marcelo Arenas Belón wrote:
> >
> > Enclose the bash specific code inside an eval to avoid parsing errors
> > and while at it, simplify the logic so that instead of traversing the
> > callstack just pop the two topmost entries that are required.
> 
> I would be okay with that, but that's not what the patch does:

FWIW that was the intention, but luckily Junio quickly predicted it was
most likely buggy and so has been since made obsolete by:

  https://lore.kernel.org/git/20200507175706.19986-1-carenas@gmail.com/

> > +	eval '
> > +		local n=$(echo ${#BASH_SOURCE[*]})
> > +		if test $n -ge 2
> > +		then
> > +			echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: "
> > +		fi
> 
> This loop is completely lost here.
> 
> Now, you could argue that your version is better because it avoids the
> loop and always shows the ultimate caller in the backtrace.
> 
> However, I would then immediately point you to `t/t0027-auto-crlf.sh` and
> ask whether it is all that useful to the `commit_chk_wrnNNO` call rather
> than one of the five `test_expect_success` calls contained in that
> function.

as I said originally, I thought it would had been even better to be specific
up to the line number of the instruction that failed, after all we already
have the whole function code for context in the message.

> Besides, you simply replaces `${1+$1: }` by `$1: `.

I couldn't figure out why that was needed, but while I am just slow, this
one is actually genius!; you are using a shell parameter expansion (which
is even POSIX sh compatible) to do string concatenation!

conditionally, noneless and not even using a silly if (like I would have
done) or a bash += concatenation operator.

> Again, you could argue that your version is better because there is now
> only one caller, and it always passes `error` as first parameter.
> 
> I would not be _too_ happy about losing the logic that allows calling
> `file_lineno` in other circumstances (I found it valuable for debugging),
> but _in the least_ you need to talk about this change in the commit
> message. Just changing the behavior without even describing, let alone
> justifying, it in the commit message is a bad idea.

fair enough; but in my defense, I couldn't had been made aware this was
being used also for debugging, or called without the parameter or even
called in a way where it will report its own LINENO (which you clearly
explained before as being the wrong value to report and that lead to
my confused first attempt at porting it)

thanks again, for your insightful feedback, and guess then there is no
more point on trying to simplify it further, and if you could ACK my
other proposal, hopefully neither a need to find workarounds to running
tests on NetBSD for the current master.

if we decide later into improving the accuracy of the report, might be
worth documenting some of what I learned in the commit message, for the
future reviewers, who hopefully will be less thick headed.

Carlo

PS. I know I botched the v2 git send-email, so you didn't get a CC, or even
    have the v2 in the subject, hope it would be still good enough but let
    me know otherwise.
Junio C Hamano May 8, 2020, 1:17 a.m. UTC | #5
Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:

> On Thu, May 07, 2020 at 09:52:12PM +0200, Johannes Schindelin wrote:
>> On Wed, 6 May 2020, Carlo Marcelo Arenas Belón wrote:
>> >
>> > Enclose the bash specific code inside an eval to avoid parsing errors
>> > and while at it, simplify the logic so that instead of traversing the
>> > callstack just pop the two topmost entries that are required.
>> 
>> I would be okay with that, but that's not what the patch does:
>
> FWIW that was the intention, but luckily Junio quickly predicted it was
> most likely buggy and so has been since made obsolete by:
>
>   https://lore.kernel.org/git/20200507175706.19986-1-carenas@gmail.com/

Heh, don't give me too much credit.  I just noticed that they cannot
be implementing the same thing, but I couldn't tell if the new
behaviour was something you two agreed to be better, and asked for a
clarification.

In any case, the "just protect with eval '' block to avoid hurting
other shells" version should be the first step.  Improving it
further is a separate topic.

Thanks.
diff mbox series

Patch

diff --git a/t/test-lib.sh b/t/test-lib.sh
index 1b221951a8..60b8e70678 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -677,14 +677,13 @@  die () {
 
 file_lineno () {
 	test -z "$GIT_TEST_FRAMEWORK_SELFTEST" && test -n "$BASH" || return 0
-	local i
-	for i in ${!BASH_SOURCE[*]}
-	do
-		case $i,"${BASH_SOURCE[$i]##*/}" in
-		0,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:$LINENO: ${1+$1: }"; return;;
-		*,t[0-9]*.sh) echo "t/${BASH_SOURCE[$i]}:${BASH_LINENO[$(($i-1))]}: ${1+$1: }"; return;;
-		esac
-	done
+	eval '
+		local n=$(echo ${#BASH_SOURCE[*]})
+		if test $n -ge 2
+		then
+			echo "${BASH_SOURCE[$n - 1]}:${BASH_LINENO[$n - 2]}: $1: "
+		fi
+	'
 }
 
 GIT_EXIT_OK=