[PATCH 1/2] test-lib-emacs: re-define test_emacs() after initialization

Subject: [PATCH 1/2] test-lib-emacs: re-define test_emacs() after initialization

Date: Sat, 22 May 2021 18:20:08 +0300

To: notmuch@notmuchmail.org

Cc: tomi.ollila@iki.fi

From: Tomi Ollila


After test_emacs() has started emacs, it re-defines
itself as just calling the started emacs.

First call to test_emacs() will execute to the end of it;
next calls will just execute the 2 lines, first clearing OUTPUT,
second and last executing TEST_EMACSCLIENT...

This simplifies away one if constructs and reduces xtrace
output when doing test debugging with set -x.
---

This has been laying around like 6 years, every now thinking whether
to post or not. Now needed to rebase so finally polished to a
sendable format. re-defining functions just may be too much
to understand (or not)...

 test/test-lib-emacs.sh | 79 +++++++++++++++++++++++-------------------
 1 file changed, 43 insertions(+), 36 deletions(-)

diff --git a/test/test-lib-emacs.sh b/test/test-lib-emacs.sh
index dde32177..b2b490be 100644
--- a/test/test-lib-emacs.sh
+++ b/test/test-lib-emacs.sh
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2010-2020 Notmuch Developers
+# Copyright (c) 2010-2021 Notmuch Developers
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -166,44 +166,51 @@ EOF
 }
 
 test_emacs () {
-	# test dependencies beforehand to avoid the waiting loop below
+	# Note: This test_emacs() is re-defined after emacs started below.
 	test_require_emacs || return
 
-	if [ -z "$EMACS_SERVER" ]; then
-		emacs_tests="$NOTMUCH_SRCDIR/test/${this_test_bare}.el"
-		if [ -f "$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
-	fi
+	local emacs_tests load_emacs_tests server_name
 
-	# Clear test-output output file.  Most Emacs tests end with a
-	# call to (test-output).  If the test code fails with an
-	# exception before this call, the output file won't get
-	# updated.  Since we don't want to compare against an output
-	# file from another test, so start out with an empty file.
-	rm -f OUTPUT
-	touch OUTPUT
-
-	${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $*)"
+	emacs_tests=$NOTMUCH_SRCDIR/test/${this_test_bare}.el
+	if [ -f "$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
+
+	# re-define test_emacs() -- further calls will use this one
+	test_emacs () {
+		# Clear test-output output file.  Most Emacs tests end with a
+		# call to (test-output).  If the test code fails with an
+		# exception before this call, the output file won't get
+		# updated.  Since we don't want to compare against an output
+		# file from another test, so start out with an empty file.
+		: > OUTPUT
+		${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" \
+			--eval "(notmuch-test-progn $*)"
+	}
+
+	# wait until the emacs server is up (now calling "new" test_emacs())
+	until test_emacs '()' >/dev/null 2>/dev/null; do
+		sleep 1
+	done
+
+	# and finally, call new test_emacs() with given args
+	test_emacs "$@"
 }
 
 emacs_generate_script
-- 
2.25.1

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: