[PATCH] test: Detect when some command unexpectedly fails
authorMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 21 Nov 2010 12:08:12 +0000 (13:08 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:37:34 +0000 (09:37 -0800)
d9/9585c8ca79052e303d5bb448d5b7cec9ebea47 [new file with mode: 0644]

diff --git a/d9/9585c8ca79052e303d5bb448d5b7cec9ebea47 b/d9/9585c8ca79052e303d5bb448d5b7cec9ebea47
new file mode 100644 (file)
index 0000000..b094774
--- /dev/null
@@ -0,0 +1,125 @@
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id D373040DF1D\r
+       for <notmuch@notmuchmail.org>; Sun, 21 Nov 2010 04:08:28 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.9\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.9 tagged_above=-999 required=5\r
+       tests=[BAYES_00=-1.9] autolearn=ham\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id 8UQWKIisO9yL for <notmuch@notmuchmail.org>;\r
+       Sun, 21 Nov 2010 04:08:18 -0800 (PST)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+       by olra.theworths.org (Postfix) with ESMTP id 312B440DF1C\r
+       for <notmuch@notmuchmail.org>; Sun, 21 Nov 2010 04:08:18 -0800 (PST)\r
+Received: from localhost (unknown [192.168.200.4])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id 2344719F3340;\r
+       Sun, 21 Nov 2010 13:08:17 +0100 (CET)\r
+X-Virus-Scanned: IMAP AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+       by localhost (styx.feld.cvut.cz [192.168.200.4]) (amavisd-new,\r
+       port 10044)\r
+       with ESMTP id p+QNtAy490Gi; Sun, 21 Nov 2010 13:08:15 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+       by max.feld.cvut.cz (Postfix) with ESMTP id E417919F30E1;\r
+       Sun, 21 Nov 2010 13:08:15 +0100 (CET)\r
+Received: from wsheee.2x.cz (unknown [213.29.198.144])\r
+       (Authenticated sender: sojkam1)\r
+       by imap.feld.cvut.cz (Postfix) with ESMTPSA id A395FFA003;\r
+       Sun, 21 Nov 2010 13:08:14 +0100 (CET)\r
+Received: from wsh by wsheee.2x.cz with local (Exim 4.72)\r
+       (envelope-from <sojkam1@fel.cvut.cz>)\r
+       id 1PK8iM-0004K4-81; Sun, 21 Nov 2010 13:08:14 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] test: Detect when some command unexpectedly fails\r
+Date: Sun, 21 Nov 2010 13:08:12 +0100\r
+Message-Id: <1290341292-16589-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 1.7.1.90.g94d6d4\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 21 Nov 2010 12:08:29 -0000\r
+\r
+When some command in between test_begin_subtest and test_expect_equal\r
+fails, the test suite does not detect it unless the failure leads to\r
+difference in the strings compared by test_expect_equal.\r
+\r
+This patch sets shell -e option which switches on the detection of\r
+failed commands (thise with non-zero exit status). If some command fails\r
+the test script exits with exit code of 1 and prints the failed command.\r
+It might be better to print the line number of the failed command but it\r
+seems that inside traps, the BASH_LINENO variable does not contain the\r
+wanted information.\r
+---\r
+ test/raw         |    2 +-\r
+ test/test-lib.sh |    5 ++++-\r
+ 2 files changed, 5 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/test/raw b/test/raw\r
+index 4ed237c..87e18b1 100755\r
+--- a/test/raw\r
++++ b/test/raw\r
+@@ -10,7 +10,7 @@ output=$(NOTMUCH_NEW)\r
+ test_expect_equal "$output" "Added 2 new messages to the database."\r
\r
+ test_begin_subtest "Attempt to show multiple raw messages"\r
+-output=$(notmuch show --format=raw "*" 2>&1)\r
++output=$(notmuch show --format=raw "*" 2>&1 || :)\r
+ test_expect_equal "$output" "Error: search term did not match precisely one message."\r
\r
+ test_begin_subtest "Show a raw message"\r
+diff --git a/test/test-lib.sh b/test/test-lib.sh\r
+index 04a4c14..1ad0da6 100644\r
+--- a/test/test-lib.sh\r
++++ b/test/test-lib.sh\r
+@@ -178,7 +178,7 @@ die () {\r
+       then\r
+               exit $code\r
+       else\r
+-              echo >&5 "FATAL: Unexpected exit with code $code"\r
++              echo >&5 "FATAL: Unexpected exit with code $code: $BASH_COMMAND"\r
+               exit 1\r
+       fi\r
+ }\r
+@@ -407,6 +407,7 @@ test_begin_subtest ()\r
+     if test "$verbose" != "t"; then exec 4>test.output 3>&4; fi\r
+     exec 6>&1 7>&2 >&3 2>&4\r
+     inside_subtest=t\r
++    set -e\r
+ }\r
\r
+ # Pass test if two arguments match\r
+@@ -418,6 +419,7 @@ test_begin_subtest ()\r
+ test_expect_equal ()\r
+ {\r
+       exec 1>&6 2>&7          # Restore stdout and stderr\r
++      set +e\r
+       inside_subtest=\r
+       test "$#" = 3 && { prereq=$1; shift; } || prereq=\r
+       test "$#" = 2 ||\r
+@@ -440,6 +442,7 @@ test_expect_equal ()\r
\r
+ test_expect_equal_failure ()\r
+ {\r
++      set +e\r
+       test "$#" = 3 && { prereq=$1; shift; } || prereq=\r
+       test "$#" = 2 ||\r
+       error "bug in the test script: not 2 or 3 parameters to test_expect_equal"\r
+-- \r
+1.7.1.90.g94d6d4\r
+\r