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

[PATCH 4/8] test-lib.sh: optionally output to test-results/$TEST.out, too

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Feb 3, 2009, 23:26 UTC
Message-ID
<alpine.DEB.1.00.0902040026120.9822@pacific.mpi-cbg.de>
In-Reply-To
<cover.1233702893u.git.johannes.schindelin@gmx.de>

When tests are run in parallel and a few tests fail, it does not help that the output of the terminal is totally confusing, as you rarely know which test which line came from.

So introduce the option '--tee' which triggers that the output of the tests will be written to t/test-results/$TEST.out in addition to the terminal, where $TEST is the basename of the script.

Unfortunately, there seems to be no way to redirect a given file descriptor to a specified subprocess in POSIX shell, only redirection to a file is supported via 'exec > $FILE'.

At least with bash, one might think that 'exec >($COMMAND)' would work as intended, but it does not.

The common way to work around the lack of proper tools support is to work with named pipes, alas, one of our most beloved platforms does not really support named pipes. Besides, we would need a pipe for every script, as the whole point of this patch is to allow parallel execution.

Therefore, we handle the redirection in the following way: when '--tee' was passed to the test script, the variable GIT_TEST_TEE_STARTED is set (to avoid triggering that code path again) and the script is started _again_, in a subshell, redirected to the command "tee".

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/README      |    6 ++++++
 t/test-lib.sh |   18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/t/README b/t/README
index 7560db5..ed1ebb6 100644
--- a/t/README
+++ b/t/README
@@ -65,6 +65,12 @@ appropriately before running "make".
 	the test script when running under -i).  Valgrind errors
 	go to stderr, so you might want to pass the -v option, too.
 
+--tee::
+	In addition to printing the test output to the terminal,
+	write it to files named 't/test-results/$TEST_NAME.out'.
+	As the names depend on the tests' file names, it is safe to
+	run the tests with this option in parallel.
+
 Skipping Tests
 --------------
 
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 2567c74..2fb2274 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -3,6 +3,22 @@
 # Copyright (c) 2005 Junio C Hamano
 #
 
+# if --tee was passed, write the output not only to the terminal, but
+# additionally to the file test-results/$BASENAME.out, too.
+case "$GIT_TEST_TEE_STARTED, $* " in
+done,*)
+	# do not redirect again
+	;;
+*' --tee '*)
+	mkdir -p test-results
+	BASE=test-results/$(basename "$0" .sh)
+	(GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
+	 echo $? > $BASE.exit) | tee $BASE.out
+	test "$(cat $BASE.exit)" = 0
+	exit
+	;;
+esac
+
 # Keep the original TERM for say_color
 ORIGINAL_TERM=$TERM
 
@@ -96,6 +112,8 @@ do
 		shift ;;
 	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
 		valgrind=t; shift ;;
+	--tee)
+		shift ;; # was handled already
 	*)
 		break ;;
 	esac
-- 
1.6.1.2.582.g3fdd5
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 4 of 11 in “Add valgrind support in test scripts”
  1. 1/8 Add valgrind support in test scriptsJohannes Schindelin, Feb 3, 2009
  2. 2/8 valgrind: ignore ldso and more libz errorsJohannes Schindelin, Feb 3, 2009
  3. 3/8 Valgrind support: check for more than just programming errorsJohannes Schindelin, Feb 3, 2009
  4. 4/8 test-lib.sh: optionally output to test-results/$TEST.out, tooJohannes Schindelin, Feb 3, 2009
  5. 5/8 t/Makefile: provide a 'valgrind' targetJohannes Schindelin, Feb 3, 2009
  6. 6/8 Add a script to coalesce the valgrind outputsJohannes Schindelin, Feb 3, 2009
  7. 7/8 Tests: let --valgrind imply --verbose and --teeJohannes Schindelin, Feb 3, 2009
  8. Jeff KingFeb 6, 2009
  9. Johannes SchindelinFeb 6, 2009
  10. Jeff KingFeb 6, 2009
  11. 8/8 test-lib: avoid assuming that templates/ are in the GIT_EXEC_PATHJohannes Schindelin, Feb 3, 2009

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.