]> git.notmuchmail.org Git - notmuch/blob - test/emacs-attachment-warnings.el
test: extend test of attachment warnings
[notmuch] / test / emacs-attachment-warnings.el
1 (require 'notmuch-mua)
2
3 (defun attachment-check-test (&optional fn)
4   "Test `notmuch-mua-attachment-check' using a message where optional FN is evaluated.
5
6 Return `t' if the message would be sent, otherwise `nil'"
7   (notmuch-mua-mail)
8   (message-goto-body)
9   (when fn
10     (funcall fn))
11   (prog1
12       (condition-case nil
13           ;; Force `y-or-n-p' to always return `nil', as if the user
14           ;; pressed "n".
15           (letf (((symbol-function 'y-or-n-p) (lambda (&rest args) nil)))
16             (notmuch-mua-attachment-check)
17             t)
18         ('error nil))
19     (set-buffer-modified-p nil)
20     (kill-buffer (current-buffer))))
21
22 (defvar attachment-check-tests
23   '(
24     ;; These are all okay:
25     (t)
26     (t . (lambda () (insert "Nothing is a-tt-a-ch-ed!\n")))
27     (t . (lambda ()
28            (insert "Here is an attachment:\n")
29            (insert "<#part filename=\"foo\" />\n")))
30     (t . (lambda () (insert "<#part filename=\"foo\" />\n")))
31     (t . (lambda ()
32            ;; "attachment" is only mentioned in a quoted section.
33            (insert "> I sent you an attachment!\n")
34            ;; Code in `notmuch-mua-attachment-check' avoids matching on
35            ;; "attachment" in a quoted section of the message by looking at
36            ;; fontification properties. For fontification to happen we need to
37            ;; allow some time for redisplay.
38            (sit-for 0.01)))
39     (t . (lambda ()
40            ;; "attach" is only mentioned in a forwarded message.
41            (insert "Hello\n")
42            (insert "<#mml type=message/rfc822 disposition=inline>\n")
43            (insert "X-Has-Attach:\n")
44            (insert "<#/mml>\n")))
45
46     ;; These should not be okay:
47     (nil . (lambda () (insert "Here is an attachment:\n")))
48     (nil . (lambda ()
49              ;; "attachment" is mentioned in both a quoted section and
50              ;; outside of it.
51              (insert "> I sent you an attachment!\n")
52              (insert "The attachment was missing!\n")
53              ;; Code in `notmuch-mua-attachment-check' avoids matching
54              ;; on "attachment" in a quoted section of the message by
55              ;; looking at fontification properties. For fontification
56              ;; to happen we need to allow some time for redisplay.
57              (sit-for 0.01)))
58     (nil . (lambda ()
59            ;; "attachment" is mentioned before a forwarded message.
60            (insert "I also attach something.\n")
61            (insert "<#mml type=message/rfc822 disposition=inline>\n")
62            (insert "X-Has-Attach:\n")
63            (insert "<#/mml>\n")))
64     ))
65
66 (defun notmuch-test-attachment-warning-1 ()
67   (let (output expected)
68     (mapcar (lambda (test)
69               (let* ((expect (car test))
70                      (body (cdr test))
71                      (result (attachment-check-test body)))
72                 (push expect expected)
73                 (push (if (eq result expect)
74                           result
75                         ;; In the case of a failure, include the test
76                         ;; details to make it simpler to debug.
77                         (format "%S <-- %S" result body))
78                       output)))
79             attachment-check-tests)
80     (notmuch-test-expect-equal output expected)))