[PATCH] test: add "set -u" to test-lib.sh

Tomi Ollila tomi.ollila at iki.fi
Thu Sep 3 12:09:55 PDT 2015


This prohibits unset variables to expand to empty strings.
Without this e.g misspelled variables cause unintentional results.

Now all the test variables are either initialized, or in case of
optional arguments and user-provided environment variables the
form ${variable-} is used.

Two unusable lines (leftovers?) in T360-symbol-hiding.sh were removed;
this was the only non-lib test code that needed to be edited for this
change to work.
---
 test/T360-symbol-hiding.sh |  3 ---
 test/test-lib.sh           | 63 ++++++++++++++++++++++++++--------------------
 2 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/test/T360-symbol-hiding.sh b/test/T360-symbol-hiding.sh
index 4ec0ea6..1e6814b 100755
--- a/test/T360-symbol-hiding.sh
+++ b/test/T360-symbol-hiding.sh
@@ -23,9 +23,6 @@ caught No chert database found at path \`CWD/nonexistent'
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
-test_begin_subtest 'checking output'
-test_expect_equal "$result" "$output"
-
 test_begin_subtest 'comparing existing to exported symbols'
 nm -P $TEST_DIRECTORY/../lib/libnotmuch.so | awk '$2 == "T" && $1 ~ "^notmuch" {print $1}' | sort | uniq > ACTUAL
 sed -n 's/[[:blank:]]*\(notmuch_[^;]*\);/\1/p' $TEST_DIRECTORY/../notmuch.sym | sort | uniq > EXPORTED
diff --git a/test/test-lib.sh b/test/test-lib.sh
index 126911f..c85d149 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -22,6 +22,9 @@ if [ ${BASH_VERSINFO[0]} -lt 4 ]; then
     exit 1
 fi
 
+# Make sure misspelled variables are not to be expanded to empty strings.
+set -u
+
 # Make sure echo builtin does not expand backslash-escape sequences by default.
 shopt -u xpg_echo
 
@@ -31,7 +34,7 @@ this_test_bare=${this_test#T[0-9][0-9][0-9]-}
 
 # if --tee was passed, write the output not only to the terminal, but
 # additionally to the file test-results/$BASENAME.out, too.
-case "$GIT_TEST_TEE_STARTED, $* " in
+case "${GIT_TEST_TEE_STARTED-}, $* " in
 done,*)
 	# do not redirect again
 	;;
@@ -66,8 +69,8 @@ TZ=UTC
 TERM=dumb
 export LANG LC_ALL PAGER TERM TZ
 GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
-if [[ ( -n "$TEST_EMACS" && -z "$TEST_EMACSCLIENT" ) || \
-      ( -z "$TEST_EMACS" && -n "$TEST_EMACSCLIENT" ) ]]; then
+if [[ ( -n "${TEST_EMACS-}" && -z "${TEST_EMACSCLIENT-}" ) || \
+      ( -z "${TEST_EMACS-}" && -n "${TEST_EMACSCLIENT-}" ) ]]; then
     echo "error: must specify both or neither of TEST_EMACS and TEST_EMACSCLIENT" >&2
     exit 1
 fi
@@ -98,6 +101,9 @@ _x32="$_x04$_x04$_x04$_x04$_x04$_x04$_x04$_x04"
 # '
 # . ./test-lib.sh || exit 1
 
+debug= immediate= GIT_TEST_LONG= help= verbose= quiet= with_dashes=
+color= valgrind= verbose= root=
+
 [ "x$ORIGINAL_TERM" != "xdumb" ] && (
 		TERM=$ORIGINAL_TERM &&
 		export TERM &&
@@ -208,7 +214,7 @@ print_test_description ()
 	echo $this_test: "Testing ${test_description}"
 	test_description_printed=1
 }
-if [ -z "$NOTMUCH_TEST_QUIET" ]
+if [ -z "${NOTMUCH_TEST_QUIET-}" ]
 then
 	print_test_description
 fi
@@ -331,6 +337,7 @@ remove_cr () {
 gen_msg_cnt=0
 gen_msg_filename=""
 gen_msg_id=""
+test_subtest_name=
 generate_message ()
 {
     # This is our (bash-specific) magic for doing named parameters
@@ -338,38 +345,38 @@ generate_message ()
     local additional_headers
 
     gen_msg_cnt=$((gen_msg_cnt + 1))
-    if [ -z "${template[filename]}" ]; then
+    if [ -z "${template[filename]-}" ]; then
 	gen_msg_name="msg-$(printf "%03d" $gen_msg_cnt)"
     else
 	gen_msg_name=${template[filename]}
     fi
 
-    if [ -z "${template[id]}" ]; then
+    if [ -z "${template[id]-}" ]; then
 	gen_msg_id="${gen_msg_name%:2,*}@notmuch-test-suite"
     else
 	gen_msg_id="${template[id]}"
     fi
 
-    if [ -z "${template[dir]}" ]; then
+    if [ -z "${template[dir]-}" ]; then
 	gen_msg_filename="${MAIL_DIR}/$gen_msg_name"
     else
 	gen_msg_filename="${MAIL_DIR}/${template[dir]}/$gen_msg_name"
 	mkdir -p "$(dirname "$gen_msg_filename")"
     fi
 
-    if [ -z "${template[body]}" ]; then
+    if [ -z "${template[body]-}" ]; then
 	template[body]="This is just a test message (#${gen_msg_cnt})"
     fi
 
-    if [ -z "${template[from]}" ]; then
+    if [ -z "${template[from]-}" ]; then
 	template[from]="Notmuch Test Suite <test_suite at notmuchmail.org>"
     fi
 
-    if [ -z "${template[to]}" ]; then
+    if [ -z "${template[to]-}" ]; then
 	template[to]="Notmuch Test Suite <test_suite at notmuchmail.org>"
     fi
 
-    if [ -z "${template[subject]}" ]; then
+    if [ -z "${template[subject]-}" ]; then
 	if [ -n "$test_subtest_name" ]; then
 	    template[subject]="$test_subtest_name"
 	else
@@ -379,7 +386,7 @@ generate_message ()
 	template[subject]=""
     fi
 
-    if [ -z "${template[date]}" ]; then
+    if [ -z "${template[date]-}" ]; then
 	# we use decreasing timestamps here for historical reasons;
 	# the existing test suite when we converted to unique timestamps just
 	# happened to have signicantly fewer failures with that choice.
@@ -392,42 +399,42 @@ generate_message ()
     fi
 
     additional_headers=""
-    if [ ! -z "${template[header]}" ]; then
+    if [ ! -z "${template[header]-}" ]; then
 	additional_headers="${template[header]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[reply-to]}" ]; then
+    if [ ! -z "${template[reply-to]-}" ]; then
 	additional_headers="Reply-To: ${template[reply-to]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[in-reply-to]}" ]; then
+    if [ ! -z "${template[in-reply-to]-}" ]; then
 	additional_headers="In-Reply-To: ${template[in-reply-to]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[cc]}" ]; then
+    if [ ! -z "${template[cc]-}" ]; then
 	additional_headers="Cc: ${template[cc]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[bcc]}" ]; then
+    if [ ! -z "${template[bcc]-}" ]; then
 	additional_headers="Bcc: ${template[bcc]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[references]}" ]; then
+    if [ ! -z "${template[references]-}" ]; then
 	additional_headers="References: ${template[references]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[content-type]}" ]; then
+    if [ ! -z "${template[content-type]-}" ]; then
 	additional_headers="Content-Type: ${template[content-type]}
 ${additional_headers}"
     fi
 
-    if [ ! -z "${template[content-transfer-encoding]}" ]; then
+    if [ ! -z "${template[content-transfer-encoding]-}" ]; then
 	additional_headers="Content-Transfer-Encoding: ${template[content-transfer-encoding]}
 ${additional_headers}"
     fi
@@ -545,6 +552,7 @@ add_email_corpus ()
     fi
 }
 
+inside_subtest=
 test_begin_subtest ()
 {
     if [ -n "$inside_subtest" ]; then
@@ -775,7 +783,7 @@ $binary () {
 # Returns success if dependency is available, failure otherwise.
 test_require_external_prereq () {
 	binary="$1"
-	if [[ ${test_missing_external_prereq_["${binary}"]} == t ]]; then
+	if [[ ${test_missing_external_prereq_["${binary}"]-} == t ]]; then
 		# dependency is missing, call the replacement function to note it
 		eval "$binary"
 	else
@@ -792,7 +800,7 @@ test_ok_ () {
 		return
 	fi
 	test_success=$(($test_success + 1))
-	if test -n "$NOTMUCH_TEST_QUIET"; then
+	if test -n "${NOTMUCH_TEST_QUIET-}"; then
 		return 0
 	fi
 	say_color pass "%-6s" "PASS"
@@ -849,7 +857,7 @@ test_run_ () {
 test_skip () {
 	test_count=$(($test_count+1))
 	to_skip=
-	for skp in $NOTMUCH_SKIP_TESTS
+	for skp in ${NOTMUCH_SKIP_TESTS-}
 	do
 		case $this_test.$test_count in
 		$skp)
@@ -1109,7 +1117,7 @@ exec ${TEST_EMACS} --quick \
 EOF
 	chmod a+x "$TMP_DIRECTORY/run_emacs"
 }
-
+EMACS_SERVER=
 test_emacs () {
 	# test dependencies beforehand to avoid the waiting loop below
 	missing_dependencies=
@@ -1213,6 +1221,7 @@ notmuch_counter_value () {
 	echo $count
 }
 
+test_init_done_=
 test_reset_state_ () {
 	test -z "$test_init_done_" && test_init_
 
@@ -1245,7 +1254,7 @@ else
 	exec 4>test.output 3>&4
 fi
 
-for skp in $NOTMUCH_SKIP_TESTS
+for skp in ${NOTMUCH_SKIP_TESTS-}
 do
 	to_skip=
 	for skp in $NOTMUCH_SKIP_TESTS
@@ -1312,8 +1321,8 @@ case $(uname -s) in
 	;;
 esac
 
-test -z "$NO_PERL" && test_set_prereq PERL
-test -z "$NO_PYTHON" && test_set_prereq PYTHON
+test -z "${NO_PERL-}" && test_set_prereq PERL
+test -z "${NO_PYTHON-}" && test_set_prereq PYTHON
 
 # test whether the filesystem supports symbolic links
 ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
-- 
1.9.3



More information about the notmuch mailing list