[PATCH] test: Detect when some command unexpectedly fails

Michal Sojka sojkam1 at fel.cvut.cz
Sun Nov 21 04:08:12 PST 2010


When some command in between test_begin_subtest and test_expect_equal
fails, the test suite does not detect it unless the failure leads to
difference in the strings compared by test_expect_equal.

This patch sets shell -e option which switches on the detection of
failed commands (thise with non-zero exit status). If some command fails
the test script exits with exit code of 1 and prints the failed command.
It might be better to print the line number of the failed command but it
seems that inside traps, the BASH_LINENO variable does not contain the
wanted information.
---
 test/raw         |    2 +-
 test/test-lib.sh |    5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/test/raw b/test/raw
index 4ed237c..87e18b1 100755
--- a/test/raw
+++ b/test/raw
@@ -10,7 +10,7 @@ output=$(NOTMUCH_NEW)
 test_expect_equal "$output" "Added 2 new messages to the database."
 
 test_begin_subtest "Attempt to show multiple raw messages"
-output=$(notmuch show --format=raw "*" 2>&1)
+output=$(notmuch show --format=raw "*" 2>&1 || :)
 test_expect_equal "$output" "Error: search term did not match precisely one message."
 
 test_begin_subtest "Show a raw message"
diff --git a/test/test-lib.sh b/test/test-lib.sh
index 04a4c14..1ad0da6 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -178,7 +178,7 @@ die () {
 	then
 		exit $code
 	else
-		echo >&5 "FATAL: Unexpected exit with code $code"
+		echo >&5 "FATAL: Unexpected exit with code $code: $BASH_COMMAND"
 		exit 1
 	fi
 }
@@ -407,6 +407,7 @@ test_begin_subtest ()
     if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi
     exec 6>&1 7>&2 >&3 2>&4
     inside_subtest=t
+    set -e
 }
 
 # Pass test if two arguments match
@@ -418,6 +419,7 @@ test_begin_subtest ()
 test_expect_equal ()
 {
 	exec 1>&6 2>&7		# Restore stdout and stderr
+	set +e
 	inside_subtest=
 	test "$#" = 3 && { prereq=$1; shift; } || prereq=
 	test "$#" = 2 ||
@@ -440,6 +442,7 @@ test_expect_equal ()
 
 test_expect_equal_failure ()
 {
+	set +e
 	test "$#" = 3 && { prereq=$1; shift; } || prereq=
 	test "$#" = 2 ||
 	error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
-- 
1.7.1.90.g94d6d4



More information about the notmuch mailing list