1 ;;; auto-revert-tests.el --- Tests of auto-revert
3 ;; Copyright (C) 2015 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 (defvar auto-revert--timeout
10)
33 (ert-deftest auto-revert-test00-auto-revert-mode
()
34 "Check autorevert for a file."
35 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
36 ;; file has been reverted.
37 (let ((tmpfile (make-temp-file "auto-revert-test"))
41 (with-current-buffer (get-buffer-create "*Messages*")
42 (narrow-to-region (point-max) (point-max)))
43 (write-region "any text" nil tmpfile nil
'no-message
)
44 (setq buf
(find-file-noselect tmpfile
))
45 (with-current-buffer buf
46 (should (string-equal (buffer-string) "any text"))
47 ;; `buffer-stale--default-function' checks for
48 ;; `verify-visited-file-modtime'. We must ensure that it
52 (should auto-revert-mode
)
54 ;; Modify file. We wait for a second, in order to have
57 (write-region "another text" nil tmpfile nil
'no-message
)
59 ;; Check, that the buffer has been reverted.
60 (with-timeout (auto-revert--timeout nil
)
61 (with-current-buffer "*Messages*"
64 (format "Reverting buffer `%s'." (buffer-name buf
))
66 (read-event nil nil
0.1))))
67 (should (string-match "another text" (buffer-string)))
69 ;; When the buffer is modified, it shall not be reverted.
70 (with-current-buffer (get-buffer-create "*Messages*")
71 (narrow-to-region (point-max) (point-max)))
72 (set-buffer-modified-p t
)
74 (write-region "any text" nil tmpfile nil
'no-message
)
76 ;; Check, whether the buffer has been reverted.
77 (with-timeout (auto-revert--timeout nil
)
78 (with-current-buffer "*Messages*"
81 (format "Reverting buffer `%s'." (buffer-name buf
))
83 (read-event nil nil
0.1))))
84 (should-not (string-match "any text" (buffer-string)))))
87 (with-current-buffer "*Messages*" (widen))
89 (with-current-buffer buf
(set-buffer-modified-p nil
))
91 (ignore-errors (delete-file tmpfile
)))))
93 (ert-deftest auto-revert-test01-auto-revert-tail-mode
()
94 "Check autorevert tail mode."
95 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
96 ;; file has been reverted.
97 (let ((tmpfile (make-temp-file "auto-revert-test"))
101 (with-current-buffer (get-buffer-create "*Messages*")
102 (narrow-to-region (point-max) (point-max)))
103 (write-region "any text" nil tmpfile nil
'no-message
)
104 (setq buf
(find-file-noselect tmpfile
))
105 (with-current-buffer buf
106 ;; `buffer-stale--default-function' checks for
107 ;; `verify-visited-file-modtime'. We must ensure that it
110 (auto-revert-tail-mode 1)
111 (should auto-revert-tail-mode
)
113 (insert "modified text\n")
114 (set-buffer-modified-p nil
)
116 ;; Modify file. We wait for a second, in order to have
117 ;; another timestamp.
119 (write-region "another text" nil tmpfile
'append
'no-message
)
121 ;; Check, that the buffer has been reverted.
122 (with-timeout (auto-revert--timeout nil
)
123 (with-current-buffer "*Messages*"
126 (format "Reverting buffer `%s'." (buffer-name buf
))
128 (read-event nil nil
0.1))))
130 (string-match "modified text\nanother text" (buffer-string)))))
133 (with-current-buffer "*Messages*" (widen))
134 (ignore-errors (kill-buffer buf
))
135 (ignore-errors (delete-file tmpfile
)))))
137 (ert-deftest auto-revert-test02-auto-revert-mode-dired
()
138 "Check autorevert for dired."
139 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
140 ;; file has been reverted.
141 (let* ((tmpfile (make-temp-file "auto-revert-test"))
142 (name (file-name-nondirectory tmpfile
))
146 (with-current-buffer (get-buffer-create "*Messages*")
147 (narrow-to-region (point-max) (point-max)))
148 (setq buf
(dired-noselect temporary-file-directory
))
149 (with-current-buffer buf
150 ;; `buffer-stale--default-function' checks for
151 ;; `verify-visited-file-modtime'. We must ensure that it
155 (should auto-revert-mode
)
157 (string-match name
(substring-no-properties (buffer-string))))
159 ;; Delete file. We wait for a second, in order to have
160 ;; another timestamp.
162 (delete-file tmpfile
)
164 ;; Check, that the buffer has been reverted.
165 (with-timeout (auto-revert--timeout nil
)
166 (with-current-buffer "*Messages*"
169 (format "Reverting buffer `%s'." (buffer-name buf
))
171 (read-event nil nil
0.1))))
174 (string-match name
(substring-no-properties (buffer-string)))))
176 ;; Make dired buffer modified. Check, that the buffer has
177 ;; been still reverted.
178 (with-current-buffer (get-buffer-create "*Messages*")
179 (narrow-to-region (point-max) (point-max)))
180 (set-buffer-modified-p t
)
182 (write-region "any text" nil tmpfile nil
'no-message
)
184 ;; Check, that the buffer hasn't been reverted.
185 (with-timeout (auto-revert--timeout nil
)
186 (with-current-buffer "*Messages*"
189 (format "Reverting buffer `%s'." (buffer-name buf
))
191 (read-event nil nil
0.1))))
193 (string-match name
(substring-no-properties (buffer-string))))))
196 (with-current-buffer "*Messages*" (widen))
198 (with-current-buffer buf
(set-buffer-modified-p nil
))
200 (ignore-errors (delete-file tmpfile
)))))
202 (defun auto-revert-test-all (&optional interactive
)
203 "Run all tests for \\[auto-revert]."
206 (ert-run-tests-interactively "^auto-revert-")
207 (ert-run-tests-batch "^auto-revert-")))
209 (provide 'auto-revert-tests
)
210 ;;; auto-revert-tests.el ends here