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