]> git.cworth.org Git - notmuch/blob - test/test-lib.el
emacs: Add new option notmuch-search-hide-excluded
[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 ;; Ensure that the dynamic variables that are defined by this library
37 ;; are defined by the time that we let-bind them.  This is needed
38 ;; because starting with Emacs 27 undeclared variables in evaluated
39 ;; interactive code (such as our tests) use lexical scope.
40 (require 'smtpmail)
41
42 ;; `read-file-name' by default uses `completing-read' function to read
43 ;; user input.  It does not respect `standard-input' variable which we
44 ;; use in tests to provide user input.  So replace it with a plain
45 ;; `read' call.
46 (setq read-file-name-function (lambda (&rest _) (read)))
47
48 (defun notmuch-test-wait ()
49   "Wait for process completion."
50   (while (get-buffer-process (current-buffer))
51     (accept-process-output nil 0.1)))
52
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")))
57
58 (defun test-visible-output (&optional filename)
59   "Save visible text in current buffer to file FILENAME.  Default
60 FILENAME is OUTPUT."
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))))
66
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)))
71
72 (defun visible-buffer-substring (start end)
73   "Same as `buffer-substring-no-properties', but excludes
74 invisible text."
75   (let (str)
76     (while (< start end)
77       (let ((next-pos (next-char-property-change start end)))
78         (unless (invisible-p start)
79           (setq str (concat str (buffer-substring-no-properties
80                                  start next-pos))))
81         (setq start next-pos)))
82     str))
83
84 ;; process-attributes is not defined everywhere, so define an
85 ;; alternate way to test if a process still exists.
86
87 (defun test-process-running (pid)
88   (= 0
89    (signal-process pid 0)))
90
91 (defun orphan-watchdog-check (pid)
92   "Periodically check that the process with id PID is still
93 running, quit if it terminated."
94   (unless (test-process-running pid)
95     (kill-emacs)))
96
97 (defun orphan-watchdog (pid)
98   "Initiate orphan watchdog check."
99   (run-at-time 60 60 'orphan-watchdog-check pid))
100
101 (defvar notmuch-hello-mode-hook-counter -100
102   "Tests that care about this counter must let-bind it to 0.")
103 (add-hook 'notmuch-hello-mode-hook
104           (lambda () (cl-incf notmuch-hello-mode-hook-counter)))
105
106 (defvar notmuch-hello-refresh-hook-counter -100
107   "Tests that care about this counter must let-bind it to 0.")
108 (add-hook 'notmuch-hello-refresh-hook
109           (lambda () (cl-incf notmuch-hello-refresh-hook-counter)))
110
111 (defvar notmuch-test-tag-hook-output nil)
112 (defun notmuch-test-tag-hook () (push (cons query tag-changes) notmuch-test-tag-hook-output))
113
114 (defun notmuch-test-mark-links ()
115   "Enclose links in the current buffer with << and >>."
116   ;; Links are often created by jit-lock functions
117   (jit-lock-fontify-now)
118   (save-excursion
119     (let ((inhibit-read-only t))
120       (goto-char (point-min))
121       (let ((button))
122         (while (setq button (next-button (point)))
123           (goto-char (button-start button))
124           (insert "<<")
125           (goto-char (button-end button))
126           (insert ">>"))))))
127
128 (defmacro notmuch-test-run (&rest body)
129   "Evaluate a BODY of test expressions and output the result."
130   `(with-temp-buffer
131      (let ((buffer (current-buffer))
132            (result (progn ,@body)))
133        (switch-to-buffer buffer)
134        (insert (if (stringp result)
135                    result
136                  (prin1-to-string result)))
137        (test-output))))
138
139 (defun notmuch-test-report-unexpected (output expected)
140   "Report that the OUTPUT does not match the EXPECTED result."
141   (concat "Expect:\t" (prin1-to-string expected) "\n"
142           "Output:\t" (prin1-to-string output) "\n"))
143
144 (defun notmuch-test-expect-equal (output expected)
145   "Compare OUTPUT with EXPECTED. Report any discrepancies."
146   (cond
147    ((equal output expected)
148     t)
149    ((and (listp output)
150          (listp expected))
151     ;; Reporting the difference between two lists is done by
152     ;; reporting differing elements of OUTPUT and EXPECTED
153     ;; pairwise. This is expected to make analysis of failures
154     ;; simpler.
155     (apply #'concat (cl-loop for o in output
156                              for e in expected
157                              if (not (equal o e))
158                              collect (notmuch-test-report-unexpected o e))))
159    (t
160     (notmuch-test-report-unexpected output expected))))
161
162 (defun notmuch-post-command ()
163   (run-hooks 'post-command-hook))
164
165 (defmacro notmuch-test-progn (&rest body)
166   (cons 'progn
167         (mapcar
168          (lambda (x) `(prog1 ,x (notmuch-post-command)))
169          body)))
170
171 ;; For testing functions in
172 ;; notmuch-{search,tree,unsorted}-result-format
173 (defun notmuch-test-result-flags (format-string result)
174   (let ((tags-to-letters (quote (("attachment" . "&")
175                                  ("signed" . "=")
176                                  ("unread" . "u")
177                                  ("inbox" . "i"))))
178         (tags (plist-get result :tags)))
179     (format format-string
180             (mapconcat (lambda (t2l)
181                          (if (member (car t2l) tags)
182                              (cdr t2l)
183                            " "))
184                        tags-to-letters ""))))
185
186 ;; Log any signalled error (and other messages) to MESSAGES
187 ;; Log "COMPLETE" if forms complete without error.
188 (defmacro test-log-error (&rest body)
189   `(progn
190      (with-current-buffer "*Messages*"
191        (let ((inhibit-read-only t)) (erase-buffer)))
192      (condition-case err
193        (progn ,@body
194           (message "COMPLETE"))
195        (t (message "%s" err)))
196      (with-current-buffer "*Messages*" (test-output "MESSAGES"))))
197
198 (defmacro test-time (&rest body)
199   `(let ((results (mapcar (lambda (x) (/ x 5.0)) (benchmark-run 5 ,@body))))
200      (message "\t\t%0.2f\t%0.2f\t%0.2f" (nth 0 results) (nth 1 results) (nth 2 results))
201      (with-current-buffer "*Messages*" (test-output "MESSAGES"))))
202
203 ;; For historical reasons, we hide deleted tags by default in the test
204 ;; suite
205 (setq notmuch-tag-deleted-formats
206       '((".*" nil)))
207
208 ;; Also for historical reasons, we set the fcc handler to file not
209 ;; insert.
210
211 (setq notmuch-maildir-use-notmuch-insert nil)
212
213 ;; force a common html renderer, to avoid test variations between
214 ;; environments
215
216 (setq mm-text-html-renderer 'html2text)
217
218 ;; Set our own default for message-hidden-headers, to avoid tests
219 ;; breaking when the Emacs default changes.
220 (setq message-hidden-headers
221       '("^References:" "^Face:" "^X-Face:" "^X-Draft-From:"))