1 ;; test-lib.el --- auxiliary stuff for Notmuch Emacs tests.
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
6 ;; This file is part of Notmuch test suit.
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
21 ;; Authors: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
23 (require 'cl) ;; This code is generally used uncompiled.
25 ;; `read-file-name' by default uses `completing-read' function to read
26 ;; user input. It does not respect `standard-input' variable which we
27 ;; use in tests to provide user input. So replace it with a plain
29 (setq read-file-name-function (lambda (&rest _) (read)))
31 ;; Work around a bug in emacs 23.1 and emacs 23.2 which prevents
32 ;; noninteractive (kill-emacs) from emacsclient.
33 (if (and (= emacs-major-version 23) (< emacs-minor-version 3))
34 (defadvice kill-emacs (before disable-yes-or-no-p activate)
35 "Disable yes-or-no-p before executing kill-emacs"
36 (defun yes-or-no-p (prompt) t)))
39 ;; 23.0.92; `accept-process-output' and `sleep-for' do not run sentinels
40 ;; seems to be present in Emacs 23.1.
41 ;; Running `list-processes' after `accept-process-output' seems to work
42 ;; around this problem.
43 (if (and (= emacs-major-version 23) (= emacs-minor-version 1))
44 (defadvice accept-process-output (after run-list-processes activate)
45 "run list-processes after executing accept-process-output"
48 (defun notmuch-test-wait ()
49 "Wait for process completion."
50 (while (get-buffer-process (current-buffer))
51 (accept-process-output nil 0.1)))
53 (defun test-output (&optional filename)
54 "Save current buffer to file FILENAME. Default FILENAME is OUTPUT."
55 (notmuch-post-command)
56 (write-region (point-min) (point-max) (or filename "OUTPUT")))
58 (defun test-visible-output (&optional filename)
59 "Save visible text in current buffer to file FILENAME. Default
61 (notmuch-post-command)
62 (let ((text (visible-buffer-string))
63 ;; Tests expect output in UTF-8 encoding
64 (coding-system-for-write 'utf-8))
65 (with-temp-file (or filename "OUTPUT") (insert text))))
67 (defun visible-buffer-string ()
68 "Same as `buffer-string', but excludes invisible text and
69 removes any text properties."
70 (visible-buffer-substring (point-min) (point-max)))
72 (defun visible-buffer-substring (start end)
73 "Same as `buffer-substring-no-properties', but excludes
77 (let ((next-pos (next-char-property-change start end)))
78 (when (not (invisible-p start))
79 (setq str (concat str (buffer-substring-no-properties
81 (setq start next-pos)))
84 ;; process-attributes is not defined everywhere, so define an
85 ;; alternate way to test if a process still exists.
87 (defun test-process-running (pid)
89 (signal-process pid 0)))
91 (defun orphan-watchdog-check (pid)
92 "Periodically check that the process with id PID is still
93 running, quit if it terminated."
94 (if (not (test-process-running pid))
97 (defun orphan-watchdog (pid)
98 "Initiate orphan watchdog check."
99 (run-at-time 60 60 'orphan-watchdog-check pid))
101 (defun hook-counter (hook)
102 "Count how many times a hook is called. Increments
103 `hook'-counter variable value if it is bound, otherwise does
105 (let ((counter (intern (concat (symbol-name hook) "-counter"))))
107 (set counter (1+ (symbol-value counter))))))
109 (defun add-hook-counter (hook)
110 "Add hook to count how many times `hook' is called."
111 (add-hook hook (apply-partially 'hook-counter hook)))
113 (add-hook-counter 'notmuch-hello-mode-hook)
114 (add-hook-counter 'notmuch-hello-refresh-hook)
116 (defadvice notmuch-search-process-filter (around pessimal activate disable)
117 "Feed notmuch-search-process-filter one character at a time."
118 (let ((string (ad-get-arg 1)))
119 (loop for char across string
121 (ad-set-arg 1 (char-to-string char))
124 (defun notmuch-test-mark-links ()
125 "Enclose links in the current buffer with << and >>."
126 ;; Links are often created by jit-lock functions
127 (jit-lock-fontify-now)
129 (let ((inhibit-read-only t))
130 (goto-char (point-min))
132 (while (setq button (next-button (point)))
133 (goto-char (button-start button))
135 (goto-char (button-end button))
138 (defmacro notmuch-test-run (&rest body)
139 "Evaluate a BODY of test expressions and output the result."
141 (let ((buffer (current-buffer))
142 (result (progn ,@body)))
143 (switch-to-buffer buffer)
144 (insert (if (stringp result)
146 (prin1-to-string result)))
149 (defun notmuch-test-report-unexpected (output expected)
150 "Report that the OUTPUT does not match the EXPECTED result."
151 (concat "Expect:\t" (prin1-to-string expected) "\n"
152 "Output:\t" (prin1-to-string output) "\n"))
154 (defun notmuch-test-expect-equal (output expected)
155 "Compare OUTPUT with EXPECTED. Report any discrepencies."
156 (if (equal output expected)
161 ;; Reporting the difference between two lists is done by
162 ;; reporting differing elements of OUTPUT and EXPECTED
163 ;; pairwise. This is expected to make analysis of failures
165 (apply #'concat (loop for o in output
168 collect (notmuch-test-report-unexpected o e))))
171 (notmuch-test-report-unexpected output expected)))))
173 (defun notmuch-post-command ()
174 (run-hooks 'post-command-hook))
176 (defmacro notmuch-test-progn (&rest body)
179 (lambda (x) `(prog1 ,x (notmuch-post-command)))
182 ;; For historical reasons, we hide deleted tags by default in the test
184 (setq notmuch-tag-deleted-formats
187 ;; force a common html renderer, to avoid test variations between
190 (setq mm-text-html-renderer 'html2text)