--- /dev/null
+Return-Path: <dmitry.kurochkin@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 59724421175\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Jun 2011 21:45:55 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id AK08Hwze01Xs for <notmuch@notmuchmail.org>;\r
+ Mon, 27 Jun 2011 21:45:53 -0700 (PDT)\r
+Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com\r
+ [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id CCFBC429E59\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Jun 2011 21:45:50 -0700 (PDT)\r
+Received: by mail-bw0-f53.google.com with SMTP id 12so4889638bwg.26\r
+ for <notmuch@notmuchmail.org>; Mon, 27 Jun 2011 21:45:50 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
+ h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer\r
+ :in-reply-to:references;\r
+ bh=4CzoeJ84PeL+RzqWxiAO1o9+PivJZuy6ndSPq1ABK5c=;\r
+ b=LCmLMtjuGaeMLAV9ycLdQYJQwe6rCcUSd5GFbAvHEP7mQDy+RXsJbkmJlq0GqZNm/B\r
+ AWgC5MGvRdpQ8ysL6i3ZLE2CuDvGBvoPfE97qxIXg86lyvRBKrEEhpWtATKv8OG6+N5H\r
+ a+3C/QTWlg84dKhIOqxjJACSCBoCChsGb718g=\r
+DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+ b=UWAJMJARZ4GKob9/Q8vg+0YWjp84D3ohDutlub+XGxsU/2fVhT/22wYdUqq3MkgWie\r
+ zLJeHAGwgwVHjrMHSYobuRJ0oSPZPAxyd9P/PQxusG+5VkOYd0tDAmfE5zrW49qmrHxo\r
+ fdcJqCLPwzQFI/G5rhfRIpJ9dAEIPisYQ2bVY=\r
+Received: by 10.204.136.1 with SMTP id p1mr5207274bkt.105.1309236350509;\r
+ Mon, 27 Jun 2011 21:45:50 -0700 (PDT)\r
+Received: from localhost ([91.144.186.21])\r
+ by mx.google.com with ESMTPS id ek1sm4746053bkb.9.2011.06.27.21.45.49\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Mon, 27 Jun 2011 21:45:49 -0700 (PDT)\r
+From: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 10/10] test: use emacsclient(1) for Emacs tests\r
+Date: Tue, 28 Jun 2011 08:45:11 +0400\r
+Message-Id: <1309236311-2162-10-git-send-email-dmitry.kurochkin@gmail.com>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1309236311-2162-1-git-send-email-dmitry.kurochkin@gmail.com>\r
+References: <1309236311-2162-1-git-send-email-dmitry.kurochkin@gmail.com>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 28 Jun 2011 04:45:55 -0000\r
+\r
+Before the change, every Emacs test ran in a separate Emacs\r
+instance. Starting Emacs many times wastes considerable time and\r
+it gets worse as the test suite grows. The patch solves this by\r
+using a single Emacs server and emacsclient(1) to run multiple\r
+tests. Emacs server is started on the first test_emacs call and\r
+stopped when test_done is called. We take care not to leave\r
+orphan Emacs processes behind when test is terminated by whatever\r
+reason: Emacs server runs a watchdog that periodically checks\r
+that the test is still running.\r
+\r
+Some tests need to provide user input. Before the change, this\r
+was done using echo(1) to Emacs stdin. This no longer works and\r
+instead `standard-input' variable is set accordingly to make\r
+`read' return the appropriate string.\r
+---\r
+ test/emacs | 10 +++++-----\r
+ test/test-lib.el | 13 +++++++++++++\r
+ test/test-lib.sh | 24 +++++++++++++-----------\r
+ 3 files changed, 31 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/test/emacs b/test/emacs\r
+index 3d42645..0f5f99c 100755\r
+--- a/test/emacs\r
++++ b/test/emacs\r
+@@ -259,15 +259,15 @@ test_expect_equal_file OUTPUT EXPECTED\r
+ \r
+ test_begin_subtest "Save attachment from within emacs using notmuch-show-save-attachments"\r
+ # save as archive to test that Emacs does not re-compress .gz\r
+-echo ./attachment1.gz |\r
+-test_emacs '(notmuch-show "id:cf0c4d610911171136h1713aa59w9cf9aa31f052ad0a@mail.gmail.com")\r
+- (notmuch-show-save-attachments)' > /dev/null 2>&1\r
++test_emacs '(let ((standard-input "\"attachment1.gz\""))\r
++ (notmuch-show "id:cf0c4d610911171136h1713aa59w9cf9aa31f052ad0a@mail.gmail.com")\r
++ (notmuch-show-save-attachments))' > /dev/null 2>&1\r
+ test_expect_equal_file attachment1.gz "$EXPECTED/attachment"\r
+ \r
+ test_begin_subtest "Save attachment from within emacs using notmuch-show-save-part"\r
+ # save as archive to test that Emacs does not re-compress .gz\r
+-echo ./attachment2.gz |\r
+-test_emacs '(notmuch-show-save-part "id:cf0c4d610911171136h1713aa59w9cf9aa31f052ad0a@mail.gmail.com" 5)' > /dev/null 2>&1\r
++test_emacs '(let ((standard-input "\"attachment2.gz\""))\r
++ (notmuch-show-save-part "id:cf0c4d610911171136h1713aa59w9cf9aa31f052ad0a@mail.gmail.com" 5))' > /dev/null 2>&1\r
+ test_expect_equal_file attachment2.gz "$EXPECTED/attachment"\r
+ \r
+ test_begin_subtest "View raw message within emacs"\r
+diff --git a/test/test-lib.el b/test/test-lib.el\r
+index 4e7f5cf..a783936 100644\r
+--- a/test/test-lib.el\r
++++ b/test/test-lib.el\r
+@@ -23,6 +23,12 @@\r
+ ;; avoid crazy 10-column default of --batch\r
+ (set-frame-width (window-frame (get-buffer-window)) 80)\r
+ \r
++;; `read-file-name' by default uses `completing-read' function to read\r
++;; user input. It does not respect `standard-input' variable which we\r
++;; use in tests to provide user input. So replace it with a plain\r
++;; `read' call.\r
++(setq read-file-name-function (lambda (&rest _) (read)))\r
++\r
+ (defun notmuch-test-wait ()\r
+ "Wait for process completion."\r
+ (while (get-buffer-process (current-buffer))\r
+@@ -51,3 +57,10 @@ FILENAME is OUTPUT."\r
+ (setq str (concat str (buffer-substring start next-pos))))\r
+ (setq start next-pos)))\r
+ str))\r
++\r
++(defun orphan-watchdog (pid)\r
++ "Periodically check that the process with id PID is still\r
++running, quit if it terminated."\r
++ (if (not (process-attributes pid))\r
++ (kill-emacs)\r
++ (run-at-time "1 min" nil 'orphan-watchdog pid)))\r
+diff --git a/test/test-lib.sh b/test/test-lib.sh\r
+index 48bace2..e8f8629 100755\r
+--- a/test/test-lib.sh\r
++++ b/test/test-lib.sh\r
+@@ -829,6 +829,8 @@ test_done () {\r
+ \r
+ echo\r
+ \r
++ [ -n "$EMACS_SERVER" ] && test_emacs '(kill-emacs)'\r
++\r
+ if [ "$test_failure" = "0" ]; then\r
+ if [ "$test_broken" = "0" ]; then \r
+ rm -rf "$remove_tmp"\r
+@@ -848,16 +850,8 @@ emacs_generate_script () {\r
+ export PATH=$PATH\r
+ export NOTMUCH_CONFIG=$NOTMUCH_CONFIG\r
+ \r
+-# We assume that the user will give a command-line argument only if\r
+-# wanting to run in batch mode.\r
+-if [ \$# -gt 0 ]; then\r
+- BATCH=--batch\r
+-fi\r
+-\r
+ # Here's what we are using here:\r
+ #\r
+-# --batch: Quit after given commands and print all (messages)\r
+-#\r
+ # --no-init-file Don't load users ~/.emacs\r
+ #\r
+ # --no-site-file Don't load the site-wide startup stuff\r
+@@ -866,16 +860,24 @@ fi\r
+ #\r
+ # --load Force loading of notmuch.el and test-lib.el\r
+ \r
+-emacs \$BATCH --no-init-file --no-site-file \\r
++emacs --no-init-file --no-site-file \\r
+ --directory "$TMP_DIRECTORY/../../emacs" --load notmuch.el \\r
+ --directory "$TMP_DIRECTORY/.." --load test-lib.el \\r
+- --eval "(progn \$@)"\r
++ "\$@"\r
+ EOF\r
+ chmod a+x "$TMP_DIRECTORY/run_emacs"\r
+ }\r
+ \r
+ test_emacs () {\r
+- "$TMP_DIRECTORY/run_emacs" "$@"\r
++ if [ -z "$EMACS_SERVER" ]; then\r
++ EMACS_SERVER="notmuch-server-$$"\r
++ "$TMP_DIRECTORY/run_emacs" \\r
++ --daemon \\r
++ --eval "(setq server-name \"$EMACS_SERVER\")" \\r
++ --eval "(orphan-watchdog $$)" || return\r
++ fi\r
++\r
++ emacsclient --socket-name="$EMACS_SERVER" --eval "(progn $@)"\r
+ }\r
+ \r
+ \r
+-- \r
+1.7.5.4\r
+\r