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 <https://www.gnu.org/licenses/>.
21 ;; Authors: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
25 ;; minimize impact of native compilation on the test suite.
26 ;; These are the Emacs 29.1 version of the variables.
27 ;; Leave trampolines enabled per Emacs upstream recommendations.
28 ;; It is important to set these variables before loading any
30 (setq native-comp-jit-compilation nil)
31 (setq native-comp-speed -1)
32 (setq native-comp-async-jobs-number 1)
36 ;; Use old pretty print algorithm, so tests don't break with Emacs 30
37 (setq-default pp-default-function 'pp-28)
39 ;; Ensure that the dynamic variables that are defined by this library
40 ;; are defined by the time that we let-bind them. This is needed
41 ;; because starting with Emacs 27 undeclared variables in evaluated
42 ;; interactive code (such as our tests) use lexical scope.
45 ;; `read-file-name' by default uses `completing-read' function to read
46 ;; user input. It does not respect `standard-input' variable which we
47 ;; use in tests to provide user input. So replace it with a plain
49 (setq read-file-name-function (lambda (&rest _) (read)))
51 (defun notmuch-test-wait ()
52 "Wait for process completion."
53 (while (get-buffer-process (current-buffer))
54 (accept-process-output nil 0.1)))
56 (defun test-output (&optional filename)
57 "Save current buffer to file FILENAME. Default FILENAME is OUTPUT."
58 (notmuch-post-command)
59 (write-region (point-min) (point-max) (or filename "OUTPUT")))
61 (defun test-visible-output (&optional filename)
62 "Save visible text in current buffer to file FILENAME. Default
64 (notmuch-post-command)
65 (let ((text (visible-buffer-string))
66 ;; Tests expect output in UTF-8 encoding
67 (coding-system-for-write 'utf-8))
68 (with-temp-file (or filename "OUTPUT") (insert text))))
70 (defun visible-buffer-string ()
71 "Same as `buffer-string', but excludes invisible text and
72 removes any text properties."
73 (visible-buffer-substring (point-min) (point-max)))
75 (defun visible-buffer-substring (start end)
76 "Same as `buffer-substring-no-properties', but excludes
80 (let ((next-pos (next-char-property-change start end)))
81 (unless (invisible-p start)
82 (setq str (concat str (buffer-substring-no-properties
84 (setq start next-pos)))
87 ;; process-attributes is not defined everywhere, so define an
88 ;; alternate way to test if a process still exists.
90 (defun test-process-running (pid)
92 (signal-process pid 0)))
94 (defun orphan-watchdog-check (pid)
95 "Periodically check that the process with id PID is still
96 running, quit if it terminated."
97 (unless (test-process-running pid)
100 (defun orphan-watchdog (pid)
101 "Initiate orphan watchdog check."
102 (run-at-time 60 60 'orphan-watchdog-check pid))
104 (defvar notmuch-hello-mode-hook-counter -100
105 "Tests that care about this counter must let-bind it to 0.")
106 (add-hook 'notmuch-hello-mode-hook
107 (lambda () (cl-incf notmuch-hello-mode-hook-counter)))
109 (defvar notmuch-hello-refresh-hook-counter -100
110 "Tests that care about this counter must let-bind it to 0.")
111 (add-hook 'notmuch-hello-refresh-hook
112 (lambda () (cl-incf notmuch-hello-refresh-hook-counter)))
114 (defvar notmuch-test-tag-hook-output nil)
115 (defun notmuch-test-tag-hook () (push (cons query tag-changes) notmuch-test-tag-hook-output))
117 (defun notmuch-test-mark-links ()
118 "Enclose links in the current buffer with << and >>."
119 ;; Links are often created by jit-lock functions
120 (jit-lock-fontify-now)
122 (let ((inhibit-read-only t))
123 (goto-char (point-min))
125 (while (setq button (next-button (point)))
126 (goto-char (button-start button))
128 (goto-char (button-end button))
131 (defmacro notmuch-test-run (&rest body)
132 "Evaluate a BODY of test expressions and output the result."
134 (let ((buffer (current-buffer))
135 (result (progn ,@body)))
136 (switch-to-buffer buffer)
137 (insert (if (stringp result)
139 (prin1-to-string result)))
142 (defun notmuch-test-report-unexpected (output expected)
143 "Report that the OUTPUT does not match the EXPECTED result."
144 (concat "Expect:\t" (prin1-to-string expected) "\n"
145 "Output:\t" (prin1-to-string output) "\n"))
147 (defun notmuch-test-expect-equal (output expected)
148 "Compare OUTPUT with EXPECTED. Report any discrepancies."
150 ((equal output expected)
154 ;; Reporting the difference between two lists is done by
155 ;; reporting differing elements of OUTPUT and EXPECTED
156 ;; pairwise. This is expected to make analysis of failures
158 (apply #'concat (cl-loop for o in output
161 collect (notmuch-test-report-unexpected o e))))
163 (notmuch-test-report-unexpected output expected))))
165 (defun notmuch-post-command ()
166 (run-hooks 'post-command-hook))
168 (defmacro notmuch-test-progn (&rest body)
171 (lambda (x) `(prog1 ,x (notmuch-post-command)))
174 ;; For testing functions in
175 ;; notmuch-{search,tree,unsorted}-result-format
176 (defun notmuch-test-result-flags (format-string result)
177 (let ((tags-to-letters (quote (("attachment" . "&")
181 (tags (plist-get result :tags)))
182 (format format-string
183 (mapconcat (lambda (t2l)
184 (if (member (car t2l) tags)
187 tags-to-letters ""))))
189 ;; Log any signalled error (and other messages) to MESSAGES
190 ;; Log "COMPLETE" if forms complete without error.
191 (defmacro test-log-error (&rest body)
193 (with-current-buffer "*Messages*"
194 (let ((inhibit-read-only t)) (erase-buffer)))
197 (message "COMPLETE"))
198 (t (message "%s" err)))
199 (with-current-buffer "*Messages*" (test-output "MESSAGES"))))
201 (defmacro test-time (&rest body)
202 `(let ((results (mapcar (lambda (x) (/ x 5.0)) (benchmark-run 5 ,@body))))
203 (message "\t\t%0.2f\t%0.2f\t%0.2f" (nth 0 results) (nth 1 results) (nth 2 results))
204 (with-current-buffer "*Messages*" (test-output "MESSAGES"))))
206 ;; For historical reasons, we hide deleted tags by default in the test
208 (setq notmuch-tag-deleted-formats
211 ;; Also for historical reasons, we set the fcc handler to file not
214 (setq notmuch-maildir-use-notmuch-insert nil)
216 ;; force a common html renderer, to avoid test variations between
219 (setq mm-text-html-renderer 'html2text)
221 ;; Set our own default for message-hidden-headers, to avoid tests
222 ;; breaking when the Emacs default changes.
223 (setq message-hidden-headers
224 '("^References:" "^Face:" "^X-Face:" "^X-Draft-From:"))