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.
This commit is contained in:
Gemini Lasswell 2017-02-04 13:36:43 +02:00 committed by Eli Zaretskii
parent 8ba27b7ce2
commit ef3d8d6f72
3 changed files with 123 additions and 119 deletions

View file

@ -285,6 +285,30 @@ BUFFER defaults to current buffer. Does not modify BUFFER."
(kill-buffer clone))))))) (kill-buffer clone)))))))
(defmacro ert-with-message-capture (var &rest body)
"Execute BODY while collecting anything written with `message' in VAR.
Capture all messages produced by `message' when it is called from
Lisp, and concatenate them separated by newlines into one string.
This is useful for separating the issuance of messages by the
code under test from the behavior of the *Messages* buffer."
(declare (debug (symbolp body))
(indent 1))
(let ((g-advice (cl-gensym)))
`(let* ((,var "")
(,g-advice (lambda (func &rest args)
(if (or (null args) (equal (car args) ""))
(apply func args)
(let ((msg (apply #'format-message args)))
(setq ,var (concat ,var msg "\n"))
(funcall func "%s" msg))))))
(advice-add 'message :around ,g-advice)
(unwind-protect
(progn ,@body)
(advice-remove 'message ,g-advice)))))
(provide 'ert-x) (provide 'ert-x)
;;; ert-x.el ends here ;;; ert-x.el ends here

View file

@ -24,24 +24,29 @@
;;; Code: ;;; Code:
(require 'ert) (require 'ert)
(require 'ert-x)
(require 'autorevert) (require 'autorevert)
(setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded" (setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
auto-revert-stop-on-user-input nil) auto-revert-stop-on-user-input nil)
(defconst auto-revert--timeout 10 (defconst auto-revert--timeout 10
"Time to wait until a message appears in the *Messages* buffer.") "Time to wait for a message.")
(defvar auto-revert--messages nil
"Used to collect messages issued during a section of a test.")
(defun auto-revert--wait-for-revert (buffer) (defun auto-revert--wait-for-revert (buffer)
"Wait until the *Messages* buffer reports reversion of BUFFER." "Wait until a message reports reversion of BUFFER.
This expects `auto-revert--messages' to be bound by
`ert-with-message-capture' before calling."
(with-timeout (auto-revert--timeout nil) (with-timeout (auto-revert--timeout nil)
(with-current-buffer "*Messages*" (while
(while (null (string-match
(null (string-match (format-message "Reverting buffer `%s'." (buffer-name buffer))
(format-message "Reverting buffer `%s'." (buffer-name buffer)) auto-revert--messages))
(buffer-string))) (if (with-current-buffer buffer auto-revert-use-notify)
(if (with-current-buffer buffer auto-revert-use-notify) (read-event nil nil 0.1)
(read-event nil nil 0.1) (sleep-for 0.1)))))
(sleep-for 0.1))))))
(ert-deftest auto-revert-test00-auto-revert-mode () (ert-deftest auto-revert-test00-auto-revert-mode ()
"Check autorevert for a file." "Check autorevert for a file."
@ -51,41 +56,38 @@
buf) buf)
(unwind-protect (unwind-protect
(progn (progn
(with-current-buffer (get-buffer-create "*Messages*") (write-region "any text" nil tmpfile nil 'no-message)
(narrow-to-region (point-max) (point-max)))
(write-region "any text" nil tmpfile nil 'no-message)
(setq buf (find-file-noselect tmpfile)) (setq buf (find-file-noselect tmpfile))
(with-current-buffer buf (with-current-buffer buf
(should (string-equal (buffer-string) "any text")) (ert-with-message-capture auto-revert--messages
;; `buffer-stale--default-function' checks for (should (string-equal (buffer-string) "any text"))
;; `verify-visited-file-modtime'. We must ensure that it ;; `buffer-stale--default-function' checks for
;; returns nil. ;; `verify-visited-file-modtime'. We must ensure that it
(sleep-for 1) ;; returns nil.
(auto-revert-mode 1) (sleep-for 1)
(should auto-revert-mode) (auto-revert-mode 1)
(should auto-revert-mode)
;; Modify file. We wait for a second, in order to have ;; Modify file. We wait for a second, in order to have
;; another timestamp. ;; another timestamp.
(sleep-for 1) (sleep-for 1)
(write-region "another text" nil tmpfile nil 'no-message) (write-region "another text" nil tmpfile nil 'no-message)
;; Check, that the buffer has been reverted. ;; Check, that the buffer has been reverted.
(auto-revert--wait-for-revert buf) (auto-revert--wait-for-revert buf))
(should (string-match "another text" (buffer-string))) (should (string-match "another text" (buffer-string)))
;; When the buffer is modified, it shall not be reverted. ;; When the buffer is modified, it shall not be reverted.
(with-current-buffer (get-buffer-create "*Messages*") (ert-with-message-capture auto-revert--messages
(narrow-to-region (point-max) (point-max))) (set-buffer-modified-p t)
(set-buffer-modified-p t) (sleep-for 1)
(sleep-for 1) (write-region "any text" nil tmpfile nil 'no-message)
(write-region "any text" nil tmpfile nil 'no-message)
;; Check, that the buffer hasn't been reverted. ;; Check, that the buffer hasn't been reverted.
(auto-revert--wait-for-revert buf) (auto-revert--wait-for-revert buf))
(should-not (string-match "any text" (buffer-string))))) (should-not (string-match "any text" (buffer-string)))))
;; Exit. ;; Exit.
(with-current-buffer "*Messages*" (widen))
(ignore-errors (ignore-errors
(with-current-buffer buf (set-buffer-modified-p nil)) (with-current-buffer buf (set-buffer-modified-p nil))
(kill-buffer buf)) (kill-buffer buf))
@ -106,13 +108,11 @@
(make-temp-file (expand-file-name "auto-revert-test" tmpdir1))) (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
buf1 buf2) buf1 buf2)
(unwind-protect (unwind-protect
(progn (ert-with-message-capture auto-revert--messages
(with-current-buffer (get-buffer-create "*Messages*") (write-region "any text" nil tmpfile1 nil 'no-message)
(narrow-to-region (point-max) (point-max))) (setq buf1 (find-file-noselect tmpfile1))
(write-region "any text" nil tmpfile1 nil 'no-message) (write-region "any text" nil tmpfile2 nil 'no-message)
(setq buf1 (find-file-noselect tmpfile1)) (setq buf2 (find-file-noselect tmpfile2))
(write-region "any text" nil tmpfile2 nil 'no-message)
(setq buf2 (find-file-noselect tmpfile2))
(dolist (buf (list buf1 buf2)) (dolist (buf (list buf1 buf2))
(with-current-buffer buf (with-current-buffer buf
@ -148,7 +148,6 @@
(should (string-match "another text" (buffer-string)))))) (should (string-match "another text" (buffer-string))))))
;; Exit. ;; Exit.
(with-current-buffer "*Messages*" (widen))
(ignore-errors (ignore-errors
(dolist (buf (list buf1 buf2)) (dolist (buf (list buf1 buf2))
(with-current-buffer buf (set-buffer-modified-p nil)) (with-current-buffer buf (set-buffer-modified-p nil))
@ -165,8 +164,6 @@
buf) buf)
(unwind-protect (unwind-protect
(progn (progn
(with-current-buffer (get-buffer-create "*Messages*")
(narrow-to-region (point-max) (point-max)))
(write-region "any text" nil tmpfile nil 'no-message) (write-region "any text" nil tmpfile nil 'no-message)
(setq buf (find-file-noselect tmpfile)) (setq buf (find-file-noselect tmpfile))
(with-current-buffer buf (with-current-buffer buf
@ -184,42 +181,36 @@
'before-revert-hook 'before-revert-hook
(lambda () (delete-file buffer-file-name)) (lambda () (delete-file buffer-file-name))
nil t) nil t)
(with-current-buffer (get-buffer-create "*Messages*")
(narrow-to-region (point-max) (point-max)))
(sleep-for 1)
(write-region "another text" nil tmpfile nil 'no-message)
;; Check, that the buffer hasn't been reverted. File (ert-with-message-capture auto-revert--messages
;; notification should be disabled, falling back to (sleep-for 1)
;; polling. (write-region "another text" nil tmpfile nil 'no-message)
(auto-revert--wait-for-revert buf) (auto-revert--wait-for-revert buf))
;; Check, that the buffer hasn't been reverted. File
;; notification should be disabled, falling back to
;; polling.
(should (string-match "any text" (buffer-string))) (should (string-match "any text" (buffer-string)))
(should-not auto-revert-use-notify) (should-not auto-revert-use-notify)
;; Once the file has been recreated, the buffer shall be ;; Once the file has been recreated, the buffer shall be
;; reverted. ;; reverted.
(kill-local-variable 'before-revert-hook) (kill-local-variable 'before-revert-hook)
(with-current-buffer (get-buffer-create "*Messages*") (ert-with-message-capture auto-revert--messages
(narrow-to-region (point-max) (point-max))) (sleep-for 1)
(sleep-for 1) (write-region "another text" nil tmpfile nil 'no-message)
(write-region "another text" nil tmpfile nil 'no-message) (auto-revert--wait-for-revert buf))
;; Check, that the buffer has been reverted.
;; Check, that the buffer has been reverted.
(auto-revert--wait-for-revert buf)
(should (string-match "another text" (buffer-string))) (should (string-match "another text" (buffer-string)))
;; An empty file shall still be reverted. ;; An empty file shall still be reverted.
(with-current-buffer (get-buffer-create "*Messages*") (ert-with-message-capture auto-revert--messages
(narrow-to-region (point-max) (point-max))) (sleep-for 1)
(sleep-for 1) (write-region "" nil tmpfile nil 'no-message)
(write-region "" nil tmpfile nil 'no-message) (auto-revert--wait-for-revert buf))
;; Check, that the buffer has been reverted.
;; Check, that the buffer has been reverted.
(auto-revert--wait-for-revert buf)
(should (string-equal "" (buffer-string))))) (should (string-equal "" (buffer-string)))))
;; Exit. ;; Exit.
(with-current-buffer "*Messages*" (widen))
(ignore-errors (ignore-errors
(with-current-buffer buf (set-buffer-modified-p nil)) (with-current-buffer buf (set-buffer-modified-p nil))
(kill-buffer buf)) (kill-buffer buf))
@ -232,9 +223,7 @@
(let ((tmpfile (make-temp-file "auto-revert-test")) (let ((tmpfile (make-temp-file "auto-revert-test"))
buf) buf)
(unwind-protect (unwind-protect
(progn (ert-with-message-capture auto-revert--messages
(with-current-buffer (get-buffer-create "*Messages*")
(narrow-to-region (point-max) (point-max)))
(write-region "any text" nil tmpfile nil 'no-message) (write-region "any text" nil tmpfile nil 'no-message)
(setq buf (find-file-noselect tmpfile)) (setq buf (find-file-noselect tmpfile))
(with-current-buffer buf (with-current-buffer buf
@ -259,7 +248,6 @@
(string-match "modified text\nanother text" (buffer-string))))) (string-match "modified text\nanother text" (buffer-string)))))
;; Exit. ;; Exit.
(with-current-buffer "*Messages*" (widen))
(ignore-errors (kill-buffer buf)) (ignore-errors (kill-buffer buf))
(ignore-errors (delete-file tmpfile))))) (ignore-errors (delete-file tmpfile)))))
@ -283,33 +271,29 @@
(should (should
(string-match name (substring-no-properties (buffer-string)))) (string-match name (substring-no-properties (buffer-string))))
;; Delete file. We wait for a second, in order to have (ert-with-message-capture auto-revert--messages
;; another timestamp. ;; Delete file. We wait for a second, in order to have
(with-current-buffer (get-buffer-create "*Messages*") ;; another timestamp.
(narrow-to-region (point-max) (point-max))) (sleep-for 1)
(sleep-for 1) (delete-file tmpfile)
(delete-file tmpfile) (auto-revert--wait-for-revert buf))
;; Check, that the buffer has been reverted.
;; Check, that the buffer has been reverted.
(auto-revert--wait-for-revert buf)
(should-not (should-not
(string-match name (substring-no-properties (buffer-string)))) (string-match name (substring-no-properties (buffer-string))))
;; Make dired buffer modified. Check, that the buffer has (ert-with-message-capture auto-revert--messages
;; been still reverted. ;; Make dired buffer modified. Check, that the buffer has
(with-current-buffer (get-buffer-create "*Messages*") ;; been still reverted.
(narrow-to-region (point-max) (point-max))) (set-buffer-modified-p t)
(set-buffer-modified-p t) (sleep-for 1)
(sleep-for 1) (write-region "any text" nil tmpfile nil 'no-message)
(write-region "any text" nil tmpfile nil 'no-message)
;; Check, that the buffer has been reverted. (auto-revert--wait-for-revert buf))
(auto-revert--wait-for-revert buf) ;; Check, that the buffer has been reverted.
(should (should
(string-match name (substring-no-properties (buffer-string)))))) (string-match name (substring-no-properties (buffer-string))))))
;; Exit. ;; Exit.
(with-current-buffer "*Messages*" (widen))
(ignore-errors (ignore-errors
(with-current-buffer buf (set-buffer-modified-p nil)) (with-current-buffer buf (set-buffer-modified-p nil))
(kill-buffer buf)) (kill-buffer buf))

View file

@ -36,6 +36,7 @@
;;; Code: ;;; Code:
(require 'ert) (require 'ert)
(require 'ert-x)
(require 'filenotify) (require 'filenotify)
(require 'tramp) (require 'tramp)
@ -703,21 +704,19 @@ delivered."
(should auto-revert-notify-watch-descriptor) (should auto-revert-notify-watch-descriptor)
;; Modify file. We wait for a second, in order to have ;; Modify file. We wait for a second, in order to have
;; another timestamp. ;; another timestamp.
(with-current-buffer (get-buffer-create "*Messages*") (ert-with-message-capture captured-messages
(narrow-to-region (point-max) (point-max))) (sleep-for 1)
(sleep-for 1) (write-region
(write-region "another text" nil file-notify--test-tmpfile nil 'no-message)
"another text" nil file-notify--test-tmpfile nil 'no-message)
;; Check, that the buffer has been reverted. ;; Check, that the buffer has been reverted.
(with-current-buffer (get-buffer-create "*Messages*") (file-notify--wait-for-events
(file-notify--wait-for-events timeout
timeout (string-match
(string-match
(format-message "Reverting buffer `%s'." (buffer-name buf)) (format-message "Reverting buffer `%s'." (buffer-name buf))
(buffer-string)))) captured-messages))
(should (string-match "another text" (buffer-string))) (should (string-match "another text" (buffer-string))))
;; Stop file notification. Autorevert shall still work via polling. ;; Stop file notification. Autorevert shall still work via polling.
(file-notify-rm-watch auto-revert-notify-watch-descriptor) (file-notify-rm-watch auto-revert-notify-watch-descriptor)
@ -728,27 +727,24 @@ delivered."
;; Modify file. We wait for two seconds, in order to ;; Modify file. We wait for two seconds, in order to
;; have another timestamp. One second seems to be too ;; have another timestamp. One second seems to be too
;; short. ;; short.
(with-current-buffer (get-buffer-create "*Messages*") (ert-with-message-capture captured-messages
(narrow-to-region (point-max) (point-max))) (sleep-for 2)
(sleep-for 2) (write-region
(write-region "foo bla" nil file-notify--test-tmpfile nil 'no-message)
"foo bla" nil file-notify--test-tmpfile nil 'no-message)
;; Check, that the buffer has been reverted. ;; Check, that the buffer has been reverted.
(with-current-buffer (get-buffer-create "*Messages*") (file-notify--wait-for-events
(file-notify--wait-for-events timeout
timeout (string-match
(string-match (format-message "Reverting buffer `%s'." (buffer-name buf))
(format-message "Reverting buffer `%s'." (buffer-name buf)) captured-messages))
(buffer-string)))) (should (string-match "foo bla" (buffer-string)))))
(should (string-match "foo bla" (buffer-string))))
;; The environment shall be cleaned up. ;; The environment shall be cleaned up.
(file-notify--test-cleanup-p)) (file-notify--test-cleanup-p))
;; Cleanup. ;; Cleanup.
(with-current-buffer "*Messages*" (widen))
(ignore-errors (kill-buffer buf)) (ignore-errors (kill-buffer buf))
(file-notify--test-cleanup)))) (file-notify--test-cleanup))))