]> code.delx.au - gnu-emacs/blob - test/automated/auto-revert-tests.el
e03ed8cb68777cb2a24c3c364d134605df1581ec
[gnu-emacs] / test / automated / auto-revert-tests.el
1 ;;; auto-revert-tests.el --- Tests of auto-revert
2
3 ;; Copyright (C) 2015 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 (skip-unless (executable-find "cp"))
98
99 (let* ((cp (executable-find "cp"))
100 (tmpdir1 (make-temp-file "auto-revert-test" 'dir))
101 (tmpdir2 (make-temp-file "auto-revert-test" 'dir))
102 (tmpfile1
103 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
104 (tmpfile2
105 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
106 buf1 buf2)
107 (unwind-protect
108 (progn
109 (with-current-buffer (get-buffer-create "*Messages*")
110 (narrow-to-region (point-max) (point-max)))
111 (write-region "any text" nil tmpfile1 nil 'no-message)
112 (setq buf1 (find-file-noselect tmpfile1))
113 (write-region "any text" nil tmpfile2 nil 'no-message)
114 (setq buf2 (find-file-noselect tmpfile2))
115
116 (dolist (buf (list buf1 buf2))
117 (with-current-buffer buf
118 (should (string-equal (buffer-string) "any text"))
119 ;; `buffer-stale--default-function' checks for
120 ;; `verify-visited-file-modtime'. We must ensure that
121 ;; it returns nil.
122 (sleep-for 1)
123 (auto-revert-mode 1)
124 (should auto-revert-mode)))
125
126 ;; Modify files. We wait for a second, in order to have
127 ;; another timestamp.
128 (sleep-for 1)
129 (write-region
130 "another text" nil
131 (expand-file-name (file-name-nondirectory tmpfile1) tmpdir2)
132 nil 'no-message)
133 (write-region
134 "another text" nil
135 (expand-file-name (file-name-nondirectory tmpfile2) tmpdir2)
136 nil 'no-message)
137 ;;(copy-directory tmpdir2 tmpdir1 nil 'copy-contents)
138 ;; Strange, that `copy-directory' does not work as expected.
139 ;; The following shell command is not portable on all
140 ;; platforms, unfortunately.
141 (shell-command (format "%s -f %s/* %s" cp tmpdir2 tmpdir1))
142
143 ;; Check, that the buffers have been reverted.
144 (dolist (buf (list buf1 buf2))
145 (with-current-buffer buf
146 (auto-revert--wait-for-revert buf)
147 (should (string-match "another text" (buffer-string))))))
148
149 ;; Exit.
150 (with-current-buffer "*Messages*" (widen))
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)))))
157
158 (ert-deftest auto-revert-test02-auto-revert-tail-mode ()
159 "Check autorevert tail mode."
160 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
161 ;; file has been reverted.
162 (let ((tmpfile (make-temp-file "auto-revert-test"))
163 buf)
164 (unwind-protect
165 (progn
166 (with-current-buffer (get-buffer-create "*Messages*")
167 (narrow-to-region (point-max) (point-max)))
168 (write-region "any text" nil tmpfile nil 'no-message)
169 (setq buf (find-file-noselect tmpfile))
170 (with-current-buffer buf
171 ;; `buffer-stale--default-function' checks for
172 ;; `verify-visited-file-modtime'. We must ensure that it
173 ;; returns nil.
174 (sleep-for 1)
175 (auto-revert-tail-mode 1)
176 (should auto-revert-tail-mode)
177 (erase-buffer)
178 (insert "modified text\n")
179 (set-buffer-modified-p nil)
180
181 ;; Modify file. We wait for a second, in order to have
182 ;; another timestamp.
183 (sleep-for 1)
184 (write-region "another text" nil tmpfile 'append 'no-message)
185
186 ;; Check, that the buffer has been reverted.
187 (auto-revert--wait-for-revert buf)
188 (should
189 (string-match "modified text\nanother text" (buffer-string)))))
190
191 ;; Exit.
192 (with-current-buffer "*Messages*" (widen))
193 (ignore-errors (kill-buffer buf))
194 (ignore-errors (delete-file tmpfile)))))
195
196 (ert-deftest auto-revert-test03-auto-revert-mode-dired ()
197 "Check autorevert for dired."
198 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
199 ;; file has been reverted.
200 (let* ((tmpfile (make-temp-file "auto-revert-test"))
201 (name (file-name-nondirectory tmpfile))
202 buf)
203 (unwind-protect
204 (progn
205 (setq buf (dired-noselect temporary-file-directory))
206 (with-current-buffer buf
207 ;; `buffer-stale--default-function' checks for
208 ;; `verify-visited-file-modtime'. We must ensure that it
209 ;; returns nil.
210 (sleep-for 1)
211 (auto-revert-mode 1)
212 (should auto-revert-mode)
213 (should
214 (string-match name (substring-no-properties (buffer-string))))
215
216 ;; Delete file. We wait for a second, in order to have
217 ;; another timestamp.
218 (with-current-buffer (get-buffer-create "*Messages*")
219 (narrow-to-region (point-max) (point-max)))
220 (sleep-for 1)
221 (delete-file tmpfile)
222
223 ;; Check, that the buffer has been reverted.
224 (auto-revert--wait-for-revert buf)
225 (should-not
226 (string-match name (substring-no-properties (buffer-string))))
227
228 ;; Make dired buffer modified. Check, that the buffer has
229 ;; been still reverted.
230 (with-current-buffer (get-buffer-create "*Messages*")
231 (narrow-to-region (point-max) (point-max)))
232 (set-buffer-modified-p t)
233 (sleep-for 1)
234 (write-region "any text" nil tmpfile nil 'no-message)
235
236 ;; Check, that the buffer has been reverted.
237 (auto-revert--wait-for-revert buf)
238 (should
239 (string-match name (substring-no-properties (buffer-string))))))
240
241 ;; Exit.
242 (with-current-buffer "*Messages*" (widen))
243 (ignore-errors
244 (with-current-buffer buf (set-buffer-modified-p nil))
245 (kill-buffer buf))
246 (ignore-errors (delete-file tmpfile)))))
247
248 (defun auto-revert-test-all (&optional interactive)
249 "Run all tests for \\[auto-revert]."
250 (interactive "p")
251 (if interactive
252 (ert-run-tests-interactively "^auto-revert-")
253 (ert-run-tests-batch "^auto-revert-")))
254
255 (provide 'auto-revert-tests)
256 ;;; auto-revert-tests.el ends here