t/test-lib.sh: add test_external and test_external_without_stderr
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Lea Wiemann committed
Jun 19, 2008 at 20:18 UTC
fb32c410087e68d650b31f68e66b3d9cbcce4a56
1 file changed
+58
t/test-lib.sh
+58
@@ -302,6 +302,64 @@ test_expect_code () {
302
echo >&3 ""
303
}
304
305
+# test_external runs external test scripts that provide continuous
306
+# test output about their progress, and succeeds/fails on
307
+# zero/non-zero exit code. It outputs the test output on stdout even
308
+# in non-verbose mode, and announces the external script with "* run
309
+# <n>: ..." before running it. When providing relative paths, keep in
310
+# mind that all scripts run in "trash directory".
311
+# Usage: test_external description command arguments...
312
+# Example: test_external 'Perl API' perl ../path/to/test.pl
313
+test_external () {
314
+ test "$#" -eq 3 ||
315
+ error >&5 "bug in the test script: not 3 parameters to test_external"
316
+ descr="$1"
317
+ shift
318
+ if ! test_skip "$descr" "$@"
319
+ then
320
+ # Announce the script to reduce confusion about the
321
+ # test output that follows.
322
+ say_color "" " run $(expr "$test_count" + 1): $descr ($*)"
323
+ # Run command; redirect its stderr to &4 as in
324
+ # test_run_, but keep its stdout on our stdout even in
325
+ # non-verbose mode.
326
+ "$@" 2>&4
327
+ if [ "$?" = 0 ]
328
+ then
329
+ test_ok_ "$descr"
330
+ else
331
+ test_failure_ "$descr" "$@"
332
+ fi
333
+ fi
334
+}
335
+
336
+# Like test_external, but in addition tests that the command generated
337
+# no output on stderr.
338
+test_external_without_stderr () {
339
+ # The temporary file has no (and must have no) security
340
+ # implications.
341
+ tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
342
+ stderr="$tmp/git-external-stderr.$$.tmp"
343
+ test_external "$@" 4> "$stderr"
344
+ [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
345
+ descr="no stderr: $1"
346
+ shift
347
+ say >&3 "expecting no stderr from previous command"
348
+ if [ ! -s "$stderr" ]; then
349
+ rm "$stderr"
350
+ test_ok_ "$descr"
351
+ else
352
+ if [ "$verbose" = t ]; then
353
+ output=`echo; echo Stderr is:; cat "$stderr"`
354
+ else
355
+ output=
356
+ fi
357
+ # rm first in case test_failure exits.
358
+ rm "$stderr"
359
+ test_failure_ "$descr" "$@" "$output"
360
+ fi
361
+}
362
+
363
# This is not among top-level (test_expect_success | test_expect_failure)
364
# but is a prefix that can be used in the test script, like:
365
#