]> code.delx.au - gnu-emacs/blob - test/automated/auto-revert-tests.el
Adapt tests in auto-revert-tests.el
[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 (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 (ert-deftest auto-revert-test01-auto-revert-tail-mode ()
93 "Check autorevert tail mode."
94 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
95 ;; file has been reverted.
96 (let ((tmpfile (make-temp-file "auto-revert-test"))
97 buf)
98 (unwind-protect
99 (progn
100 (with-current-buffer (get-buffer-create "*Messages*")
101 (narrow-to-region (point-max) (point-max)))
102 (write-region "any text" nil tmpfile nil 'no-message)
103 (setq buf (find-file-noselect tmpfile))
104 (with-current-buffer buf
105 ;; `buffer-stale--default-function' checks for
106 ;; `verify-visited-file-modtime'. We must ensure that it
107 ;; returns nil.
108 (sleep-for 1)
109 (auto-revert-tail-mode 1)
110 (should auto-revert-tail-mode)
111 (erase-buffer)
112 (insert "modified text\n")
113 (set-buffer-modified-p nil)
114
115 ;; Modify file. We wait for a second, in order to have
116 ;; another timestamp.
117 (sleep-for 1)
118 (write-region "another text" nil tmpfile 'append 'no-message)
119
120 ;; Check, that the buffer has been reverted.
121 (auto-revert--wait-for-revert buf)
122 (should
123 (string-match "modified text\nanother text" (buffer-string)))))
124
125 ;; Exit.
126 (with-current-buffer "*Messages*" (widen))
127 (ignore-errors (kill-buffer buf))
128 (ignore-errors (delete-file tmpfile)))))
129
130 (ert-deftest auto-revert-test02-auto-revert-mode-dired ()
131 "Check autorevert for dired."
132 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
133 ;; file has been reverted.
134 (let* ((tmpfile (make-temp-file "auto-revert-test"))
135 (name (file-name-nondirectory tmpfile))
136 buf)
137 (unwind-protect
138 (progn
139 (with-current-buffer (get-buffer-create "*Messages*")
140 (narrow-to-region (point-max) (point-max)))
141 (setq buf (dired-noselect temporary-file-directory))
142 (with-current-buffer buf
143 ;; `buffer-stale--default-function' checks for
144 ;; `verify-visited-file-modtime'. We must ensure that it
145 ;; returns nil.
146 (sleep-for 1)
147 (auto-revert-mode 1)
148 (should auto-revert-mode)
149 (should
150 (string-match name (substring-no-properties (buffer-string))))
151
152 ;; Delete file. We wait for a second, in order to have
153 ;; another timestamp.
154 (sleep-for 1)
155 (delete-file tmpfile)
156
157 ;; Check, that the buffer has been reverted.
158 (auto-revert--wait-for-revert buf)
159 (should-not
160 (string-match name (substring-no-properties (buffer-string))))
161
162 ;; Make dired buffer modified. Check, that the buffer has
163 ;; been still reverted.
164 (with-current-buffer (get-buffer-create "*Messages*")
165 (narrow-to-region (point-max) (point-max)))
166 (set-buffer-modified-p t)
167 (sleep-for 1)
168 (write-region "any text" nil tmpfile nil 'no-message)
169
170 ;; Check, that the buffer has been reverted.
171 (auto-revert--wait-for-revert buf)
172 (should
173 (string-match name (substring-no-properties (buffer-string))))))
174
175 ;; Exit.
176 (with-current-buffer "*Messages*" (widen))
177 (ignore-errors
178 (with-current-buffer buf (set-buffer-modified-p nil))
179 (kill-buffer buf))
180 (ignore-errors (delete-file tmpfile)))))
181
182 (defun auto-revert-test-all (&optional interactive)
183 "Run all tests for \\[auto-revert]."
184 (interactive "p")
185 (if interactive
186 (ert-run-tests-interactively "^auto-revert-")
187 (ert-run-tests-batch "^auto-revert-")))
188
189 (provide 'auto-revert-tests)
190 ;;; auto-revert-tests.el ends here