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