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

[PATCH 1/2 v3] t/test-lib.sh: add test_external and test_external_without_stderr

From
LWLea Wiemann <lewiemann@gmail.com>
Date
Jun 19, 2008, 18:18 UTC
Message-ID
<d5ac06cabb7eb235ca82525fad2e96cdab205469.1213899000.git.LeWiemann@gmail.com>
In-Reply-To
<48596EE7.90202@free.fr>

This is for running external test scripts in other programming languages that provide continuous output about their tests. Using test_expect_success (like "test_expect_success 'description' 'perl test-script.pl'") doesn't suffice here because test_expect_success eats stdout in non-verbose mode, which is not fixable without major file descriptor trickery.

Signed-off-by: Lea Wiemann <LeWiemann@gmail.com>
---
Olivier Marin wrote:
> Just a typo here: s/eror/error/

Thanks for spotting this, and also the missing ampersand in the other patch!

(This typo is the only change since v2.)
 t/test-lib.sh |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 3ac8755..dc2736e 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -304,6 +304,64 @@ test_expect_code () {
 	echo >&3 ""
 }
 
+# test_external runs external test scripts that provide continuous
+# test output about their progress, and succeeds/fails on
+# zero/non-zero exit code.  It outputs the test output on stdout even
+# in non-verbose mode, and announces the external script with "* run
+# <n>: ..." before running it.  When providing relative paths, keep in
+# mind that all scripts run in "trash directory".
+# Usage: test_external description command arguments...
+# Example: test_external 'Perl API' perl ../path/to/test.pl
+test_external () {
+	test "$#" -eq 3 ||
+	error >&5 "bug in the test script: not 3 parameters to test_external"
+	descr="$1"
+	shift
+	if ! test_skip "$descr" "$@"
+	then
+		# Announce the script to reduce confusion about the
+		# test output that follows.
+		say_color "" " run $(expr "$test_count" + 1): $descr ($*)"
+		# Run command; redirect its stderr to &4 as in
+		# test_run_, but keep its stdout on our stdout even in
+		# non-verbose mode.
+		"$@" 2>&4
+		if [ "$?" = 0 ]
+		then
+			test_ok_ "$descr"
+		else
+			test_failure_ "$descr" "$@"
+		fi
+	fi
+}
+
+# Like test_external, but in addition tests that the command generated
+# no output on stderr.
+test_external_without_stderr () {
+	# The temporary file has no (and must have no) security
+	# implications.
+	tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
+	stderr="$tmp/git-external-stderr.$$.tmp"
+	test_external "$@" 4> "$stderr"
+	[ -f "$stderr" ] || error "Internal error: $stderr disappeared."
+	descr="no stderr: $1"
+	shift
+	say >&3 "expecting no stderr from previous command"
+	if [ ! -s "$stderr" ]; then
+		rm "$stderr"
+		test_ok_ "$descr"
+	else
+		if [ "$verbose" = t ]; then
+			output=`echo; echo Stderr is:; cat "$stderr"`
+		else
+			output=
+		fi
+		# rm first in case test_failure exits.
+		rm "$stderr"
+		test_failure_ "$descr" "$@" "$output"
+	fi
+}
+
 # This is not among top-level (test_expect_success | test_expect_failure)
 # but is a prefix that can be used in the test script, like:
 #
-- 
1.5.6.149.g06c04.dirty
Previous: Olivier MarinNext: Lea Wiemann
Message 8 of 13 in “t/test-lib.sh: add test_external and test_external_without_stderr”
  1. 1/2 t/test-lib.sh: add test_external and test_external_without_stderrLea Wiemann, Jun 17, 2008
  2. 2/2 Git.pm: add test suiteLea Wiemann, Jun 17, 2008
  3. Junio C HamanoJun 18, 2008
  4. Lea WiemannJun 18, 2008
  5. Junio C HamanoJun 18, 2008
  6. Olivier MarinJun 18, 2008
  7. Olivier MarinJun 18, 2008
  8. 1/2 t/test-lib.sh: add test_external and test_external_without_stderrLea Wiemann, Jun 19, 2008
  9. 2/2 Git.pm: add test suiteLea Wiemann, Jun 19, 2008
  10. 2/2 Git.pm: add test suiteLea Wiemann, Jun 19, 2008
  11. Jakub NarebskiJun 19, 2008
  12. Lea WiemannJun 19, 2008
  13. Junio C HamanoJun 19, 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.