]> code.delx.au - gnu-emacs/blob - test/lisp/autorevert-tests.el
Merge from origin/emacs-25
[gnu-emacs] / test / lisp / autorevert-tests.el
1 ;;; auto-revert-tests.el --- Tests of auto-revert
2
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6
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.
11 ;;
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.
16 ;;
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/'.
19
20 ;;; Commentary:
21
22 ;; A whole test run can be performed calling the command `auto-revert-test-all'.
23
24 ;;; Code:
25
26 (require 'ert)
27 (require 'autorevert)
28 (setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
29 auto-revert-stop-on-user-input nil)
30
31 (defconst auto-revert--timeout 10
32 "Time to wait until a message appears in the *Messages* buffer.")
33
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*"
38 (while
39 (null (string-match
40 (format-message "Reverting buffer `%s'." (buffer-name buffer))
41 (buffer-string)))
42 (if (with-current-buffer buffer auto-revert-use-notify)
43 (read-event nil nil 0.1)
44 (sleep-for 0.1))))))
45
46 (ert-deftest auto-revert-test00-auto-revert-mode ()
47 "Check autorevert for a file."
48 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
49 ;; file has been reverted.
50 (let ((tmpfile (make-temp-file "auto-revert-test"))
51 buf)
52 (unwind-protect
53 (progn
54 (with-current-buffer (get-buffer-create "*Messages*")
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))
58 (with-current-buffer buf
59 (should (string-equal (buffer-string) "any text"))
60 ;; `buffer-stale--default-function' checks for
61 ;; `verify-visited-file-modtime'. We must ensure that it
62 ;; returns nil.
63 (sleep-for 1)
64 (auto-revert-mode 1)
65 (should auto-revert-mode)
66
67 ;; Modify file. We wait for a second, in order to have
68 ;; another timestamp.
69 (sleep-for 1)
70 (write-region "another text" nil tmpfile nil 'no-message)
71
72 ;; Check, that the buffer has been reverted.
73 (auto-revert--wait-for-revert buf)
74 (should (string-match "another text" (buffer-string)))
75
76 ;; When the buffer is modified, it shall not be reverted.
77 (with-current-buffer (get-buffer-create "*Messages*")
78 (narrow-to-region (point-max) (point-max)))
79 (set-buffer-modified-p t)
80 (sleep-for 1)
81 (write-region "any text" nil tmpfile nil 'no-message)
82
83 ;; Check, that the buffer hasn't been reverted.
84 (auto-revert--wait-for-revert buf)
85 (should-not (string-match "any text" (buffer-string)))))
86
87 ;; Exit.
88 (with-current-buffer "*Messages*" (widen))
89 (ignore-errors
90 (with-current-buffer buf (set-buffer-modified-p nil))
91 (kill-buffer buf))
92 (ignore-errors (delete-file tmpfile)))))
93
94 ;; This is inspired by Bug#21841.
95 (ert-deftest auto-revert-test01-auto-revert-several-files ()
96 "Check autorevert for several files at once."
97 :tags '(:expensive-test)
98 (skip-unless (executable-find "cp"))
99
100 (let* ((cp (executable-find "cp"))
101 (tmpdir1 (make-temp-file "auto-revert-test" 'dir))
102 (tmpdir2 (make-temp-file "auto-revert-test" 'dir))
103 (tmpfile1
104 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
105 (tmpfile2
106 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
107 buf1 buf2)
108 (unwind-protect
109 (progn
110 (with-current-buffer (get-buffer-create "*Messages*")
111 (narrow-to-region (point-max) (point-max)))
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))
116
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)))
126
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))
143
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))))))
149
150 ;; Exit.
151 (with-current-buffer "*Messages*" (widen))
152 (ignore-errors
153 (dolist (buf (list buf1 buf2))
154 (with-current-buffer buf (set-buffer-modified-p nil))
155 (kill-buffer buf)))
156 (ignore-errors (delete-directory tmpdir1 'recursive))
157 (ignore-errors (delete-directory tmpdir2 'recursive)))))
158
159 ;; This is inspired by Bug#23276.
160 (ert-deftest auto-revert-test02-auto-revert-deleted-file ()
161 "Check autorevert for a deleted file."
162 :tags '(:expensive-test)
163
164 (let ((tmpfile (make-temp-file "auto-revert-test"))
165 buf)
166 (unwind-protect
167 (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)
171 (setq buf (find-file-noselect tmpfile))
172 (with-current-buffer buf
173 (should (string-equal (buffer-string) "any text"))
174 ;; `buffer-stale--default-function' checks for
175 ;; `verify-visited-file-modtime'. We must ensure that
176 ;; it returns nil.
177 (sleep-for 1)
178 (auto-revert-mode 1)
179 (should auto-revert-mode)
180
181 ;; Remove file while reverting. We simulate this by
182 ;; modifying `before-revert-hook'.
183 (add-hook
184 'before-revert-hook
185 (lambda () (delete-file buffer-file-name))
186 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
192 ;; Check, that the buffer hasn't been reverted. File
193 ;; notification should be disabled, falling back to
194 ;; polling.
195 (auto-revert--wait-for-revert buf)
196 (should (string-match "any text" (buffer-string)))
197 (should-not auto-revert-use-notify)
198
199 ;; Once the file has been recreated, the buffer shall be
200 ;; reverted.
201 (kill-local-variable 'before-revert-hook)
202 (with-current-buffer (get-buffer-create "*Messages*")
203 (narrow-to-region (point-max) (point-max)))
204 (sleep-for 1)
205 (write-region "another text" nil tmpfile nil 'no-message)
206
207 ;; Check, that the buffer has been reverted.
208 (auto-revert--wait-for-revert buf)
209 (should (string-match "another text" (buffer-string)))
210
211 ;; An empty file shall still be reverted.
212 (with-current-buffer (get-buffer-create "*Messages*")
213 (narrow-to-region (point-max) (point-max)))
214 (sleep-for 1)
215 (write-region "" nil tmpfile nil 'no-message)
216
217 ;; Check, that the buffer has been reverted.
218 (auto-revert--wait-for-revert buf)
219 (should (string-equal "" (buffer-string)))))
220
221 ;; Exit.
222 (with-current-buffer "*Messages*" (widen))
223 (ignore-errors
224 (with-current-buffer buf (set-buffer-modified-p nil))
225 (kill-buffer buf))
226 (ignore-errors (delete-file tmpfile)))))
227
228 (ert-deftest auto-revert-test03-auto-revert-tail-mode ()
229 "Check autorevert tail mode."
230 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
231 ;; file has been reverted.
232 (let ((tmpfile (make-temp-file "auto-revert-test"))
233 buf)
234 (unwind-protect
235 (progn
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)
239 (setq buf (find-file-noselect tmpfile))
240 (with-current-buffer buf
241 ;; `buffer-stale--default-function' checks for
242 ;; `verify-visited-file-modtime'. We must ensure that it
243 ;; returns nil.
244 (sleep-for 1)
245 (auto-revert-tail-mode 1)
246 (should auto-revert-tail-mode)
247 (erase-buffer)
248 (insert "modified text\n")
249 (set-buffer-modified-p nil)
250
251 ;; Modify file. We wait for a second, in order to have
252 ;; another timestamp.
253 (sleep-for 1)
254 (write-region "another text" nil tmpfile 'append 'no-message)
255
256 ;; Check, that the buffer has been reverted.
257 (auto-revert--wait-for-revert buf)
258 (should
259 (string-match "modified text\nanother text" (buffer-string)))))
260
261 ;; Exit.
262 (with-current-buffer "*Messages*" (widen))
263 (ignore-errors (kill-buffer buf))
264 (ignore-errors (delete-file tmpfile)))))
265
266 (ert-deftest auto-revert-test04-auto-revert-mode-dired ()
267 "Check autorevert for dired."
268 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
269 ;; file has been reverted.
270 (let* ((tmpfile (make-temp-file "auto-revert-test"))
271 (name (file-name-nondirectory tmpfile))
272 buf)
273 (unwind-protect
274 (progn
275 (setq buf (dired-noselect temporary-file-directory))
276 (with-current-buffer buf
277 ;; `buffer-stale--default-function' checks for
278 ;; `verify-visited-file-modtime'. We must ensure that it
279 ;; returns nil.
280 (sleep-for 1)
281 (auto-revert-mode 1)
282 (should auto-revert-mode)
283 (should
284 (string-match name (substring-no-properties (buffer-string))))
285
286 ;; Delete file. We wait for a second, in order to have
287 ;; another timestamp.
288 (with-current-buffer (get-buffer-create "*Messages*")
289 (narrow-to-region (point-max) (point-max)))
290 (sleep-for 1)
291 (delete-file tmpfile)
292
293 ;; Check, that the buffer has been reverted.
294 (auto-revert--wait-for-revert buf)
295 (should-not
296 (string-match name (substring-no-properties (buffer-string))))
297
298 ;; Make dired buffer modified. Check, that the buffer has
299 ;; been still reverted.
300 (with-current-buffer (get-buffer-create "*Messages*")
301 (narrow-to-region (point-max) (point-max)))
302 (set-buffer-modified-p t)
303 (sleep-for 1)
304 (write-region "any text" nil tmpfile nil 'no-message)
305
306 ;; Check, that the buffer has been reverted.
307 (auto-revert--wait-for-revert buf)
308 (should
309 (string-match name (substring-no-properties (buffer-string))))))
310
311 ;; Exit.
312 (with-current-buffer "*Messages*" (widen))
313 (ignore-errors
314 (with-current-buffer buf (set-buffer-modified-p nil))
315 (kill-buffer buf))
316 (ignore-errors (delete-file tmpfile)))))
317
318 (defun auto-revert-test-all (&optional interactive)
319 "Run all tests for \\[auto-revert]."
320 (interactive "p")
321 (if interactive
322 (ert-run-tests-interactively "^auto-revert-")
323 (ert-run-tests-batch "^auto-revert-")))
324
325 (provide 'auto-revert-tests)
326 ;;; auto-revert-tests.el ends here