git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] t/test_lib: avoid naked bash arrays in file_lineno
@ 2020-05-07  5:51 Carlo Marcelo Arenas Belón
  2020-05-07 14:53 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2020-05-07  5:51 UTC (permalink / raw)
  To: git
  Cc: congdanhqx, gitster, Carlo Marcelo Arenas Belón,
	Johannes Schindelin

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(-)

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=
-- 
2.26.2.686.gfaf46a9ccd


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

end of thread, other threads:[~2020-05-08 23:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-07  5:51 [PATCH] t/test_lib: avoid naked bash arrays in file_lineno Carlo Marcelo Arenas Belón
2020-05-07 14:53 ` Junio C Hamano
2020-05-07 17:33   ` Carlo Arenas
2020-05-07 17:57 ` Carlo Marcelo Arenas Belón
2020-05-08 23:53   ` Johannes Schindelin
2020-05-07 19:52 ` Johannes Schindelin
2020-05-08  0:58   ` Carlo Marcelo Arenas Belón
2020-05-08  1:17     ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).