+ # test dependencies beforehand to avoid the waiting loop below
+ missing_dependencies=
+ test_require_external_prereq dtach || missing_dependencies=1
+ test_require_external_prereq emacs || missing_dependencies=1
+ test_require_external_prereq emacsclient || missing_dependencies=1
+ test -z "$missing_dependencies" || return
+
+ if [ -z "$EMACS_SERVER" ]; then
+ emacs_tests="$(basename $0).el"
+ if [ -f "$TEST_DIRECTORY/$emacs_tests" ]; then
+ load_emacs_tests="--eval '(load \"$emacs_tests\")'"
+ else
+ load_emacs_tests=
+ fi
+ server_name="notmuch-test-suite-$$"
+ # start a detached session with an emacs server
+ # user's TERM is given to dtach which assumes a minimally
+ # VT100-compatible terminal -- and emacs inherits that
+ TERM=$ORIGINAL_TERM dtach -n "$TEST_TMPDIR/emacs-dtach-socket.$$" \
+ sh -c "stty rows 24 cols 80; exec '$TMP_DIRECTORY/run_emacs' \
+ --no-window-system \
+ $load_emacs_tests \
+ --eval '(setq server-name \"$server_name\")' \
+ --eval '(server-start)' \
+ --eval '(orphan-watchdog $$)'" || return
+ EMACS_SERVER="$server_name"
+ # wait until the emacs server is up
+ until test_emacs '()' >/dev/null 2>/dev/null; do
+ sleep 1
+ done
+ fi
+
+ emacsclient --socket-name="$EMACS_SERVER" --eval "(progn $@)"
+}
+
+test_python() {
+ export LD_LIBRARY_PATH=$TEST_DIRECTORY/../lib
+ export PYTHONPATH=$TEST_DIRECTORY/../bindings/python
+
+ # Some distros (e.g. Arch Linux) ship Python 2.* as /usr/bin/python2,
+ # most others as /usr/bin/python. So first try python2, and fallback to
+ # python if python2 doesn't exist.
+ cmd=python2
+ [[ "$test_missing_external_prereq_python2_" = t ]] && cmd=python
+
+ (echo "import sys; _orig_stdout=sys.stdout; sys.stdout=open('OUTPUT', 'w')"; cat) \
+ | $cmd -
+}
+
+# Creates a script that counts how much time it is executed and calls
+# notmuch. $notmuch_counter_command is set to the path to the
+# generated script. Use notmuch_counter_value() function to get the
+# current counter value.
+notmuch_counter_reset () {
+ notmuch_counter_command="$TMP_DIRECTORY/notmuch_counter"
+ if [ ! -x "$notmuch_counter_command" ]; then
+ notmuch_counter_state_path="$TMP_DIRECTORY/notmuch_counter.state"
+ cat >"$notmuch_counter_command" <<EOF || return
+#!/bin/sh
+
+read count < "$notmuch_counter_state_path"
+echo \$((count + 1)) > "$notmuch_counter_state_path"
+
+exec notmuch "\$@"
+EOF
+ chmod +x "$notmuch_counter_command" || return
+ fi
+
+ echo 0 > "$notmuch_counter_state_path"
+}
+
+# Returns the current notmuch counter value.
+notmuch_counter_value () {
+ if [ -r "$notmuch_counter_state_path" ]; then
+ read count < "$notmuch_counter_state_path"
+ else
+ count=0
+ fi
+ echo $count
+}
+
+test_reset_state_ () {
+ test -z "$test_init_done_" && test_init_
+
+ test_subtest_known_broken_=
+ test_subtest_missing_external_prereqs_=
+}
+
+# called once before the first subtest
+test_init_ () {
+ test_init_done_=t
+
+ # skip all tests if there were external prerequisites missing during init
+ test_check_missing_external_prereqs_ "all tests in $this_test" && test_done