* automated/file-notify-tests.el (file-notify-test02-events):
[emacs.git] / test / automated / file-notify-tests.el
blobde0f32b5e94a3d7cb30418d2cf3575c4044b95d1
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 ;; The local test could have passed, skipped, or quit. All of
106 ;; these results should not prevent us to run the remote test.
107 ;; That's why we skip only for failed local tests.
108 (skip-unless
109 (not (ert-test-failed-p (ert-test-most-recent-result ert-test))))
110 (tramp-cleanup-connection
111 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password)
112 (funcall (ert-test-body ert-test)))))
114 (ert-deftest file-notify-test00-availability ()
115 "Test availability of `file-notify'."
116 (skip-unless (file-notify--test-local-enabled))
117 (let (desc)
118 ;; Check, that different valid parameters are accepted.
119 (should (setq desc (file-notify-add-watch
120 temporary-file-directory '(change) 'ignore)))
121 (file-notify-rm-watch desc)))
123 (file-notify--deftest-remote file-notify-test00-availability
124 "Test availability of `file-notify' for remote files.")
126 (ert-deftest file-notify-test01-add-watch ()
127 "Check `file-notify-add-watch'."
128 (skip-unless (file-notify--test-local-enabled))
129 (let (desc)
130 ;; Check, that different valid parameters are accepted.
131 (should (setq desc (file-notify-add-watch
132 temporary-file-directory '(change) 'ignore)))
133 (file-notify-rm-watch desc)
134 (should (setq desc (file-notify-add-watch
135 temporary-file-directory
136 '(attribute-change) 'ignore)))
137 (file-notify-rm-watch desc)
138 (should (setq desc (file-notify-add-watch
139 temporary-file-directory
140 '(change attribute-change) 'ignore)))
141 (file-notify-rm-watch desc)
143 ;; Check error handling.
144 (should-error (file-notify-add-watch 1 2 3 4)
145 :type 'wrong-number-of-arguments)
146 (should
147 (equal (should-error (file-notify-add-watch 1 2 3))
148 '(wrong-type-argument 1)))
149 (should
150 (equal (should-error (file-notify-add-watch
151 temporary-file-directory 2 3))
152 '(wrong-type-argument 2)))
153 (should
154 (equal (should-error (file-notify-add-watch
155 temporary-file-directory '(change) 3))
156 '(wrong-type-argument 3)))))
158 (file-notify--deftest-remote file-notify-test01-add-watch
159 "Check `file-notify-add-watch' for remote files.")
161 (defun file-notify--test-event-test ()
162 "Ert test function to be called by `file-notify--test-event-handler'.
163 We cannot pass arguments, so we assume that `file-notify--test-event'
164 is bound somewhere."
165 ;(message "Event %S" file-notify--test-event)
166 ;; Check the file name.
167 (should
168 (string-equal (file-notify--event-file-name file-notify--test-event)
169 file-notify--test-tmpfile))
170 ;; Check the second file name if exists.
171 (when (eq (nth 1 file-notify--test-event) 'renamed)
172 (should
173 (string-equal
174 (file-notify--event-file1-name file-notify--test-event)
175 file-notify--test-tmpfile1))))
177 (defun file-notify--test-event-handler (file-notify--test-event)
178 "Run a test over FILE-NOTIFY--TEST-EVENT.
179 Save the result in `file-notify--test-results', for later analysis."
180 (let ((result
181 (ert-run-test (make-ert-test :body 'file-notify--test-event-test))))
182 (setq file-notify--test-results
183 (append file-notify--test-results `(,result)))))
185 (defun file-notify--test-make-temp-name ()
186 "Create a temporary file name for test."
187 (expand-file-name
188 (make-temp-name "file-notify-test") temporary-file-directory))
190 (defmacro file-notify--wait-for-events (timeout until)
191 "Wait for file notification events until form UNTIL is true.
192 TIMEOUT is the maximum time to wait for."
193 `(with-timeout (,timeout (ignore))
194 (while (null ,until)
195 ;; glib events, and remote events.
196 (accept-process-output nil 0.1)
197 ;; inotify events.
198 (read-event nil nil 0.1))))
200 (ert-deftest file-notify-test02-events ()
201 "Check file creation/removal notifications."
202 (skip-unless (file-notify--test-local-enabled))
203 (let (desc)
204 (unwind-protect
205 (progn
206 (setq file-notify--test-results nil
207 file-notify--test-tmpfile (file-notify--test-make-temp-name)
208 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
209 desc
210 (file-notify-add-watch
211 file-notify--test-tmpfile
212 '(change) 'file-notify--test-event-handler))
214 ;; Check creation and removal.
215 (write-region
216 "any text" nil file-notify--test-tmpfile nil 'no-message)
217 (delete-file file-notify--test-tmpfile)
218 (sit-for 0.1 'nodisplay)
220 ;; Check copy and rename.
221 (write-region
222 "any text" nil file-notify--test-tmpfile nil 'no-message)
223 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1)
224 (delete-file file-notify--test-tmpfile)
225 (delete-file file-notify--test-tmpfile1)
226 (sit-for 0.1 'nodisplay)
228 (write-region
229 "any text" nil file-notify--test-tmpfile nil 'no-message)
230 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1)
231 (delete-file file-notify--test-tmpfile1)
232 (sit-for 0.1 'nodisplay))
234 ;; Wait for events, and exit.
235 (file-notify--wait-for-events 5 file-notify--test-results)
236 (file-notify-rm-watch desc)
237 (ignore-errors (delete-file file-notify--test-tmpfile))
238 (ignore-errors (delete-file file-notify--test-tmpfile1))))
240 (should
241 (or file-notify--test-results
242 (and noninteractive (eq file-notify--library 'gfilenotify)))) ;; Bug#16519.
243 (dolist (result file-notify--test-results)
244 ;(message "%s" (ert-test-result-messages result))
245 (when (ert-test-failed-p result)
246 (ert-fail (cadr (ert-test-result-with-condition-condition result))))))
248 (file-notify--deftest-remote file-notify-test02-events
249 "Check file creation/removal notifications for remote files.")
251 (defvar auto-revert-remote-files)
252 (defvar auto-revert-stop-on-user-input)
253 (setq auto-revert-remote-files t
254 auto-revert-stop-on-user-input nil)
255 (require 'autorevert)
257 (ert-deftest file-notify-test03-autorevert ()
258 "Check autorevert via file notification.
259 This test is skipped in batch mode."
260 (skip-unless (file-notify--test-local-enabled))
261 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
262 ;; file has been reverted.
263 (let* ((remote (file-remote-p temporary-file-directory))
264 (timeout (if remote 60 10))
265 buf)
266 (unwind-protect
267 (progn
268 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
270 (write-region
271 "any text" nil file-notify--test-tmpfile nil 'no-message)
272 (setq buf (find-file-noselect file-notify--test-tmpfile))
273 (with-current-buffer buf
274 (should (string-equal (buffer-string) "any text"))
275 (auto-revert-mode 1)
277 ;; `auto-revert-buffers' runs every 5".
278 (with-timeout (timeout (ignore))
279 (while (null auto-revert-notify-watch-descriptor)
280 (sit-for 1 'nodisplay)))
282 ;; Check, that file notification has been used.
283 (should auto-revert-mode)
284 (should auto-revert-use-notify)
285 (should auto-revert-notify-watch-descriptor)
287 ;; Modify file. We wait for a second, in order to
288 ;; have another timestamp.
289 (sit-for 1)
290 (shell-command
291 (format "echo -n 'another text' >%s"
292 (or (file-remote-p file-notify--test-tmpfile 'localname)
293 file-notify--test-tmpfile)))
295 ;; Check, that the buffer has been reverted.
296 (with-current-buffer (get-buffer-create "*Messages*")
297 (file-notify--wait-for-events
298 timeout
299 (string-match (format "Reverting buffer `%s'." (buffer-name buf))
300 (buffer-string))))
301 (should (string-match "another text" (buffer-string)))))
303 ;; Exit.
304 (ignore-errors (kill-buffer buf))
305 (ignore-errors (delete-file file-notify--test-tmpfile)))))
307 (file-notify--deftest-remote file-notify-test03-autorevert
308 "Check autorevert via file notification for remote files.
309 This test is skipped in batch mode.")
311 (defun file-notify-test-all (&optional interactive)
312 "Run all tests for \\[file-notify]."
313 (interactive "p")
314 (if interactive
315 (ert-run-tests-interactively "^file-notify-")
316 (ert-run-tests-batch "^file-notify-")))
318 (provide 'file-notify-tests)
319 ;;; file-notify-tests.el ends here