Skip to contentSkip to conversation

t/test_lib: avoid naked bash arrays in file_lineno

PATCH In review Carlo Marcelo Arenas Belón 3 participants 8 messages May 7, 2020 – May 8, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 t/test_lib: avoid naked bash arrays in file_lineno

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.

---
 t/test-lib.sh | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
t/test-lib.sh +7 -8
index 1b221951a8..60b8e70678 100644
@@ -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=
Signature · 1 line
2.26.2.686.gfaf46a9ccd
#1 lore raw
Quoted 8 lines · Carlo Marcelo Arenas Belón

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:

Quoted 8 lines

** 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.

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.

Quoted 31 lines
---
 t/test-lib.sh | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
t/test-lib.sh +7 -8
index 1b221951a8..60b8e70678 100644
@@ -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=
#2 lore raw
Quoted 14 lines · Junio C Hamano
Quoted 11 lines · Carlo Marcelo Arenas Belón

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

Quoted 8 lines
Quoted 5 lines

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.

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.

Quoted 4 lines

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.

Quoted 6 lines

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

#3 lore raw

PATCH 1/1 t/test_lib: avoid naked bash arrays in file_lineno

In reply to Carlo Marcelo Arenas Belón

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 uses shell arrays 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 in the same way than 5826b7b595 (test-lib: check Bash version for '-x' without using shell arrays, 2019-01-03)

---
 t/test-lib.sh | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
t/test-lib.sh +10 -8
index 1b221951a8..baf94546da 100644
@@ -677,14 +677,16 @@ 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 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
+	'
 }
 
 GIT_EXIT_OK=
Signature · 1 line
2.26.2.717.g5cccb0e1a8
#4 lore raw

Hi Carlo,

this is v2, right? The subject says [PATCH], not [PATCH v2], so I am just trying to make sure.

It's things like this why I never use git send-email manually anymore. Yes, sure, GitGitGadget helps others, too, but its initial primary goal was to help me not botch my patch submissions.

Quoted 8 lines · Carlo Marcelo Arenas Belón

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 uses shell arrays and is therefore protected by a guard but NetBSD sh will still have to parse the function and therefore will result in:

Quoted 8 lines

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

Enclose the bash specific code inside an eval to avoid parsing errors in the same way than 5826b7b595 (test-lib: check Bash version for '-x' without using shell arrays, 2019-01-03)

I still provided help, I hope?

Quoted 20 lines
---
 t/test-lib.sh | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
t/test-lib.sh +0 -8
index 1b221951a8..baf94546da 100644
@@ -677,14 +677,16 @@ 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

t/test-lib.sh die () { · line 689

Quoted 10 lines
t/test-lib.sh +10 -0
+	eval '
+		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
+	'

This one looks correct to me.

Ciao,
Dscho

Quoted 7 lines
t/test-lib.sh +0 -1
 }
 
 GIT_EXIT_OK=
--

2.26.2.717.g5cccb0e1a8

#5 lore raw
In reply to Carlo Marcelo Arenas Belón

Hi Carlo,

Quoted 8 lines · Carlo Marcelo Arenas Belón

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:

Quoted 6 lines

** 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:

Quoted 12 lines
---
 t/test-lib.sh | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
t/test-lib.sh +0 -0
index 1b221951a8..60b8e70678 100644
@@ -677,14 +677,13 @@ die () {
 

t/test-lib.sh die () { · line 687

Quoted 10 lines
t/test-lib.sh +0 -8
 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?

t/test-lib.sh die () { · line 685

Quoted 6 lines
t/test-lib.sh +6 -0
+	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

Quoted 8 lines
t/test-lib.sh +1 -1
+	'
 }
 
 GIT_EXIT_OK=
--

2.26.2.686.gfaf46a9ccd

#6 lore raw
Quoted 7 lines · Johannes Schindelin
Quoted 4 lines · Carlo Marcelo Arenas Belón

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/[email protected]/

Quoted 9 lines

t/test-lib.sh die () { · line 685

Quoted 6 lines
t/test-lib.sh +6 -0
+	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.

Quoted 7 lines

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.

Quoted 1 line

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.

Quoted 8 lines

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.

#7 lore raw
Quoted 7 lines · Carlo Marcelo Arenas Belón
Quoted 6 lines · Johannes Schindelin
Quoted 4 lines · Carlo Marcelo Arenas Belón

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.

Quoted 6 lines
Quoted 1 line

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/[email protected]/

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.