]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
Merge branch 'release'
[notmuch] / test / test-lib.el
1 ;;; test-lib.el --- auxiliary stuff for Notmuch Emacs tests
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch test suit.
7 ;;
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.
12 ;;
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.
17 ;;
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/>.
20 ;;
21 ;; Authors: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
22
23 ;;; Code:
24
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
29 ;; .elc files.
30 (setq native-comp-jit-compilation nil)
31 (setq native-comp-speed -1)
32 (setq native-comp-async-jobs-number 1)
33
34 (require 'cl-lib)
35
36 ;; Use old pretty print algorithm, so tests don't break with Emacs 30
37 (setq-default pp-default-function 'pp-28)
38
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.
43 (require 'smtpmail)
44
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
48 ;; `read' call.
49 (setq read-file-name-function (lambda (&rest _) (read)))
50
51 (defun notmuch-test-wait ()
52   "Wait for process completion."
53   (while (get-buffer-process (current-buffer))
54     (accept-process-output nil 0.1)))
55
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")))
60
61 (defun test-visible-output (&optional filename)
62   "Save visible text in current buffer to file FILENAME.  Default
63 FILENAME is OUTPUT."
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))))
69
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)))
74
75 (defun visible-buffer-substring (start end)
76   "Same as `buffer-substring-no-properties', but excludes
77 invisible text."
78   (let (str)
79     (while (< start end)
80       (let ((next-pos (next-char-property-change start end)))
81         (unless (invisible-p start)
82           (setq str (concat str (buffer-substring-no-properties
83                                  start next-pos))))
84         (setq start next-pos)))
85     str))
86
87 ;; process-attributes is not defined everywhere, so define an
88 ;; alternate way to test if a process still exists.
89
90 (defun test-process-running (pid)
91   (= 0
92    (signal-process pid 0)))
93
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)
98     (kill-emacs)))
99
100 (defun orphan-watchdog (pid)
101   "Initiate orphan watchdog check."
102   (run-at-time 60 60 'orphan-watchdog-check pid))
103
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)))
108
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)))
113
114 (defvar notmuch-test-tag-hook-output nil)
115 (defun notmuch-test-tag-hook () (push (cons query tag-changes) notmuch-test-tag-hook-output))
116
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)
121   (save-excursion
122     (let ((inhibit-read-only t))
123       (goto-char (point-min))
124       (let ((button))
125         (while (setq button (next-button (point)))
126           (goto-char (button-start button))
127           (insert "<<")
128           (goto-char (button-end button))
129           (insert ">>"))))))
130
131 (defmacro notmuch-test-run (&rest body)
132   "Evaluate a BODY of test expressions and output the result."
133   `(with-temp-buffer
134      (let ((buffer (current-buffer))
135            (result (progn ,@body)))
136        (switch-to-buffer buffer)
137        (insert (if (stringp result)
138                    result
139                  (prin1-to-string result)))
140        (test-output))))
141
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"))
146
147 (defun notmuch-test-expect-equal (output expected)
148   "Compare OUTPUT with EXPECTED. Report any discrepancies."
149   (cond
150    ((equal output expected)
151     t)
152    ((and (listp output)
153          (listp 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
157     ;; simpler.
158     (apply #'concat (cl-loop for o in output
159                              for e in expected
160                              if (not (equal o e))
161                              collect (notmuch-test-report-unexpected o e))))
162    (t
163     (notmuch-test-report-unexpected output expected))))
164
165 (defun notmuch-post-command ()
166   (run-hooks 'post-command-hook))
167
168 (defmacro notmuch-test-progn (&rest body)
169   (cons 'progn
170         (mapcar
171          (lambda (x) `(prog1 ,x (notmuch-post-command)))
172          body)))
173
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" . "&")
178                                  ("signed" . "=")
179                                  ("unread" . "u")
180                                  ("inbox" . "i"))))
181         (tags (plist-get result :tags)))
182     (format format-string
183             (mapconcat (lambda (t2l)
184                          (if (member (car t2l) tags)
185                              (cdr t2l)
186                            " "))
187                        tags-to-letters ""))))
188
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)
192   `(progn
193      (with-current-buffer "*Messages*"
194        (let ((inhibit-read-only t)) (erase-buffer)))
195      (condition-case err
196        (progn ,@body
197           (message "COMPLETE"))
198        (t (message "%s" err)))
199      (with-current-buffer "*Messages*" (test-output "MESSAGES"))))
200
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"))))
205
206 ;; For historical reasons, we hide deleted tags by default in the test
207 ;; suite
208 (setq notmuch-tag-deleted-formats
209       '((".*" nil)))
210
211 ;; Also for historical reasons, we set the fcc handler to file not
212 ;; insert.
213
214 (setq notmuch-maildir-use-notmuch-insert nil)
215
216 ;; force a common html renderer, to avoid test variations between
217 ;; environments
218
219 (setq mm-text-html-renderer 'html2text)
220
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:"))