1 ;;; auto-revert-tests.el --- Tests of auto-revert
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
22 ;; A whole test run can be performed calling the command `auto-revert-test-all'.
28 (setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
29 auto-revert-stop-on-user-input nil)
31 (defconst auto-revert--timeout 10
32 "Time to wait until a message appears in the *Messages* buffer.")
34 (defun auto-revert--wait-for-revert (buffer)
35 "Wait until the *Messages* buffer reports reversion of BUFFER."
36 (with-timeout (auto-revert--timeout nil)
37 (with-current-buffer "*Messages*"
40 (format-message "Reverting buffer `%s'." (buffer-name buffer))
42 (read-event nil nil 0.1)))))
44 (ert-deftest auto-revert-test00-auto-revert-mode ()
45 "Check autorevert for a file."
46 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
47 ;; file has been reverted.
48 (let ((tmpfile (make-temp-file "auto-revert-test"))
52 (with-current-buffer (get-buffer-create "*Messages*")
53 (narrow-to-region (point-max) (point-max)))
54 (write-region "any text" nil tmpfile nil 'no-message)
55 (setq buf (find-file-noselect tmpfile))
56 (with-current-buffer buf
57 (should (string-equal (buffer-string) "any text"))
58 ;; `buffer-stale--default-function' checks for
59 ;; `verify-visited-file-modtime'. We must ensure that it
63 (should auto-revert-mode)
65 ;; Modify file. We wait for a second, in order to have
68 (write-region "another text" nil tmpfile nil 'no-message)
70 ;; Check, that the buffer has been reverted.
71 (auto-revert--wait-for-revert buf)
72 (should (string-match "another text" (buffer-string)))
74 ;; When the buffer is modified, it shall not be reverted.
75 (with-current-buffer (get-buffer-create "*Messages*")
76 (narrow-to-region (point-max) (point-max)))
77 (set-buffer-modified-p t)
79 (write-region "any text" nil tmpfile nil 'no-message)
81 ;; Check, that the buffer hasn't been reverted.
82 (auto-revert--wait-for-revert buf)
83 (should-not (string-match "any text" (buffer-string)))))
86 (with-current-buffer "*Messages*" (widen))
88 (with-current-buffer buf (set-buffer-modified-p nil))
90 (ignore-errors (delete-file tmpfile)))))
92 (ert-deftest auto-revert-test01-auto-revert-tail-mode ()
93 "Check autorevert tail mode."
94 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
95 ;; file has been reverted.
96 (let ((tmpfile (make-temp-file "auto-revert-test"))
100 (with-current-buffer (get-buffer-create "*Messages*")
101 (narrow-to-region (point-max) (point-max)))
102 (write-region "any text" nil tmpfile nil 'no-message)
103 (setq buf (find-file-noselect tmpfile))
104 (with-current-buffer buf
105 ;; `buffer-stale--default-function' checks for
106 ;; `verify-visited-file-modtime'. We must ensure that it
109 (auto-revert-tail-mode 1)
110 (should auto-revert-tail-mode)
112 (insert "modified text\n")
113 (set-buffer-modified-p nil)
115 ;; Modify file. We wait for a second, in order to have
116 ;; another timestamp.
118 (write-region "another text" nil tmpfile 'append 'no-message)
120 ;; Check, that the buffer has been reverted.
121 (auto-revert--wait-for-revert buf)
123 (string-match "modified text\nanother text" (buffer-string)))))
126 (with-current-buffer "*Messages*" (widen))
127 (ignore-errors (kill-buffer buf))
128 (ignore-errors (delete-file tmpfile)))))
130 (ert-deftest auto-revert-test02-auto-revert-mode-dired ()
131 "Check autorevert for dired."
132 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
133 ;; file has been reverted.
134 (let* ((tmpfile (make-temp-file "auto-revert-test"))
135 (name (file-name-nondirectory tmpfile))
139 (with-current-buffer (get-buffer-create "*Messages*")
140 (narrow-to-region (point-max) (point-max)))
141 (setq buf (dired-noselect temporary-file-directory))
142 (with-current-buffer buf
143 ;; `buffer-stale--default-function' checks for
144 ;; `verify-visited-file-modtime'. We must ensure that it
148 (should auto-revert-mode)
150 (string-match name (substring-no-properties (buffer-string))))
152 ;; Delete file. We wait for a second, in order to have
153 ;; another timestamp.
155 (delete-file tmpfile)
157 ;; Check, that the buffer has been reverted.
158 (auto-revert--wait-for-revert buf)
160 (string-match name (substring-no-properties (buffer-string))))
162 ;; Make dired buffer modified. Check, that the buffer has
163 ;; been still reverted.
164 (with-current-buffer (get-buffer-create "*Messages*")
165 (narrow-to-region (point-max) (point-max)))
166 (set-buffer-modified-p t)
168 (write-region "any text" nil tmpfile nil 'no-message)
170 ;; Check, that the buffer has been reverted.
171 (auto-revert--wait-for-revert buf)
173 (string-match name (substring-no-properties (buffer-string))))))
176 (with-current-buffer "*Messages*" (widen))
178 (with-current-buffer buf (set-buffer-modified-p nil))
180 (ignore-errors (delete-file tmpfile)))))
182 (defun auto-revert-test-all (&optional interactive)
183 "Run all tests for \\[auto-revert]."
186 (ert-run-tests-interactively "^auto-revert-")
187 (ert-run-tests-batch "^auto-revert-")))
189 (provide 'auto-revert-tests)
190 ;;; auto-revert-tests.el ends here