1 ;;; auto-revert-tests.el --- Tests of auto-revert
3 ;; Copyright (C) 2015-2016 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'.
28 (setq auto-revert-notify-exclude-dir-regexp
"nothing-to-be-excluded"
29 auto-revert-stop-on-user-input nil
)
31 (defconst auto-revert--timeout
10
32 "Time to wait until a message appears in the *Messages* buffer.")
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*"
40 (format-message "Reverting buffer `%s'." (buffer-name buffer
))
42 (if (with-current-buffer buffer auto-revert-use-notify
)
43 (read-event nil nil
0.1)
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"))
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
65 (should auto-revert-mode
)
67 ;; Modify file. We wait for a second, in order to have
70 (write-region "another text" nil tmpfile nil
'no-message
)
72 ;; Check, that the buffer has been reverted.
73 (auto-revert--wait-for-revert buf
)
74 (should (string-match "another text" (buffer-string)))
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
)
81 (write-region "any text" nil tmpfile nil
'no-message
)
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)))))
88 (with-current-buffer "*Messages*" (widen))
90 (with-current-buffer buf
(set-buffer-modified-p nil
))
92 (ignore-errors (delete-file tmpfile
)))))
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"))
100 (let* ((cp (executable-find "cp"))
101 (tmpdir1 (make-temp-file "auto-revert-test" 'dir
))
102 (tmpdir2 (make-temp-file "auto-revert-test" 'dir
))
104 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1
)))
106 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1
)))
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
))
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))))))
151 (with-current-buffer "*Messages*" (widen))
153 (dolist (buf (list buf1 buf2
))
154 (with-current-buffer buf
(set-buffer-modified-p nil
))
156 (ignore-errors (delete-directory tmpdir1
'recursive
))
157 (ignore-errors (delete-directory tmpdir2
'recursive
)))))
159 (ert-deftest auto-revert-test02-auto-revert-tail-mode
()
160 "Check autorevert tail mode."
161 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
162 ;; file has been reverted.
163 (let ((tmpfile (make-temp-file "auto-revert-test"))
167 (with-current-buffer (get-buffer-create "*Messages*")
168 (narrow-to-region (point-max) (point-max)))
169 (write-region "any text" nil tmpfile nil
'no-message
)
170 (setq buf
(find-file-noselect tmpfile
))
171 (with-current-buffer buf
172 ;; `buffer-stale--default-function' checks for
173 ;; `verify-visited-file-modtime'. We must ensure that it
176 (auto-revert-tail-mode 1)
177 (should auto-revert-tail-mode
)
179 (insert "modified text\n")
180 (set-buffer-modified-p nil
)
182 ;; Modify file. We wait for a second, in order to have
183 ;; another timestamp.
185 (write-region "another text" nil tmpfile
'append
'no-message
)
187 ;; Check, that the buffer has been reverted.
188 (auto-revert--wait-for-revert buf
)
190 (string-match "modified text\nanother text" (buffer-string)))))
193 (with-current-buffer "*Messages*" (widen))
194 (ignore-errors (kill-buffer buf
))
195 (ignore-errors (delete-file tmpfile
)))))
197 (ert-deftest auto-revert-test03-auto-revert-mode-dired
()
198 "Check autorevert for dired."
199 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
200 ;; file has been reverted.
201 (let* ((tmpfile (make-temp-file "auto-revert-test"))
202 (name (file-name-nondirectory tmpfile
))
206 (setq buf
(dired-noselect temporary-file-directory
))
207 (with-current-buffer buf
208 ;; `buffer-stale--default-function' checks for
209 ;; `verify-visited-file-modtime'. We must ensure that it
213 (should auto-revert-mode
)
215 (string-match name
(substring-no-properties (buffer-string))))
217 ;; Delete file. We wait for a second, in order to have
218 ;; another timestamp.
219 (with-current-buffer (get-buffer-create "*Messages*")
220 (narrow-to-region (point-max) (point-max)))
222 (delete-file tmpfile
)
224 ;; Check, that the buffer has been reverted.
225 (auto-revert--wait-for-revert buf
)
227 (string-match name
(substring-no-properties (buffer-string))))
229 ;; Make dired buffer modified. Check, that the buffer has
230 ;; been still reverted.
231 (with-current-buffer (get-buffer-create "*Messages*")
232 (narrow-to-region (point-max) (point-max)))
233 (set-buffer-modified-p t
)
235 (write-region "any text" nil tmpfile nil
'no-message
)
237 ;; Check, that the buffer has been reverted.
238 (auto-revert--wait-for-revert buf
)
240 (string-match name
(substring-no-properties (buffer-string))))))
243 (with-current-buffer "*Messages*" (widen))
245 (with-current-buffer buf
(set-buffer-modified-p nil
))
247 (ignore-errors (delete-file tmpfile
)))))
249 (defun auto-revert-test-all (&optional interactive
)
250 "Run all tests for \\[auto-revert]."
253 (ert-run-tests-interactively "^auto-revert-")
254 (ert-run-tests-batch "^auto-revert-")))
256 (provide 'auto-revert-tests
)
257 ;;; auto-revert-tests.el ends here