Improve on previous quote autopairing change
[emacs/old-mirror.git] / test / automated / file-notify-tests.el
blobf1d9aee974cf80c948e366544c5011cc77be165d
1 ;;; file-notify-tests.el --- Tests of file notifications
3 ;; Copyright (C) 2013-2014 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 ;; Some of the tests require access to a remote host files. Set
23 ;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order
24 ;; to overwrite the default value. If you want to skip tests
25 ;; accessing a remote host, set this environment variable to
26 ;; "/dev/null" or whatever is appropriate on your system.
28 ;; When running the tests in batch mode, it must NOT require an
29 ;; interactive password prompt unless the environment variable
30 ;; $REMOTE_ALLOW_PASSWORD is set.
32 ;; A whole test run can be performed calling the command `file-notify-test-all'.
34 ;;; Code:
36 (require 'ert)
37 (require 'filenotify)
39 ;; There is no default value on w32 systems, which could work out of the box.
40 (defconst file-notify-test-remote-temporary-file-directory
41 (cond
42 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
43 ((eq system-type 'windows-nt) null-device)
44 (t (format "/ssh::%s" temporary-file-directory)))
45 "Temporary directory for Tramp tests.")
47 (defvar file-notify--test-tmpfile nil)
48 (defvar file-notify--test-tmpfile1 nil)
49 (defvar file-notify--test-results nil)
50 (defvar file-notify--test-event nil)
52 (require 'tramp)
53 (setq tramp-verbose 0
54 tramp-message-show-message nil)
56 ;; Disable interactive passwords in batch mode.
57 (when (and noninteractive (not (getenv "REMOTE_ALLOW_PASSWORD")))
58 (defalias 'tramp-read-passwd 'ignore))
60 ;; This shall happen on hydra only.
61 (when (getenv "NIX_STORE")
62 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
64 ;; We do not want to try and fail `file-notify-add-watch'.
65 (defun file-notify--test-local-enabled ()
66 "Whether local file notification is enabled.
67 This is needed for local `temporary-file-directory' only, in the
68 remote case we return always `t'."
69 (or file-notify--library
70 (file-remote-p temporary-file-directory)))
72 (defvar file-notify--test-remote-enabled-checked nil
73 "Cached result of `file-notify--test-remote-enabled'.
74 If the function did run, the value is a cons cell, the `cdr'
75 being the result.")
77 (defun file-notify--test-remote-enabled ()
78 "Whether remote file notification is enabled."
79 (unless (consp file-notify--test-remote-enabled-checked)
80 (let (desc)
81 (unwind-protect
82 (ignore-errors
83 (and
84 (file-remote-p file-notify-test-remote-temporary-file-directory)
85 (file-directory-p file-notify-test-remote-temporary-file-directory)
86 (file-writable-p file-notify-test-remote-temporary-file-directory)
87 (setq desc
88 (file-notify-add-watch
89 file-notify-test-remote-temporary-file-directory
90 '(change) 'ignore))))
91 ;; Unwind forms.
92 (setq file-notify--test-remote-enabled-checked (cons t desc))
93 (when desc (file-notify-rm-watch desc)))))
94 ;; Return result.
95 (cdr file-notify--test-remote-enabled-checked))
97 (defmacro file-notify--deftest-remote (test docstring)
98 "Define ert `TEST-remote' for remote files."
99 `(ert-deftest ,(intern (concat (symbol-name test) "-remote")) ()
100 ,docstring
101 (let* ((temporary-file-directory
102 file-notify-test-remote-temporary-file-directory)
103 (ert-test (ert-get-test ',test)))
104 (skip-unless (file-notify--test-remote-enabled))
105 (tramp-cleanup-connection
106 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password)
107 (funcall (ert-test-body ert-test)))))
109 (ert-deftest file-notify-test00-availability ()
110 "Test availability of `file-notify'."
111 (skip-unless (file-notify--test-local-enabled))
112 (let (desc)
113 ;; Check, that different valid parameters are accepted.
114 (should (setq desc (file-notify-add-watch
115 temporary-file-directory '(change) 'ignore)))
116 (file-notify-rm-watch desc)))
118 (file-notify--deftest-remote file-notify-test00-availability
119 "Test availability of `file-notify' for remote files.")
121 (ert-deftest file-notify-test01-add-watch ()
122 "Check `file-notify-add-watch'."
123 (skip-unless (file-notify--test-local-enabled))
124 (let (desc)
125 ;; Check, that different valid parameters are accepted.
126 (should (setq desc (file-notify-add-watch
127 temporary-file-directory '(change) 'ignore)))
128 (file-notify-rm-watch desc)
129 (should (setq desc (file-notify-add-watch
130 temporary-file-directory
131 '(attribute-change) 'ignore)))
132 (file-notify-rm-watch desc)
133 (should (setq desc (file-notify-add-watch
134 temporary-file-directory
135 '(change attribute-change) 'ignore)))
136 (file-notify-rm-watch desc)
138 ;; Check error handling.
139 (should-error (file-notify-add-watch 1 2 3 4)
140 :type 'wrong-number-of-arguments)
141 (should
142 (equal (should-error (file-notify-add-watch 1 2 3))
143 '(wrong-type-argument 1)))
144 (should
145 (equal (should-error (file-notify-add-watch
146 temporary-file-directory 2 3))
147 '(wrong-type-argument 2)))
148 (should
149 (equal (should-error (file-notify-add-watch
150 temporary-file-directory '(change) 3))
151 '(wrong-type-argument 3)))))
153 (file-notify--deftest-remote file-notify-test01-add-watch
154 "Check `file-notify-add-watch' for remote files.")
156 (defun file-notify--test-event-test ()
157 "Ert test function to be called by `file-notify--test-event-handler'.
158 We cannot pass arguments, so we assume that `file-notify--test-event'
159 is bound somewhere."
160 ;(message "Event %S" file-notify--test-event)
161 ;; Check the file name.
162 (should
163 (string-equal (file-notify--event-file-name file-notify--test-event)
164 file-notify--test-tmpfile))
165 ;; Check the second file name if exists.
166 (when (eq (nth 1 file-notify--test-event) 'renamed)
167 (should
168 (string-equal
169 (file-notify--event-file1-name file-notify--test-event)
170 file-notify--test-tmpfile1))))
172 (defun file-notify--test-event-handler (file-notify--test-event)
173 "Run a test over FILE-NOTIFY--TEST-EVENT.
174 Save the result in `file-notify--test-results', for later analysis."
175 (let ((result
176 (ert-run-test (make-ert-test :body 'file-notify--test-event-test))))
177 (setq file-notify--test-results
178 (append file-notify--test-results `(,result)))))
180 (defun file-notify--test-make-temp-name ()
181 "Create a temporary file name for test."
182 (expand-file-name
183 (make-temp-name "file-notify-test") temporary-file-directory))
185 (defmacro file-notify--wait-for-events (timeout until)
186 "Wait for file notification events until form UNTIL is true.
187 TIMEOUT is the maximum time to wait for, in seconds."
188 `(with-timeout (,timeout (ignore))
189 (while (null ,until)
190 (read-event nil nil 0.1))))
192 (ert-deftest file-notify-test02-events ()
193 "Check file creation/removal notifications."
194 (skip-unless (file-notify--test-local-enabled))
195 (let (desc)
196 (unwind-protect
197 (progn
198 (setq file-notify--test-results nil
199 file-notify--test-tmpfile (file-notify--test-make-temp-name)
200 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
201 desc
202 (file-notify-add-watch
203 file-notify--test-tmpfile
204 '(change) 'file-notify--test-event-handler))
206 ;; Check creation and removal.
207 (write-region
208 "any text" nil file-notify--test-tmpfile nil 'no-message)
209 (delete-file file-notify--test-tmpfile)
210 (sleep-for 0.1)
212 ;; Check copy and rename.
213 (write-region
214 "any text" nil file-notify--test-tmpfile nil 'no-message)
215 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1)
216 (delete-file file-notify--test-tmpfile)
217 (delete-file file-notify--test-tmpfile1)
218 (sleep-for 0.1)
220 (write-region
221 "any text" nil file-notify--test-tmpfile nil 'no-message)
222 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1)
223 (delete-file file-notify--test-tmpfile1)
224 (sleep-for 0.1))
226 ;; Wait for events, and exit.
227 (file-notify--wait-for-events 5 file-notify--test-results)
228 (file-notify-rm-watch desc)
229 (ignore-errors (delete-file file-notify--test-tmpfile))
230 (ignore-errors (delete-file file-notify--test-tmpfile1))))
232 (should file-notify--test-results)
233 (dolist (result file-notify--test-results)
234 ;(message "%s" (ert-test-result-messages result))
235 (when (ert-test-failed-p result)
236 (ert-fail (cadr (ert-test-result-with-condition-condition result))))))
238 (file-notify--deftest-remote file-notify-test02-events
239 "Check file creation/removal notifications for remote files.")
241 (defvar auto-revert-remote-files)
242 (defvar auto-revert-stop-on-user-input)
243 (setq auto-revert-remote-files t
244 auto-revert-stop-on-user-input nil)
245 (require 'autorevert)
247 (ert-deftest file-notify-test03-autorevert ()
248 "Check autorevert via file notification.
249 This test is skipped in batch mode."
250 (skip-unless (file-notify--test-local-enabled))
251 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
252 ;; file has been reverted.
253 (let* ((remote (file-remote-p temporary-file-directory))
254 (timeout (if remote 60 10))
255 buf)
256 (unwind-protect
257 (progn
258 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
260 (write-region
261 "any text" nil file-notify--test-tmpfile nil 'no-message)
262 (setq buf (find-file-noselect file-notify--test-tmpfile))
263 (with-current-buffer buf
264 (should (string-equal (buffer-string) "any text"))
265 (auto-revert-mode 1)
267 ;; `auto-revert-buffers' runs every 5".
268 (with-timeout (timeout (ignore))
269 (while (null auto-revert-notify-watch-descriptor)
270 (sleep-for 1)))
272 ;; Check, that file notification has been used.
273 (should auto-revert-mode)
274 (should auto-revert-use-notify)
275 (should auto-revert-notify-watch-descriptor)
277 ;; Modify file. We wait for a second, in order to
278 ;; have another timestamp.
279 (sleep-for 1)
280 (shell-command
281 (format "echo -n 'another text' >%s"
282 (or (file-remote-p file-notify--test-tmpfile 'localname)
283 file-notify--test-tmpfile)))
285 ;; Check, that the buffer has been reverted.
286 (with-current-buffer (get-buffer-create "*Messages*")
287 (file-notify--wait-for-events
288 timeout
289 (string-match (format "Reverting buffer `%s'." (buffer-name buf))
290 (buffer-string))))
291 (should (string-match "another text" (buffer-string)))))
293 ;; Exit.
294 (ignore-errors (kill-buffer buf))
295 (ignore-errors (delete-file file-notify--test-tmpfile)))))
297 (file-notify--deftest-remote file-notify-test03-autorevert
298 "Check autorevert via file notification for remote files.
299 This test is skipped in batch mode.")
301 (defun file-notify-test-all (&optional interactive)
302 "Run all tests for \\[file-notify]."
303 (interactive "p")
304 (if interactive
305 (ert-run-tests-interactively "^file-notify-")
306 (ert-run-tests-batch "^file-notify-")))
308 (provide 'file-notify-tests)
309 ;;; file-notify-tests.el ends here