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