Give '$' punctuation syntax in make-mode (Bug#24477)
[emacs.git] / test / lisp / autorevert-tests.el
blob05d24b51ee79b199a283ce2c5eee8c415240cc28
1 ;;; auto-revert-tests.el --- Tests of auto-revert
3 ;; Copyright (C) 2015-2018 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 `https://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 desc)
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)
177 (setq desc auto-revert-notify-watch-descriptor)
179 ;; Remove file while reverting. We simulate this by
180 ;; modifying `before-revert-hook'.
181 (add-hook
182 'before-revert-hook
183 (lambda () (delete-file buffer-file-name))
184 nil t)
186 (ert-with-message-capture auto-revert--messages
187 (sleep-for 1)
188 (write-region "another text" nil tmpfile nil 'no-message)
189 (auto-revert--wait-for-revert buf))
190 ;; Check, that the buffer hasn't been reverted. File
191 ;; notification should be disabled, falling back to
192 ;; polling.
193 (should (string-match "any text" (buffer-string)))
194 ;; With w32notify, the 'stopped' events are not sent.
195 (or (eq file-notify--library 'w32notify)
196 (should-not auto-revert-notify-watch-descriptor))
198 ;; Once the file has been recreated, the buffer shall be
199 ;; reverted.
200 (kill-local-variable 'before-revert-hook)
201 (ert-with-message-capture auto-revert--messages
202 (sleep-for 1)
203 (write-region "another text" nil tmpfile nil 'no-message)
204 (auto-revert--wait-for-revert buf))
205 ;; Check, that the buffer has been reverted.
206 (should (string-match "another text" (buffer-string)))
207 ;; When file notification is used, it must be reenabled
208 ;; after recreation of the file. We cannot expect that
209 ;; the descriptor is the same, so we just check the
210 ;; existence.
211 (should (eq (null desc) (null auto-revert-notify-watch-descriptor)))
213 ;; An empty file shall still be reverted.
214 (ert-with-message-capture auto-revert--messages
215 (sleep-for 1)
216 (write-region "" nil tmpfile nil 'no-message)
217 (auto-revert--wait-for-revert buf))
218 ;; Check, that the buffer has been reverted.
219 (should (string-equal "" (buffer-string)))))
221 ;; Exit.
222 (ignore-errors
223 (with-current-buffer buf (set-buffer-modified-p nil))
224 (kill-buffer buf))
225 (ignore-errors (delete-file tmpfile)))))
227 (ert-deftest auto-revert-test03-auto-revert-tail-mode ()
228 "Check autorevert tail mode."
229 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
230 ;; file has been reverted.
231 (let ((tmpfile (make-temp-file "auto-revert-test"))
232 buf)
233 (unwind-protect
234 (ert-with-message-capture auto-revert--messages
235 (write-region "any text" nil tmpfile nil 'no-message)
236 (setq buf (find-file-noselect tmpfile))
237 (with-current-buffer buf
238 ;; `buffer-stale--default-function' checks for
239 ;; `verify-visited-file-modtime'. We must ensure that it
240 ;; returns nil.
241 (sleep-for 1)
242 (auto-revert-tail-mode 1)
243 (should auto-revert-tail-mode)
244 (erase-buffer)
245 (insert "modified text\n")
246 (set-buffer-modified-p nil)
248 ;; Modify file. We wait for a second, in order to have
249 ;; another timestamp.
250 (sleep-for 1)
251 (write-region "another text" nil tmpfile 'append 'no-message)
253 ;; Check, that the buffer has been reverted.
254 (auto-revert--wait-for-revert buf)
255 (should
256 (string-match "modified text\nanother text" (buffer-string)))))
258 ;; Exit.
259 (ignore-errors (kill-buffer buf))
260 (ignore-errors (delete-file tmpfile)))))
262 (ert-deftest auto-revert-test04-auto-revert-mode-dired ()
263 "Check autorevert for dired."
264 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
265 ;; file has been reverted.
266 (let* ((tmpfile (make-temp-file "auto-revert-test"))
267 (name (file-name-nondirectory tmpfile))
268 buf)
269 (unwind-protect
270 (progn
271 (setq buf (dired-noselect temporary-file-directory))
272 (with-current-buffer buf
273 ;; `buffer-stale--default-function' checks for
274 ;; `verify-visited-file-modtime'. We must ensure that it
275 ;; returns nil.
276 (sleep-for 1)
277 (auto-revert-mode 1)
278 (should auto-revert-mode)
279 (should
280 (string-match name (substring-no-properties (buffer-string))))
282 (ert-with-message-capture auto-revert--messages
283 ;; Delete file. We wait for a second, in order to have
284 ;; another timestamp.
285 (sleep-for 1)
286 (delete-file tmpfile)
287 (auto-revert--wait-for-revert buf))
288 ;; Check, that the buffer has been reverted.
289 (should-not
290 (string-match name (substring-no-properties (buffer-string))))
292 (ert-with-message-capture auto-revert--messages
293 ;; Make dired buffer modified. Check, that the buffer has
294 ;; been still reverted.
295 (set-buffer-modified-p t)
296 (sleep-for 1)
297 (write-region "any text" nil tmpfile nil 'no-message)
299 (auto-revert--wait-for-revert buf))
300 ;; Check, that the buffer has been reverted.
301 (should
302 (string-match name (substring-no-properties (buffer-string))))))
304 ;; Exit.
305 (ignore-errors
306 (with-current-buffer buf (set-buffer-modified-p nil))
307 (kill-buffer buf))
308 (ignore-errors (delete-file tmpfile)))))
310 (defun auto-revert-test-all (&optional interactive)
311 "Run all tests for \\[auto-revert]."
312 (interactive "p")
313 (if interactive
314 (ert-run-tests-interactively "^auto-revert-")
315 (ert-run-tests-batch "^auto-revert-")))
317 (provide 'auto-revert-tests)
318 ;;; auto-revert-tests.el ends here