[PATCH v2 7/9] test/count: fix numeric comparision tests on FreeBSD

Mike Kelly pioto at pioto.org
Wed May 30 00:02:02 PDT 2012


FreeBSD's `wc -l` includes some white space in front of the number.
Instead, we add a test_expect_equal_num() to test-lib.sh, which ensures
we do a proper numeric comparision, and in a portable way.
---
 test/count       |    8 ++++----
 test/test-lib.sh |   22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/test/count b/test/count
index 300b171..902c7c6 100755
--- a/test/count
+++ b/test/count
@@ -7,22 +7,22 @@ add_email_corpus
 SEARCH="\"*\""
 
 test_begin_subtest "message count is the default for notmuch count"
-test_expect_equal \
+test_expect_equal_num \
     "`notmuch search --output=messages ${SEARCH} | wc -l`" \
     "`notmuch count ${SEARCH}`"
 
 test_begin_subtest "message count with --output=messages"
-test_expect_equal \
+test_expect_equal_num \
     "`notmuch search --output=messages ${SEARCH} | wc -l`" \
     "`notmuch count --output=messages ${SEARCH}`"
 
 test_begin_subtest "thread count with --output=threads"
-test_expect_equal \
+test_expect_equal_num \
     "`notmuch search --output=threads ${SEARCH} | wc -l`" \
     "`notmuch count --output=threads ${SEARCH}`"
 
 test_begin_subtest "thread count is the default for notmuch search"
-test_expect_equal \
+test_expect_equal_num \
     "`notmuch search ${SEARCH} | wc -l`" \
     "`notmuch count --output=threads ${SEARCH}`"
 
diff --git a/test/test-lib.sh b/test/test-lib.sh
index da792b7..2ac92f7 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -512,6 +512,28 @@ test_expect_equal_file ()
     fi
 }
 
+# Like test_expect_equal, but does a numeric comparision instead of a
+# string comparision
+test_expect_equal_num () {
+	exec 1>&6 2>&7		# Restore stdout and stderr
+	inside_subtest=
+	test "$#" = 3 && { prereq=$1; shift; } || prereq=
+	test "$#" = 2 ||
+	error "bug in the test script: not 2 or 3 parameters to test_expect_equal"
+
+	output="$1"
+	expected="$2"
+	if ! test_skip "$test_subtest_name"
+	then
+		if test "$output" -eq "$expected"
+		then
+			test_ok_ "$test_subtest_name"
+		else
+			test_failure_ "$test_subtest_name" "$output != $expected"
+		fi
+	fi
+}
+
 test_emacs_expect_t () {
 	test "$#" = 2 && { prereq=$1; shift; } || prereq=
 	test "$#" = 1 ||
-- 
1.7.10.2



More information about the notmuch mailing list