In ‘adjust-window-trailing-edge’ fix bug with size-preserved windows.
[emacs.git] / test / automated / auto-revert-tests.el
blob204e03d423df95e4d54216bcc19828a43b0479e5
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/'.
20 ;;; Commentary:
22 ;; A whole test run can be performed calling the command `auto-revert-test-all'.
24 ;;; Code:
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)
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"))
38 buf)
39 (unwind-protect
40 (progn
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
49 ;; returns nil.
50 (sleep-for 1)
51 (auto-revert-mode 1)
52 (should auto-revert-mode)
54 ;; Modify file. We wait for a second, in order to have
55 ;; another timestamp.
56 (sleep-for 1)
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*"
62 (while
63 (null (string-match
64 (format "Reverting buffer `%s'." (buffer-name buf))
65 (buffer-string)))
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)
73 (sleep-for 1)
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*"
79 (while
80 (null (string-match
81 (format "Reverting buffer `%s'." (buffer-name buf))
82 (buffer-string)))
83 (read-event nil nil 0.1))))
84 (should-not (string-match "any text" (buffer-string)))))
86 ;; Exit.
87 (with-current-buffer "*Messages*" (widen))
88 (ignore-errors
89 (with-current-buffer buf (set-buffer-modified-p nil))
90 (kill-buffer buf))
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"))
98 buf)
99 (unwind-protect
100 (progn
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
108 ;; returns nil.
109 (sleep-for 1)
110 (auto-revert-tail-mode 1)
111 (should auto-revert-tail-mode)
112 (erase-buffer)
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.
118 (sleep-for 1)
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*"
124 (while
125 (null (string-match
126 (format "Reverting buffer `%s'." (buffer-name buf))
127 (buffer-string)))
128 (read-event nil nil 0.1))))
129 (should
130 (string-match "modified text\nanother text" (buffer-string)))))
132 ;; Exit.
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))
143 buf)
144 (unwind-protect
145 (progn
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
152 ;; returns nil.
153 (sleep-for 1)
154 (auto-revert-mode 1)
155 (should auto-revert-mode)
156 (should
157 (string-match name (substring-no-properties (buffer-string))))
159 ;; Delete file. We wait for a second, in order to have
160 ;; another timestamp.
161 (sleep-for 1)
162 (delete-file tmpfile)
164 ;; Check, that the buffer has been reverted.
165 (with-timeout (auto-revert--timeout nil)
166 (with-current-buffer "*Messages*"
167 (while
168 (null (string-match
169 (format "Reverting buffer `%s'." (buffer-name buf))
170 (buffer-string)))
171 (read-event nil nil 0.1))))
172 (should
173 (null
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)
181 (sleep-for 1)
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*"
187 (while
188 (null (string-match
189 (format "Reverting buffer `%s'." (buffer-name buf))
190 (buffer-string)))
191 (read-event nil nil 0.1))))
192 (should
193 (string-match name (substring-no-properties (buffer-string))))))
195 ;; Exit.
196 (with-current-buffer "*Messages*" (widen))
197 (ignore-errors
198 (with-current-buffer buf (set-buffer-modified-p nil))
199 (kill-buffer buf))
200 (ignore-errors (delete-file tmpfile)))))
202 (defun auto-revert-test-all (&optional interactive)
203 "Run all tests for \\[auto-revert]."
204 (interactive "p")
205 (if interactive
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