Merge branch 'master' into comment-cache
[emacs.git] / test / lisp / autorevert-tests.el
blobc6f103321c62f88904983f71b5a688f8c40a9ae8
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/'.
20 ;;; Commentary:
22 ;; A whole test run can be performed calling the command `auto-revert-test-all'.
24 ;;; Code:
26 (require 'ert)
27 (require 'ert-x)
28 (require 'autorevert)
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)
43 (while
44 (null (string-match
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)
49 (sleep-for 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"))
56 buf)
57 (unwind-protect
58 (progn
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
66 ;; returns nil.
67 (sleep-for 1)
68 (auto-revert-mode 1)
69 (should auto-revert-mode)
71 ;; Modify file. We wait for a second, in order to have
72 ;; another timestamp.
73 (sleep-for 1)
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)
83 (sleep-for 1)
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)))))
90 ;; Exit.
91 (ignore-errors
92 (with-current-buffer buf (set-buffer-modified-p nil))
93 (kill-buffer buf))
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))
105 (tmpfile1
106 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
107 (tmpfile2
108 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
109 buf1 buf2)
110 (unwind-protect
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
122 ;; it returns nil.
123 (sleep-for 1)
124 (auto-revert-mode 1)
125 (should auto-revert-mode)))
127 ;; Modify files. We wait for a second, in order to have
128 ;; another timestamp.
129 (sleep-for 1)
130 (write-region
131 "another text" nil
132 (expand-file-name (file-name-nondirectory tmpfile1) tmpdir2)
133 nil 'no-message)
134 (write-region
135 "another text" nil
136 (expand-file-name (file-name-nondirectory tmpfile2) tmpdir2)
137 nil 'no-message)
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))))))
150 ;; Exit.
151 (ignore-errors
152 (dolist (buf (list buf1 buf2))
153 (with-current-buffer buf (set-buffer-modified-p nil))
154 (kill-buffer buf)))
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"))
164 buf)
165 (unwind-protect
166 (progn
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
173 ;; it returns nil.
174 (sleep-for 1)
175 (auto-revert-mode 1)
176 (should auto-revert-mode)
178 ;; Remove file while reverting. We simulate this by
179 ;; modifying `before-revert-hook'.
180 (add-hook
181 'before-revert-hook
182 (lambda () (delete-file buffer-file-name))
183 nil t)
185 (ert-with-message-capture auto-revert--messages
186 (sleep-for 1)
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
191 ;; polling.
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
198 ;; reverted.
199 (kill-local-variable 'before-revert-hook)
200 (ert-with-message-capture auto-revert--messages
201 (sleep-for 1)
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
209 (sleep-for 1)
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)))))
215 ;; Exit.
216 (ignore-errors
217 (with-current-buffer buf (set-buffer-modified-p nil))
218 (kill-buffer buf))
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"))
226 buf)
227 (unwind-protect
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
234 ;; returns nil.
235 (sleep-for 1)
236 (auto-revert-tail-mode 1)
237 (should auto-revert-tail-mode)
238 (erase-buffer)
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.
244 (sleep-for 1)
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)
249 (should
250 (string-match "modified text\nanother text" (buffer-string)))))
252 ;; Exit.
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))
262 buf)
263 (unwind-protect
264 (progn
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
269 ;; returns nil.
270 (sleep-for 1)
271 (auto-revert-mode 1)
272 (should auto-revert-mode)
273 (should
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.
279 (sleep-for 1)
280 (delete-file tmpfile)
281 (auto-revert--wait-for-revert buf))
282 ;; Check, that the buffer has been reverted.
283 (should-not
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)
290 (sleep-for 1)
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.
295 (should
296 (string-match name (substring-no-properties (buffer-string))))))
298 ;; Exit.
299 (ignore-errors
300 (with-current-buffer buf (set-buffer-modified-p nil))
301 (kill-buffer buf))
302 (ignore-errors (delete-file tmpfile)))))
304 (defun auto-revert-test-all (&optional interactive)
305 "Run all tests for \\[auto-revert]."
306 (interactive "p")
307 (if interactive
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