git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[HACK] t/test-lib.sh HACK: Add -s/--show-hack to test suite.

From
Stephan Beyer <s-beyer@gmx.net>
Date
Jul 6, 2008, 20:22 UTC
Message-ID
<1215375751-30853-1-git-send-email-s-beyer@gmx.net>

This option realizes a stupid hack that tries to run the test cases line by line (separated by &&). Furthermore it shows the line it is testing. With that information it is easier to find the reason why a test fails.

This hack works as long as there are no multi-line for/while/subshell/... in the test cases.

Note, that the -s option should only be used if a test case failed. It is slow and error-prone.

Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
---
Hi,

I wrote that before I started to make the sequencer prototype and then I used this hack more than expected ;-) Today I cherry-picked this commit into another branch and then I thought this could be useful for others, too. (Explicitly not for inclusion!)

So how to use it? When running a test case in t/, add the -s option and then it shows something like this:

--snip--[...]
-------
Testing:
        ! test -d "$SEQDIR"
-------
Testing:
        session_ok
* FAIL 3: "pick", "squash", "ref" from stdin
                next_session squashCE &&
                valgrind git sequencer <todotest1 &&
                ! test -d "$SEQDIR" &&
                session_ok &&
                test -f file2 &&
                test -f file3 &&
                test $(git rev-parse CE) = $(git rev-parse HEAD) &&
                test $(git rev-parse I) = $(git rev-parse HEAD^)
--snap--
Because of the "Testing:" lines you can explicitly see where it failed.
Regards,
  Stephan
 t/test-lib.sh |   45 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index c0c5e0e..6f42106 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -86,6 +86,8 @@ do
 		help=t; shift ;;
 	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
 		verbose=t; shift ;;
+	-s|--show-hack)
+		verbose=t; show_hack=t; immediate=t; shift ;;
 	-q|--q|--qu|--qui|--quie|--quiet)
 		quiet=t; shift ;;
 	--no-color)
@@ -225,9 +227,48 @@ test_debug () {
 	test "$debug" = "" || eval "$1"
 }
 
-test_run_ () {
-	eval >&3 2>&4 "$1"
+test_run__ () {
+	eval "$1"
 	eval_ret="$?"
+}
+
+test_run_op_ () {
+	echo "Testing:"
+	printf "%s\n" "$op_"
+	echo
+	eval "$op_"
+	eval_ret="$?"
+}
+
+test_run_hack_ () {
+	i_=1
+	j_=1
+	total_=$(printf '%s' "$1" | wc -l)
+	while test "$j_" -lt $(expr "$total_" + 1)
+	do
+		op_=$(printf '%s' "$1" | sed -n -e "$i_,$j_ p")
+		if test -n "$(printf '%s' "$op_" | sed -n -e '/<<[-\\ A-Za-z]/q;/&& *$/p;')"
+		then
+			i_=$(expr "$j_" + 1)
+			op_=$(printf '%s' "$op_" | sed -e 's/ *&& *$//')
+			test_run_op_
+			test "$eval_ret" -ne 0 && return 0
+			echo -------
+		fi
+		j_=$(expr "$j_" + 1)
+	done
+	op_="$(printf '%s' "$1" | sed -n -e "$i_,$j_ p")"
+	test_run_op_
+	return 0
+}
+
+test_run_ () {
+	if test -z "$show_hack"
+	then
+		test_run__ >&3 2>&4 "$1"
+	else
+		test_run_hack_ >&3 2>&4 "$1"
+	fi
 	return 0
 }
 
-- 
1.5.6.363.g7ba71
Next: Johannes Schindelin
Message 1 of 9 in “[HACK] t/test-lib.sh HACK: Add -s/--show-hack to test suite.”
  1. Stephan BeyerJul 6, 2008
  2. Johannes SchindelinJul 6, 2008
  3. Stephan BeyerJul 7, 2008
  4. Johannes SchindelinJul 7, 2008
  5. Stephan BeyerJul 12, 2008
  6. Johannes SchindelinJul 13, 2008
  7. Stephan BeyerJul 13, 2008
  8. Johannes SchindelinJul 13, 2008
  9. Stephan BeyerJul 21, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.