]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
test: Deal with Emacs 27 switching to lexical scope by default
[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 ;; Work around a bug in emacs 23.1 and emacs 23.2 which prevents
38 ;; noninteractive (kill-emacs) from emacsclient.
39 (if (and (= emacs-major-version 23) (< emacs-minor-version 3))
40   (defadvice kill-emacs (before disable-yes-or-no-p activate)
41     "Disable yes-or-no-p before executing kill-emacs"
42     (defun yes-or-no-p (prompt) t)))
43
44 ;; Emacs bug #2930:
45 ;;      23.0.92; `accept-process-output' and `sleep-for' do not run sentinels
46 ;; seems to be present in Emacs 23.1.
47 ;; Running `list-processes' after `accept-process-output' seems to work
48 ;; around this problem.
49 (if (and (= emacs-major-version 23) (= emacs-minor-version 1))
50   (defadvice accept-process-output (after run-list-processes activate)
51     "run list-processes after executing accept-process-output"
52     (list-processes)))
53
54 (defun notmuch-test-wait ()
55   "Wait for process completion."
56   (while (get-buffer-process (current-buffer))
57     (accept-process-output nil 0.1)))
58
59 (defun test-output (&optional filename)
60   "Save current buffer to file FILENAME.  Default FILENAME is OUTPUT."
61   (notmuch-post-command)
62   (write-region (point-min) (point-max) (or filename "OUTPUT")))
63
64 (defun test-visible-output (&optional filename)
65   "Save visible text in current buffer to file FILENAME.  Default
66 FILENAME is OUTPUT."
67   (notmuch-post-command)
68   (let ((text (visible-buffer-string))
69         ;; Tests expect output in UTF-8 encoding
70         (coding-system-for-write 'utf-8))
71     (with-temp-file (or filename "OUTPUT") (insert text))))
72
73 (defun visible-buffer-string ()
74   "Same as `buffer-string', but excludes invisible text and
75 removes any text properties."
76   (visible-buffer-substring (point-min) (point-max)))
77
78 (defun visible-buffer-substring (start end)
79   "Same as `buffer-substring-no-properties', but excludes
80 invisible text."
81   (let (str)
82     (while (< start end)
83       (let ((next-pos (next-char-property-change start end)))
84         (when (not (invisible-p start))
85           (setq str (concat str (buffer-substring-no-properties
86                                  start next-pos))))
87         (setq start next-pos)))
88     str))
89
90 ;; process-attributes is not defined everywhere, so define an
91 ;; alternate way to test if a process still exists.
92
93 (defun test-process-running (pid)
94   (= 0
95    (signal-process pid 0)))
96
97 (defun orphan-watchdog-check (pid)
98   "Periodically check that the process with id PID is still
99 running, quit if it terminated."
100   (if (not (test-process-running pid))
101       (kill-emacs)))
102
103 (defun orphan-watchdog (pid)
104   "Initiate orphan watchdog check."
105   (run-at-time 60 60 'orphan-watchdog-check pid))
106
107 (defun hook-counter (hook)
108   "Count how many times a hook is called.  Increments
109 `hook'-counter variable value if it is bound, otherwise does
110 nothing."
111   (let ((counter (intern (concat (symbol-name hook) "-counter"))))
112     (if (boundp counter)
113         (set counter (1+ (symbol-value counter))))))
114
115 (defun add-hook-counter (hook)
116   "Add hook to count how many times `hook' is called."
117   (add-hook hook (apply-partially 'hook-counter hook)))
118
119 (add-hook-counter 'notmuch-hello-mode-hook)
120 (add-hook-counter 'notmuch-hello-refresh-hook)
121
122 (defvar notmuch-hello-mode-hook-counter -100
123   "Tests that care about this counter must let-bind it to 0.")
124
125 (defvar notmuch-hello-refresh-hook-counter -100
126   "Tests that care about this counter must let-bind it to 0.")
127
128 (defadvice notmuch-search-process-filter (around pessimal activate disable)
129   "Feed notmuch-search-process-filter one character at a time."
130   (let ((string (ad-get-arg 1)))
131     (cl-loop for char across string
132              do (progn
133                   (ad-set-arg 1 (char-to-string char))
134                   ad-do-it))))
135
136 (defun notmuch-test-mark-links ()
137   "Enclose links in the current buffer with << and >>."
138   ;; Links are often created by jit-lock functions
139   (jit-lock-fontify-now)
140   (save-excursion
141     (let ((inhibit-read-only t))
142       (goto-char (point-min))
143       (let ((button))
144         (while (setq button (next-button (point)))
145           (goto-char (button-start button))
146           (insert "<<")
147           (goto-char (button-end button))
148           (insert ">>"))))))
149
150 (defmacro notmuch-test-run (&rest body)
151   "Evaluate a BODY of test expressions and output the result."
152   `(with-temp-buffer
153      (let ((buffer (current-buffer))
154            (result (progn ,@body)))
155        (switch-to-buffer buffer)
156        (insert (if (stringp result)
157                    result
158                  (prin1-to-string result)))
159        (test-output))))
160
161 (defun notmuch-test-report-unexpected (output expected)
162   "Report that the OUTPUT does not match the EXPECTED result."
163   (concat "Expect:\t" (prin1-to-string expected) "\n"
164           "Output:\t" (prin1-to-string output) "\n"))
165
166 (defun notmuch-test-expect-equal (output expected)
167   "Compare OUTPUT with EXPECTED. Report any discrepencies."
168   (if (equal output expected)
169       t
170     (cond
171      ((and (listp output)
172            (listp expected))
173       ;; Reporting the difference between two lists is done by
174       ;; reporting differing elements of OUTPUT and EXPECTED
175       ;; pairwise. This is expected to make analysis of failures
176       ;; simpler.
177       (apply #'concat (cl-loop for o in output
178                                for e in expected
179                                if (not (equal o e))
180                                collect (notmuch-test-report-unexpected o e))))
181
182      (t
183       (notmuch-test-report-unexpected output expected)))))
184
185 (defun notmuch-post-command ()
186   (run-hooks 'post-command-hook))
187
188 (defmacro notmuch-test-progn (&rest body)
189   (cons 'progn
190         (mapcar
191          (lambda (x) `(prog1 ,x (notmuch-post-command)))
192          body)))
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)