+ exit 1
+ fi
+}
+
+emacs_generate_script () {
+ # Construct a little test script here for the benefit of the user,
+ # (who can easily run "run_emacs" to get the same emacs environment
+ # for investigating any failures).
+ cat <<EOF >"$TMP_DIRECTORY/run_emacs"
+#!/bin/sh
+export PATH=$PATH
+export NOTMUCH_CONFIG=$NOTMUCH_CONFIG
+
+# Here's what we are using here:
+#
+# --quick Use minimal customization. This implies --no-init-file,
+# --no-site-file and (emacs 24) --no-site-lisp
+#
+# --directory Ensure that the local elisp sources are found
+#
+# --load Force loading of notmuch.el and test-lib.el
+
+exec ${TEST_EMACS} --quick \
+ --directory "$TEST_DIRECTORY/../emacs" --load notmuch.el \
+ --directory "$TEST_DIRECTORY" --load test-lib.el \
+ "\$@"
+EOF
+ chmod a+x "$TMP_DIRECTORY/run_emacs"
+}
+
+test_emacs () {
+ # 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 ${TEST_EMACSCLIENT} || missing_dependencies=1
+ test -z "$missing_dependencies" || return
+
+ if [ -z "$EMACS_SERVER" ]; then
+ emacs_tests="${this_test_bare}.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 (or 'vt100' in case user's TERM is known dumb
+ # or unknown) is given to dtach which assumes a minimally
+ # VT100-compatible terminal -- and emacs inherits that
+ TERM=$SMART_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