aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGemini Lasswell2017-02-04 13:36:43 +0200
committerEli Zaretskii2017-02-04 13:36:43 +0200
commitef3d8d6f7226e570209e913d2754e828d0cb121c (patch)
tree598390fc1084a54d90ae1c5cec952ddd79e8dbfc
parent8ba27b7ce2f4a98e3c14fe752042c60fd7576fef (diff)
downloademacs-ef3d8d6f7226e570209e913d2754e828d0cb121c.tar.gz
emacs-ef3d8d6f7226e570209e913d2754e828d0cb121c.zip
New macro 'ert-with-message-capture'
* lisp/emacs-lisp/ert-x.el (ert-with-message-capture): New macro. (Bug#25158) * test/lisp/autorevert-tests.el (auto-revert--wait-for-revert) (auto-revert-test00-auto-revert-mode) (auto-revert-test01-auto-revert-several-files) (auto-revert-test02-auto-revert-deleted-file) (auto-revert-test03-auto-revert-tail-mode) (auto-revert-test04-auto-revert-mode-dired): * test/lisp/filenotify-tests.el (file-notify-test03-autorevert): Use ert-with-message-capture.
-rw-r--r--lisp/emacs-lisp/ert-x.el24
-rw-r--r--test/lisp/autorevert-tests.el166
-rw-r--r--test/lisp/filenotify-tests.el56
3 files changed, 125 insertions, 121 deletions
diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 8530253d5b4..4cf9d9609e9 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -285,6 +285,30 @@ BUFFER defaults to current buffer. Does not modify BUFFER."
285 (kill-buffer clone))))))) 285 (kill-buffer clone)))))))
286 286
287 287
288(defmacro ert-with-message-capture (var &rest body)
289 "Execute BODY while collecting anything written with `message' in VAR.
290
291Capture all messages produced by `message' when it is called from
292Lisp, and concatenate them separated by newlines into one string.
293
294This is useful for separating the issuance of messages by the
295code under test from the behavior of the *Messages* buffer."
296 (declare (debug (symbolp body))
297 (indent 1))
298 (let ((g-advice (cl-gensym)))
299 `(let* ((,var "")
300 (,g-advice (lambda (func &rest args)
301 (if (or (null args) (equal (car args) ""))
302 (apply func args)
303 (let ((msg (apply #'format-message args)))
304 (setq ,var (concat ,var msg "\n"))
305 (funcall func "%s" msg))))))
306 (advice-add 'message :around ,g-advice)
307 (unwind-protect
308 (progn ,@body)
309 (advice-remove 'message ,g-advice)))))
310
311
288(provide 'ert-x) 312(provide 'ert-x)
289 313
290;;; ert-x.el ends here 314;;; ert-x.el ends here
diff --git a/test/lisp/autorevert-tests.el b/test/lisp/autorevert-tests.el
index aea855ae02f..c082ba95639 100644
--- a/test/lisp/autorevert-tests.el
+++ b/test/lisp/autorevert-tests.el
@@ -24,24 +24,29 @@
24;;; Code: 24;;; Code:
25 25
26(require 'ert) 26(require 'ert)
27(require 'ert-x)
27(require 'autorevert) 28(require 'autorevert)
28(setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded" 29(setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
29 auto-revert-stop-on-user-input nil) 30 auto-revert-stop-on-user-input nil)
30 31
31(defconst auto-revert--timeout 10 32(defconst auto-revert--timeout 10
32 "Time to wait until a message appears in the *Messages* buffer.") 33 "Time to wait for a message.")
34
35(defvar auto-revert--messages nil
36 "Used to collect messages issued during a section of a test.")
33 37
34(defun auto-revert--wait-for-revert (buffer) 38(defun auto-revert--wait-for-revert (buffer)
35 "Wait until the *Messages* buffer reports reversion of BUFFER." 39 "Wait until a message reports reversion of BUFFER.
40This expects `auto-revert--messages' to be bound by
41`ert-with-message-capture' before calling."
36 (with-timeout (auto-revert--timeout nil) 42 (with-timeout (auto-revert--timeout nil)
37 (with-current-buffer "*Messages*" 43 (while
38 (while 44 (null (string-match
39 (null (string-match 45 (format-message "Reverting buffer `%s'." (buffer-name buffer))
40 (format-message "Reverting buffer `%s'." (buffer-name buffer)) 46 auto-revert--messages))
41 (buffer-string))) 47 (if (with-current-buffer buffer auto-revert-use-notify)
42 (if (with-current-buffer buffer auto-revert-use-notify) 48 (read-event nil nil 0.1)
43 (read-event nil nil 0.1) 49 (sleep-for 0.1)))))
44 (sleep-for 0.1))))))
45 50
46(ert-deftest auto-revert-test00-auto-revert-mode () 51(ert-deftest auto-revert-test00-auto-revert-mode ()
47 "Check autorevert for a file." 52 "Check autorevert for a file."
@@ -51,41 +56,38 @@
51 buf) 56 buf)
52 (unwind-protect 57 (unwind-protect
53 (progn 58 (progn
54 (with-current-buffer (get-buffer-create "*Messages*") 59 (write-region "any text" nil tmpfile nil 'no-message)
55 (narrow-to-region (point-max) (point-max)))
56 (write-region "any text" nil tmpfile nil 'no-message)
57 (setq buf (find-file-noselect tmpfile)) 60 (setq buf (find-file-noselect tmpfile))
58 (with-current-buffer buf 61 (with-current-buffer buf
59 (should (string-equal (buffer-string) "any text")) 62 (ert-with-message-capture auto-revert--messages
60 ;; `buffer-stale--default-function' checks for 63 (should (string-equal (buffer-string) "any text"))
61 ;; `verify-visited-file-modtime'. We must ensure that it 64 ;; `buffer-stale--default-function' checks for
62 ;; returns nil. 65 ;; `verify-visited-file-modtime'. We must ensure that it
63 (sleep-for 1) 66 ;; returns nil.
64 (auto-revert-mode 1) 67 (sleep-for 1)
65 (should auto-revert-mode) 68 (auto-revert-mode 1)
69 (should auto-revert-mode)
66 70
67 ;; Modify file. We wait for a second, in order to have 71 ;; Modify file. We wait for a second, in order to have
68 ;; another timestamp. 72 ;; another timestamp.
69 (sleep-for 1) 73 (sleep-for 1)
70 (write-region "another text" nil tmpfile nil 'no-message) 74 (write-region "another text" nil tmpfile nil 'no-message)
71 75
72 ;; Check, that the buffer has been reverted. 76 ;; Check, that the buffer has been reverted.
73 (auto-revert--wait-for-revert buf) 77 (auto-revert--wait-for-revert buf))
74 (should (string-match "another text" (buffer-string))) 78 (should (string-match "another text" (buffer-string)))
75 79
76 ;; When the buffer is modified, it shall not be reverted. 80 ;; When the buffer is modified, it shall not be reverted.
77 (with-current-buffer (get-buffer-create "*Messages*") 81 (ert-with-message-capture auto-revert--messages
78 (narrow-to-region (point-max) (point-max))) 82 (set-buffer-modified-p t)
79 (set-buffer-modified-p t) 83 (sleep-for 1)
80 (sleep-for 1) 84 (write-region "any text" nil tmpfile nil 'no-message)
81 (write-region "any text" nil tmpfile nil 'no-message)
82 85
83 ;; Check, that the buffer hasn't been reverted. 86 ;; Check, that the buffer hasn't been reverted.
84 (auto-revert--wait-for-revert buf) 87 (auto-revert--wait-for-revert buf))
85 (should-not (string-match "any text" (buffer-string))))) 88 (should-not (string-match "any text" (buffer-string)))))
86 89
87 ;; Exit. 90 ;; Exit.
88 (with-current-buffer "*Messages*" (widen))
89 (ignore-errors 91 (ignore-errors
90 (with-current-buffer buf (set-buffer-modified-p nil)) 92 (with-current-buffer buf (set-buffer-modified-p nil))
91 (kill-buffer buf)) 93 (kill-buffer buf))
@@ -106,13 +108,11 @@
106 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1))) 108 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
107 buf1 buf2) 109 buf1 buf2)
108 (unwind-protect 110 (unwind-protect
109 (progn 111 (ert-with-message-capture auto-revert--messages
110 (with-current-buffer (get-buffer-create "*Messages*") 112 (write-region "any text" nil tmpfile1 nil 'no-message)
111 (narrow-to-region (point-max) (point-max))) 113 (setq buf1 (find-file-noselect tmpfile1))
112 (write-region "any text" nil tmpfile1 nil 'no-message) 114 (write-region "any text" nil tmpfile2 nil 'no-message)
113 (setq buf1 (find-file-noselect tmpfile1)) 115 (setq buf2 (find-file-noselect tmpfile2))
114 (write-region "any text" nil tmpfile2 nil 'no-message)
115 (setq buf2 (find-file-noselect tmpfile2))
116 116
117 (dolist (buf (list buf1 buf2)) 117 (dolist (buf (list buf1 buf2))
118 (with-current-buffer buf 118 (with-current-buffer buf
@@ -148,7 +148,6 @@
148 (should (string-match "another text" (buffer-string)))))) 148 (should (string-match "another text" (buffer-string))))))
149 149
150 ;; Exit. 150 ;; Exit.
151 (with-current-buffer "*Messages*" (widen))
152 (ignore-errors 151 (ignore-errors
153 (dolist (buf (list buf1 buf2)) 152 (dolist (buf (list buf1 buf2))
154 (with-current-buffer buf (set-buffer-modified-p nil)) 153 (with-current-buffer buf (set-buffer-modified-p nil))
@@ -165,8 +164,6 @@
165 buf) 164 buf)
166 (unwind-protect 165 (unwind-protect
167 (progn 166 (progn
168 (with-current-buffer (get-buffer-create "*Messages*")
169 (narrow-to-region (point-max) (point-max)))
170 (write-region "any text" nil tmpfile nil 'no-message) 167 (write-region "any text" nil tmpfile nil 'no-message)
171 (setq buf (find-file-noselect tmpfile)) 168 (setq buf (find-file-noselect tmpfile))
172 (with-current-buffer buf 169 (with-current-buffer buf
@@ -184,42 +181,36 @@
184 'before-revert-hook 181 'before-revert-hook
185 (lambda () (delete-file buffer-file-name)) 182 (lambda () (delete-file buffer-file-name))
186 nil t) 183 nil t)
187 (with-current-buffer (get-buffer-create "*Messages*")
188 (narrow-to-region (point-max) (point-max)))
189 (sleep-for 1)
190 (write-region "another text" nil tmpfile nil 'no-message)
191 184
192 ;; Check, that the buffer hasn't been reverted. File 185 (ert-with-message-capture auto-revert--messages
193 ;; notification should be disabled, falling back to 186 (sleep-for 1)
194 ;; polling. 187 (write-region "another text" nil tmpfile nil 'no-message)
195 (auto-revert--wait-for-revert buf) 188 (auto-revert--wait-for-revert buf))
189 ;; Check, that the buffer hasn't been reverted. File
190 ;; notification should be disabled, falling back to
191 ;; polling.
196 (should (string-match "any text" (buffer-string))) 192 (should (string-match "any text" (buffer-string)))
197 (should-not auto-revert-use-notify) 193 (should-not auto-revert-use-notify)
198 194
199 ;; Once the file has been recreated, the buffer shall be 195 ;; Once the file has been recreated, the buffer shall be
200 ;; reverted. 196 ;; reverted.
201 (kill-local-variable 'before-revert-hook) 197 (kill-local-variable 'before-revert-hook)
202 (with-current-buffer (get-buffer-create "*Messages*") 198 (ert-with-message-capture auto-revert--messages
203 (narrow-to-region (point-max) (point-max))) 199 (sleep-for 1)
204 (sleep-for 1) 200 (write-region "another text" nil tmpfile nil 'no-message)
205 (write-region "another text" nil tmpfile nil 'no-message) 201 (auto-revert--wait-for-revert buf))
206 202 ;; Check, that the buffer has been reverted.
207 ;; Check, that the buffer has been reverted.
208 (auto-revert--wait-for-revert buf)
209 (should (string-match "another text" (buffer-string))) 203 (should (string-match "another text" (buffer-string)))
210 204
211 ;; An empty file shall still be reverted. 205 ;; An empty file shall still be reverted.
212 (with-current-buffer (get-buffer-create "*Messages*") 206 (ert-with-message-capture auto-revert--messages
213 (narrow-to-region (point-max) (point-max))) 207 (sleep-for 1)
214 (sleep-for 1) 208 (write-region "" nil tmpfile nil 'no-message)
215 (write-region "" nil tmpfile nil 'no-message) 209 (auto-revert--wait-for-revert buf))
216 210 ;; Check, that the buffer has been reverted.
217 ;; Check, that the buffer has been reverted.
218 (auto-revert--wait-for-revert buf)
219 (should (string-equal "" (buffer-string))))) 211 (should (string-equal "" (buffer-string)))))
220 212
221 ;; Exit. 213 ;; Exit.
222 (with-current-buffer "*Messages*" (widen))
223 (ignore-errors 214 (ignore-errors
224 (with-current-buffer buf (set-buffer-modified-p nil)) 215 (with-current-buffer buf (set-buffer-modified-p nil))
225 (kill-buffer buf)) 216 (kill-buffer buf))
@@ -232,9 +223,7 @@
232 (let ((tmpfile (make-temp-file "auto-revert-test")) 223 (let ((tmpfile (make-temp-file "auto-revert-test"))
233 buf) 224 buf)
234 (unwind-protect 225 (unwind-protect
235 (progn 226 (ert-with-message-capture auto-revert--messages
236 (with-current-buffer (get-buffer-create "*Messages*")
237 (narrow-to-region (point-max) (point-max)))
238 (write-region "any text" nil tmpfile nil 'no-message) 227 (write-region "any text" nil tmpfile nil 'no-message)
239 (setq buf (find-file-noselect tmpfile)) 228 (setq buf (find-file-noselect tmpfile))
240 (with-current-buffer buf 229 (with-current-buffer buf
@@ -259,7 +248,6 @@
259 (string-match "modified text\nanother text" (buffer-string))))) 248 (string-match "modified text\nanother text" (buffer-string)))))
260 249
261 ;; Exit. 250 ;; Exit.
262 (with-current-buffer "*Messages*" (widen))
263 (ignore-errors (kill-buffer buf)) 251 (ignore-errors (kill-buffer buf))
264 (ignore-errors (delete-file tmpfile))))) 252 (ignore-errors (delete-file tmpfile)))))
265 253
@@ -283,33 +271,29 @@
283 (should 271 (should
284 (string-match name (substring-no-properties (buffer-string)))) 272 (string-match name (substring-no-properties (buffer-string))))
285 273
286 ;; Delete file. We wait for a second, in order to have 274 (ert-with-message-capture auto-revert--messages
287 ;; another timestamp. 275 ;; Delete file. We wait for a second, in order to have
288 (with-current-buffer (get-buffer-create "*Messages*") 276 ;; another timestamp.
289 (narrow-to-region (point-max) (point-max))) 277 (sleep-for 1)
290 (sleep-for 1) 278 (delete-file tmpfile)
291 (delete-file tmpfile) 279 (auto-revert--wait-for-revert buf))
292 280 ;; Check, that the buffer has been reverted.
293 ;; Check, that the buffer has been reverted.
294 (auto-revert--wait-for-revert buf)
295 (should-not 281 (should-not
296 (string-match name (substring-no-properties (buffer-string)))) 282 (string-match name (substring-no-properties (buffer-string))))
297 283
298 ;; Make dired buffer modified. Check, that the buffer has 284 (ert-with-message-capture auto-revert--messages
299 ;; been still reverted. 285 ;; Make dired buffer modified. Check, that the buffer has
300 (with-current-buffer (get-buffer-create "*Messages*") 286 ;; been still reverted.
301 (narrow-to-region (point-max) (point-max))) 287 (set-buffer-modified-p t)
302 (set-buffer-modified-p t) 288 (sleep-for 1)
303 (sleep-for 1) 289 (write-region "any text" nil tmpfile nil 'no-message)
304 (write-region "any text" nil tmpfile nil 'no-message)
305 290
306 ;; Check, that the buffer has been reverted. 291 (auto-revert--wait-for-revert buf))
307 (auto-revert--wait-for-revert buf) 292 ;; Check, that the buffer has been reverted.
308 (should 293 (should
309 (string-match name (substring-no-properties (buffer-string)))))) 294 (string-match name (substring-no-properties (buffer-string))))))
310 295
311 ;; Exit. 296 ;; Exit.
312 (with-current-buffer "*Messages*" (widen))
313 (ignore-errors 297 (ignore-errors
314 (with-current-buffer buf (set-buffer-modified-p nil)) 298 (with-current-buffer buf (set-buffer-modified-p nil))
315 (kill-buffer buf)) 299 (kill-buffer buf))
diff --git a/test/lisp/filenotify-tests.el b/test/lisp/filenotify-tests.el
index db7f55e8fc5..27434bcef20 100644
--- a/test/lisp/filenotify-tests.el
+++ b/test/lisp/filenotify-tests.el
@@ -36,6 +36,7 @@
36;;; Code: 36;;; Code:
37 37
38(require 'ert) 38(require 'ert)
39(require 'ert-x)
39(require 'filenotify) 40(require 'filenotify)
40(require 'tramp) 41(require 'tramp)
41 42
@@ -703,21 +704,19 @@ delivered."
703 (should auto-revert-notify-watch-descriptor) 704 (should auto-revert-notify-watch-descriptor)
704 705
705 ;; Modify file. We wait for a second, in order to have 706 ;; Modify file. We wait for a second, in order to have
706 ;; another timestamp. 707 ;; another timestamp.
707 (with-current-buffer (get-buffer-create "*Messages*") 708 (ert-with-message-capture captured-messages
708 (narrow-to-region (point-max) (point-max))) 709 (sleep-for 1)
709 (sleep-for 1) 710 (write-region
710 (write-region 711 "another text" nil file-notify--test-tmpfile nil 'no-message)
711 "another text" nil file-notify--test-tmpfile nil 'no-message) 712
712 713 ;; Check, that the buffer has been reverted.
713 ;; Check, that the buffer has been reverted. 714 (file-notify--wait-for-events
714 (with-current-buffer (get-buffer-create "*Messages*") 715 timeout
715 (file-notify--wait-for-events 716 (string-match
716 timeout
717 (string-match
718 (format-message "Reverting buffer `%s'." (buffer-name buf)) 717 (format-message "Reverting buffer `%s'." (buffer-name buf))
719 (buffer-string)))) 718 captured-messages))
720 (should (string-match "another text" (buffer-string))) 719 (should (string-match "another text" (buffer-string))))
721 720
722 ;; Stop file notification. Autorevert shall still work via polling. 721 ;; Stop file notification. Autorevert shall still work via polling.
723 (file-notify-rm-watch auto-revert-notify-watch-descriptor) 722 (file-notify-rm-watch auto-revert-notify-watch-descriptor)
@@ -728,27 +727,24 @@ delivered."
728 727
729 ;; Modify file. We wait for two seconds, in order to 728 ;; Modify file. We wait for two seconds, in order to
730 ;; have another timestamp. One second seems to be too 729 ;; have another timestamp. One second seems to be too
731 ;; short. 730 ;; short.
732 (with-current-buffer (get-buffer-create "*Messages*") 731 (ert-with-message-capture captured-messages
733 (narrow-to-region (point-max) (point-max))) 732 (sleep-for 2)
734 (sleep-for 2) 733 (write-region
735 (write-region 734 "foo bla" nil file-notify--test-tmpfile nil 'no-message)
736 "foo bla" nil file-notify--test-tmpfile nil 'no-message) 735
737 736 ;; Check, that the buffer has been reverted.
738 ;; Check, that the buffer has been reverted. 737 (file-notify--wait-for-events
739 (with-current-buffer (get-buffer-create "*Messages*") 738 timeout
740 (file-notify--wait-for-events 739 (string-match
741 timeout 740 (format-message "Reverting buffer `%s'." (buffer-name buf))
742 (string-match 741 captured-messages))
743 (format-message "Reverting buffer `%s'." (buffer-name buf)) 742 (should (string-match "foo bla" (buffer-string)))))
744 (buffer-string))))
745 (should (string-match "foo bla" (buffer-string))))
746 743
747 ;; The environment shall be cleaned up. 744 ;; The environment shall be cleaned up.
748 (file-notify--test-cleanup-p)) 745 (file-notify--test-cleanup-p))
749 746
750 ;; Cleanup. 747 ;; Cleanup.
751 (with-current-buffer "*Messages*" (widen))
752 (ignore-errors (kill-buffer buf)) 748 (ignore-errors (kill-buffer buf))
753 (file-notify--test-cleanup)))) 749 (file-notify--test-cleanup))))
754 750