1 ;;; auto-revert-tests.el --- Tests of auto-revert
3 ;; Copyright (C) 2015-2017 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'.
29 (setq auto-revert-notify-exclude-dir-regexp
"nothing-to-be-excluded"
30 auto-revert-stop-on-user-input nil
)
32 (defconst auto-revert--timeout
10
33 "Time to wait for a message.")
35 (defvar auto-revert--messages nil
36 "Used to collect messages issued during a section of a test.")
38 (defun auto-revert--wait-for-revert (buffer)
39 "Wait until a message reports reversion of BUFFER.
40 This expects `auto-revert--messages' to be bound by
41 `ert-with-message-capture' before calling."
42 (with-timeout (auto-revert--timeout nil
)
45 (format-message "Reverting buffer `%s'." (buffer-name buffer
))
46 auto-revert--messages
))
47 (if (with-current-buffer buffer auto-revert-use-notify
)
48 (read-event nil nil
0.1)
51 (ert-deftest auto-revert-test00-auto-revert-mode
()
52 "Check autorevert for a file."
53 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
54 ;; file has been reverted.
55 (let ((tmpfile (make-temp-file "auto-revert-test"))
59 (write-region "any text" nil tmpfile nil
'no-message
)
60 (setq buf
(find-file-noselect tmpfile
))
61 (with-current-buffer buf
62 (ert-with-message-capture auto-revert--messages
63 (should (string-equal (buffer-string) "any text"))
64 ;; `buffer-stale--default-function' checks for
65 ;; `verify-visited-file-modtime'. We must ensure that it
69 (should auto-revert-mode
)
71 ;; Modify file. We wait for a second, in order to have
74 (write-region "another text" nil tmpfile nil
'no-message
)
76 ;; Check, that the buffer has been reverted.
77 (auto-revert--wait-for-revert buf
))
78 (should (string-match "another text" (buffer-string)))
80 ;; When the buffer is modified, it shall not be reverted.
81 (ert-with-message-capture auto-revert--messages
82 (set-buffer-modified-p t
)
84 (write-region "any text" nil tmpfile nil
'no-message
)
86 ;; Check, that the buffer hasn't been reverted.
87 (auto-revert--wait-for-revert buf
))
88 (should-not (string-match "any text" (buffer-string)))))
92 (with-current-buffer buf
(set-buffer-modified-p nil
))
94 (ignore-errors (delete-file tmpfile
)))))
96 ;; This is inspired by Bug#21841.
97 (ert-deftest auto-revert-test01-auto-revert-several-files
()
98 "Check autorevert for several files at once."
99 :tags
'(:expensive-test
)
100 (skip-unless (executable-find "cp"))
102 (let* ((cp (executable-find "cp"))
103 (tmpdir1 (make-temp-file "auto-revert-test" 'dir
))
104 (tmpdir2 (make-temp-file "auto-revert-test" 'dir
))
106 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1
)))
108 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1
)))
111 (ert-with-message-capture auto-revert--messages
112 (write-region "any text" nil tmpfile1 nil
'no-message
)
113 (setq buf1
(find-file-noselect tmpfile1
))
114 (write-region "any text" nil tmpfile2 nil
'no-message
)
115 (setq buf2
(find-file-noselect tmpfile2
))
117 (dolist (buf (list buf1 buf2
))
118 (with-current-buffer buf
119 (should (string-equal (buffer-string) "any text"))
120 ;; `buffer-stale--default-function' checks for
121 ;; `verify-visited-file-modtime'. We must ensure that
125 (should auto-revert-mode
)))
127 ;; Modify files. We wait for a second, in order to have
128 ;; another timestamp.
132 (expand-file-name (file-name-nondirectory tmpfile1
) tmpdir2
)
136 (expand-file-name (file-name-nondirectory tmpfile2
) tmpdir2
)
138 ;;(copy-directory tmpdir2 tmpdir1 nil 'copy-contents)
139 ;; Strange, that `copy-directory' does not work as expected.
140 ;; The following shell command is not portable on all
141 ;; platforms, unfortunately.
142 (shell-command (format "%s -f %s/* %s" cp tmpdir2 tmpdir1
))
144 ;; Check, that the buffers have been reverted.
145 (dolist (buf (list buf1 buf2
))
146 (with-current-buffer buf
147 (auto-revert--wait-for-revert buf
)
148 (should (string-match "another text" (buffer-string))))))
152 (dolist (buf (list buf1 buf2
))
153 (with-current-buffer buf
(set-buffer-modified-p nil
))
155 (ignore-errors (delete-directory tmpdir1
'recursive
))
156 (ignore-errors (delete-directory tmpdir2
'recursive
)))))
158 ;; This is inspired by Bug#23276.
159 (ert-deftest auto-revert-test02-auto-revert-deleted-file
()
160 "Check autorevert for a deleted file."
161 :tags
'(:expensive-test
)
163 (let ((tmpfile (make-temp-file "auto-revert-test"))
167 (write-region "any text" nil tmpfile nil
'no-message
)
168 (setq buf
(find-file-noselect tmpfile
))
169 (with-current-buffer buf
170 (should (string-equal (buffer-string) "any text"))
171 ;; `buffer-stale--default-function' checks for
172 ;; `verify-visited-file-modtime'. We must ensure that
176 (should auto-revert-mode
)
178 ;; Remove file while reverting. We simulate this by
179 ;; modifying `before-revert-hook'.
182 (lambda () (delete-file buffer-file-name
))
185 (ert-with-message-capture auto-revert--messages
187 (write-region "another text" nil tmpfile nil
'no-message
)
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
192 (should (string-match "any text" (buffer-string)))
193 ;; With w32notify, the 'stopped' events are not sent.
194 (or (eq file-notify--library
'w32notify
)
195 (should-not auto-revert-use-notify
))
197 ;; Once the file has been recreated, the buffer shall be
199 (kill-local-variable 'before-revert-hook
)
200 (ert-with-message-capture auto-revert--messages
202 (write-region "another text" nil tmpfile nil
'no-message
)
203 (auto-revert--wait-for-revert buf
))
204 ;; Check, that the buffer has been reverted.
205 (should (string-match "another text" (buffer-string)))
207 ;; An empty file shall still be reverted.
208 (ert-with-message-capture auto-revert--messages
210 (write-region "" nil tmpfile nil
'no-message
)
211 (auto-revert--wait-for-revert buf
))
212 ;; Check, that the buffer has been reverted.
213 (should (string-equal "" (buffer-string)))))
217 (with-current-buffer buf
(set-buffer-modified-p nil
))
219 (ignore-errors (delete-file tmpfile
)))))
221 (ert-deftest auto-revert-test03-auto-revert-tail-mode
()
222 "Check autorevert tail mode."
223 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
224 ;; file has been reverted.
225 (let ((tmpfile (make-temp-file "auto-revert-test"))
228 (ert-with-message-capture auto-revert--messages
229 (write-region "any text" nil tmpfile nil
'no-message
)
230 (setq buf
(find-file-noselect tmpfile
))
231 (with-current-buffer buf
232 ;; `buffer-stale--default-function' checks for
233 ;; `verify-visited-file-modtime'. We must ensure that it
236 (auto-revert-tail-mode 1)
237 (should auto-revert-tail-mode
)
239 (insert "modified text\n")
240 (set-buffer-modified-p nil
)
242 ;; Modify file. We wait for a second, in order to have
243 ;; another timestamp.
245 (write-region "another text" nil tmpfile
'append
'no-message
)
247 ;; Check, that the buffer has been reverted.
248 (auto-revert--wait-for-revert buf
)
250 (string-match "modified text\nanother text" (buffer-string)))))
253 (ignore-errors (kill-buffer buf
))
254 (ignore-errors (delete-file tmpfile
)))))
256 (ert-deftest auto-revert-test04-auto-revert-mode-dired
()
257 "Check autorevert for dired."
258 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
259 ;; file has been reverted.
260 (let* ((tmpfile (make-temp-file "auto-revert-test"))
261 (name (file-name-nondirectory tmpfile
))
265 (setq buf
(dired-noselect temporary-file-directory
))
266 (with-current-buffer buf
267 ;; `buffer-stale--default-function' checks for
268 ;; `verify-visited-file-modtime'. We must ensure that it
272 (should auto-revert-mode
)
274 (string-match name
(substring-no-properties (buffer-string))))
276 (ert-with-message-capture auto-revert--messages
277 ;; Delete file. We wait for a second, in order to have
278 ;; another timestamp.
280 (delete-file tmpfile
)
281 (auto-revert--wait-for-revert buf
))
282 ;; Check, that the buffer has been reverted.
284 (string-match name
(substring-no-properties (buffer-string))))
286 (ert-with-message-capture auto-revert--messages
287 ;; Make dired buffer modified. Check, that the buffer has
288 ;; been still reverted.
289 (set-buffer-modified-p t
)
291 (write-region "any text" nil tmpfile nil
'no-message
)
293 (auto-revert--wait-for-revert buf
))
294 ;; Check, that the buffer has been reverted.
296 (string-match name
(substring-no-properties (buffer-string))))))
300 (with-current-buffer buf
(set-buffer-modified-p nil
))
302 (ignore-errors (delete-file tmpfile
)))))
304 (defun auto-revert-test-all (&optional interactive
)
305 "Run all tests for \\[auto-revert]."
308 (ert-run-tests-interactively "^auto-revert-")
309 (ert-run-tests-batch "^auto-revert-")))
311 (provide 'auto-revert-tests
)
312 ;;; auto-revert-tests.el ends here