]> git.notmuchmail.org Git - notmuch/blob - test/test-lib.el
test/emacs: globally force the html renderer to html2text
[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 <http://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
22
23 (require 'cl)   ;; This code is generally used uncompiled.
24
25 ;; `read-file-name' by default uses `completing-read' function to read
26 ;; user input.  It does not respect `standard-input' variable which we
27 ;; use in tests to provide user input.  So replace it with a plain
28 ;; `read' call.
29 (setq read-file-name-function (lambda (&rest _) (read)))
30
31 ;; Work around a bug in emacs 23.1 and emacs 23.2 which prevents
32 ;; noninteractive (kill-emacs) from emacsclient.
33 (if (and (= emacs-major-version 23) (< emacs-minor-version 3))
34   (defadvice kill-emacs (before disable-yes-or-no-p activate)
35     "Disable yes-or-no-p before executing kill-emacs"
36     (defun yes-or-no-p (prompt) t)))
37
38 ;; Emacs bug #2930:
39 ;;      23.0.92; `accept-process-output' and `sleep-for' do not run sentinels
40 ;; seems to be present in Emacs 23.1.
41 ;; Running `list-processes' after `accept-process-output' seems to work
42 ;; around this problem.
43 (if (and (= emacs-major-version 23) (= emacs-minor-version 1))
44   (defadvice accept-process-output (after run-list-processes activate)
45     "run list-processes after executing accept-process-output"
46     (list-processes)))
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     (with-temp-file (or filename "OUTPUT") (insert text))))
64
65 (defun visible-buffer-string ()
66   "Same as `buffer-string', but excludes invisible text and
67 removes any text properties."
68   (visible-buffer-substring (point-min) (point-max)))
69
70 (defun visible-buffer-substring (start end)
71   "Same as `buffer-substring-no-properties', but excludes
72 invisible text."
73   (let (str)
74     (while (< start end)
75       (let ((next-pos (next-char-property-change start end)))
76         (when (not (invisible-p start))
77           (setq str (concat str (buffer-substring-no-properties
78                                  start next-pos))))
79         (setq start next-pos)))
80     str))
81
82 ;; process-attributes is not defined everywhere, so define an
83 ;; alternate way to test if a process still exists.
84
85 (defun test-process-running (pid)
86   (= 0
87    (signal-process pid 0)))
88
89 (defun orphan-watchdog-check (pid)
90   "Periodically check that the process with id PID is still
91 running, quit if it terminated."
92   (if (not (test-process-running pid))
93       (kill-emacs)))
94
95 (defun orphan-watchdog (pid)
96   "Initiate orphan watchdog check."
97   (run-at-time 60 60 'orphan-watchdog-check pid))
98
99 (defun hook-counter (hook)
100   "Count how many times a hook is called.  Increments
101 `hook'-counter variable value if it is bound, otherwise does
102 nothing."
103   (let ((counter (intern (concat (symbol-name hook) "-counter"))))
104     (if (boundp counter)
105         (set counter (1+ (symbol-value counter))))))
106
107 (defun add-hook-counter (hook)
108   "Add hook to count how many times `hook' is called."
109   (add-hook hook (apply-partially 'hook-counter hook)))
110
111 (add-hook-counter 'notmuch-hello-mode-hook)
112 (add-hook-counter 'notmuch-hello-refresh-hook)
113
114 (defadvice notmuch-search-process-filter (around pessimal activate disable)
115   "Feed notmuch-search-process-filter one character at a time."
116   (let ((string (ad-get-arg 1)))
117     (loop for char across string
118           do (progn
119                (ad-set-arg 1 (char-to-string char))
120                ad-do-it))))
121
122 (defun notmuch-test-mark-links ()
123   "Enclose links in the current buffer with << and >>."
124   ;; Links are often created by jit-lock functions
125   (jit-lock-fontify-now)
126   (save-excursion
127     (let ((inhibit-read-only t))
128       (goto-char (point-min))
129       (let ((button))
130         (while (setq button (next-button (point)))
131           (goto-char (button-start button))
132           (insert "<<")
133           (goto-char (button-end button))
134           (insert ">>"))))))
135
136 (defmacro notmuch-test-run (&rest body)
137   "Evaluate a BODY of test expressions and output the result."
138   `(with-temp-buffer
139      (let ((buffer (current-buffer))
140            (result (progn ,@body)))
141        (switch-to-buffer buffer)
142        (insert (if (stringp result)
143                    result
144                  (prin1-to-string result)))
145        (test-output))))
146
147 (defun notmuch-test-report-unexpected (output expected)
148   "Report that the OUTPUT does not match the EXPECTED result."
149   (concat "Expect:\t" (prin1-to-string expected) "\n"
150           "Output:\t" (prin1-to-string output) "\n"))
151
152 (defun notmuch-test-expect-equal (output expected)
153   "Compare OUTPUT with EXPECTED. Report any discrepencies."
154   (if (equal output expected)
155       t
156     (cond
157      ((and (listp output)
158            (listp expected))
159       ;; Reporting the difference between two lists is done by
160       ;; reporting differing elements of OUTPUT and EXPECTED
161       ;; pairwise. This is expected to make analysis of failures
162       ;; simpler.
163       (apply #'concat (loop for o in output
164                             for e in expected
165                             if (not (equal o e))
166                             collect (notmuch-test-report-unexpected o e))))
167
168      (t
169       (notmuch-test-report-unexpected output expected)))))
170
171 (defun notmuch-post-command ()
172   (run-hooks 'post-command-hook))
173
174 (defmacro notmuch-test-progn (&rest body)
175   (cons 'progn
176         (mapcar
177          (lambda (x) `(prog1 ,x (notmuch-post-command)))
178          body)))
179
180 ;; For historical reasons, we hide deleted tags by default in the test
181 ;; suite
182 (setq notmuch-tag-deleted-formats
183       '((".*" nil)))
184
185 ;; force a common html renderer, to avoid test variations between
186 ;; environments
187
188 (setq mm-text-html-renderer 'html2text)