]> code.delx.au - gnu-emacs/blob - test/lisp/autorevert-tests.el
Further Unicode restrictive fixups
[gnu-emacs] / test / lisp / autorevert-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 (read-event nil nil 0.1)))))
43
44 (ert-deftest auto-revert-test00-auto-revert-mode ()
45 "Check autorevert for a file."
46 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
47 ;; file has been reverted.
48 (let ((tmpfile (make-temp-file "auto-revert-test"))
49 buf)
50 (unwind-protect
51 (progn
52 (with-current-buffer (get-buffer-create "*Messages*")
53 (narrow-to-region (point-max) (point-max)))
54 (write-region "any text" nil tmpfile nil 'no-message)
55 (setq buf (find-file-noselect tmpfile))
56 (with-current-buffer buf
57 (should (string-equal (buffer-string) "any text"))
58 ;; `buffer-stale--default-function' checks for
59 ;; `verify-visited-file-modtime'. We must ensure that it
60 ;; returns nil.
61 (sleep-for 1)
62 (auto-revert-mode 1)
63 (should auto-revert-mode)
64
65 ;; Modify file. We wait for a second, in order to have
66 ;; another timestamp.
67 (sleep-for 1)
68 (write-region "another text" nil tmpfile nil 'no-message)
69
70 ;; Check, that the buffer has been reverted.
71 (auto-revert--wait-for-revert buf)
72 (should (string-match "another text" (buffer-string)))
73
74 ;; When the buffer is modified, it shall not be reverted.
75 (with-current-buffer (get-buffer-create "*Messages*")
76 (narrow-to-region (point-max) (point-max)))
77 (set-buffer-modified-p t)
78 (sleep-for 1)
79 (write-region "any text" nil tmpfile nil 'no-message)
80
81 ;; Check, that the buffer hasn't been reverted.
82 (auto-revert--wait-for-revert buf)
83 (should-not (string-match "any text" (buffer-string)))))
84
85 ;; Exit.
86 (with-current-buffer "*Messages*" (widen))
87 (ignore-errors
88 (with-current-buffer buf (set-buffer-modified-p nil))
89 (kill-buffer buf))
90 (ignore-errors (delete-file tmpfile)))))
91
92 ;; This is inspired by Bug#21841.
93 (ert-deftest auto-revert-test01-auto-revert-several-files ()
94 "Check autorevert for several files at once."
95 (skip-unless (executable-find "cp"))
96
97 (let* ((cp (executable-find "cp"))
98 (tmpdir1 (make-temp-file "auto-revert-test" 'dir))
99 (tmpdir2 (make-temp-file "auto-revert-test" 'dir))
100 (tmpfile1
101 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
102 (tmpfile2
103 (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
104 buf1 buf2)
105 (unwind-protect
106 (progn
107 (with-current-buffer (get-buffer-create "*Messages*")
108 (narrow-to-region (point-max) (point-max)))
109 (write-region "any text" nil tmpfile1 nil 'no-message)
110 (setq buf1 (find-file-noselect tmpfile1))
111 (write-region "any text" nil tmpfile2 nil 'no-message)
112 (setq buf2 (find-file-noselect tmpfile2))
113
114 (dolist (buf (list buf1 buf2))
115 (with-current-buffer buf
116 (should (string-equal (buffer-string) "any text"))
117 ;; `buffer-stale--default-function' checks for
118 ;; `verify-visited-file-modtime'. We must ensure that
119 ;; it returns nil.
120 (sleep-for 1)
121 (auto-revert-mode 1)
122 (should auto-revert-mode)))
123
124 ;; Modify files. We wait for a second, in order to have
125 ;; another timestamp.
126 (sleep-for 1)
127 (write-region
128 "another text" nil
129 (expand-file-name (file-name-nondirectory tmpfile1) tmpdir2)
130 nil 'no-message)
131 (write-region
132 "another text" nil
133 (expand-file-name (file-name-nondirectory tmpfile2) tmpdir2)
134 nil 'no-message)
135 ;;(copy-directory tmpdir2 tmpdir1 nil 'copy-contents)
136 ;; Strange, that `copy-directory' does not work as expected.
137 ;; The following shell command is not portable on all
138 ;; platforms, unfortunately.
139 (shell-command (format "%s %s/* %s" cp tmpdir2 tmpdir1))
140
141 ;; Check, that the buffers have been reverted.
142 (dolist (buf (list buf1 buf2))
143 (with-current-buffer buf
144 (auto-revert--wait-for-revert buf)
145 (should (string-match "another text" (buffer-string))))))
146
147 ;; Exit.
148 (with-current-buffer "*Messages*" (widen))
149 (ignore-errors
150 (dolist (buf (list buf1 buf2))
151 (with-current-buffer buf (set-buffer-modified-p nil))
152 (kill-buffer buf)))
153 (ignore-errors (delete-directory tmpdir1 'recursive))
154 (ignore-errors (delete-directory tmpdir2 'recursive)))))
155
156 (ert-deftest auto-revert-test02-auto-revert-tail-mode ()
157 "Check autorevert tail mode."
158 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
159 ;; file has been reverted.
160 (let ((tmpfile (make-temp-file "auto-revert-test"))
161 buf)
162 (unwind-protect
163 (progn
164 (with-current-buffer (get-buffer-create "*Messages*")
165 (narrow-to-region (point-max) (point-max)))
166 (write-region "any text" nil tmpfile nil 'no-message)
167 (setq buf (find-file-noselect tmpfile))
168 (with-current-buffer buf
169 ;; `buffer-stale--default-function' checks for
170 ;; `verify-visited-file-modtime'. We must ensure that it
171 ;; returns nil.
172 (sleep-for 1)
173 (auto-revert-tail-mode 1)
174 (should auto-revert-tail-mode)
175 (erase-buffer)
176 (insert "modified text\n")
177 (set-buffer-modified-p nil)
178
179 ;; Modify file. We wait for a second, in order to have
180 ;; another timestamp.
181 (sleep-for 1)
182 (write-region "another text" nil tmpfile 'append 'no-message)
183
184 ;; Check, that the buffer has been reverted.
185 (auto-revert--wait-for-revert buf)
186 (should
187 (string-match "modified text\nanother text" (buffer-string)))))
188
189 ;; Exit.
190 (with-current-buffer "*Messages*" (widen))
191 (ignore-errors (kill-buffer buf))
192 (ignore-errors (delete-file tmpfile)))))
193
194 (ert-deftest auto-revert-test03-auto-revert-mode-dired ()
195 "Check autorevert for dired."
196 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
197 ;; file has been reverted.
198 (let* ((tmpfile (make-temp-file "auto-revert-test"))
199 (name (file-name-nondirectory tmpfile))
200 buf)
201 (unwind-protect
202 (progn
203 (setq buf (dired-noselect temporary-file-directory))
204 (with-current-buffer buf
205 ;; `buffer-stale--default-function' checks for
206 ;; `verify-visited-file-modtime'. We must ensure that it
207 ;; returns nil.
208 (sleep-for 1)
209 (auto-revert-mode 1)
210 (should auto-revert-mode)
211 (should
212 (string-match name (substring-no-properties (buffer-string))))
213
214 ;; Delete file. We wait for a second, in order to have
215 ;; another timestamp.
216 (with-current-buffer (get-buffer-create "*Messages*")
217 (narrow-to-region (point-max) (point-max)))
218 (sleep-for 1)
219 (delete-file tmpfile)
220
221 ;; Check, that the buffer has been reverted.
222 (auto-revert--wait-for-revert buf)
223 (should-not
224 (string-match name (substring-no-properties (buffer-string))))
225
226 ;; Make dired buffer modified. Check, that the buffer has
227 ;; been still reverted.
228 (with-current-buffer (get-buffer-create "*Messages*")
229 (narrow-to-region (point-max) (point-max)))
230 (set-buffer-modified-p t)
231 (sleep-for 1)
232 (write-region "any text" nil tmpfile nil 'no-message)
233
234 ;; Check, that the buffer has been reverted.
235 (auto-revert--wait-for-revert buf)
236 (should
237 (string-match name (substring-no-properties (buffer-string))))))
238
239 ;; Exit.
240 (with-current-buffer "*Messages*" (widen))
241 (ignore-errors
242 (with-current-buffer buf (set-buffer-modified-p nil))
243 (kill-buffer buf))
244 (ignore-errors (delete-file tmpfile)))))
245
246 (defun auto-revert-test-all (&optional interactive)
247 "Run all tests for \\[auto-revert]."
248 (interactive "p")
249 (if interactive
250 (ert-run-tests-interactively "^auto-revert-")
251 (ert-run-tests-batch "^auto-revert-")))
252
253 (provide 'auto-revert-tests)
254 ;;; auto-revert-tests.el ends here