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/'.
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'.
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
45 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
46 ((eq system-type
'windows-nt
) null-device
)
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-desc nil
)
61 (defvar file-notify--test-results nil
)
62 (defvar file-notify--test-event nil
)
64 (setq password-cache-expiry nil
66 tramp-message-show-message nil
)
68 ;; This shall happen on hydra only.
69 (when (getenv "NIX_STORE")
70 (add-to-list 'tramp-remote-path
'tramp-own-remote-path
))
72 ;; We do not want to try and fail `file-notify-add-watch'.
73 (defun file-notify--test-local-enabled ()
74 "Whether local file notification is enabled.
75 This is needed for local `temporary-file-directory' only, in the
76 remote case we return always t."
77 (or file-notify--library
78 (file-remote-p temporary-file-directory
)))
80 (defvar file-notify--test-remote-enabled-checked nil
81 "Cached result of `file-notify--test-remote-enabled'.
82 If the function did run, the value is a cons cell, the `cdr'
85 (defun file-notify--test-remote-enabled ()
86 "Whether remote file notification is enabled."
87 (unless (consp file-notify--test-remote-enabled-checked
)
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
)
95 (file-notify-add-watch
96 file-notify-test-remote-temporary-file-directory
98 (setq file-notify--test-remote-enabled-checked
(cons t desc
))
99 (when desc
(file-notify-rm-watch desc
))))
101 (cdr file-notify--test-remote-enabled-checked
))
103 (defmacro file-notify--deftest-remote
(test docstring
)
104 "Define ert `TEST-remote' for remote files."
106 `(ert-deftest ,(intern (concat (symbol-name test
) "-remote")) ()
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))
120 (setq file-notify--test-desc
121 (file-notify-add-watch temporary-file-directory
'(change) 'ignore
)))
122 (file-notify-rm-watch file-notify--test-desc
))
124 (file-notify--deftest-remote file-notify-test00-availability
125 "Test availability of `file-notify' for remote files.")
127 (ert-deftest file-notify-test01-add-watch
()
128 "Check `file-notify-add-watch'."
129 (skip-unless (file-notify--test-local-enabled))
130 ;; Check, that different valid parameters are accepted.
132 (setq file-notify--test-desc
133 (file-notify-add-watch temporary-file-directory
'(change) 'ignore
)))
134 (file-notify-rm-watch file-notify--test-desc
)
136 (setq file-notify--test-desc
137 (file-notify-add-watch
138 temporary-file-directory
'(attribute-change) 'ignore
)))
139 (file-notify-rm-watch file-notify--test-desc
)
141 (setq file-notify--test-desc
142 (file-notify-add-watch
143 temporary-file-directory
'(change attribute-change
) 'ignore
)))
144 (file-notify-rm-watch file-notify--test-desc
)
146 ;; Check error handling.
147 (should-error (file-notify-add-watch 1 2 3 4)
148 :type
'wrong-number-of-arguments
)
151 (file-notify-add-watch 1 2 3))
152 '(wrong-type-argument 1)))
155 (file-notify-add-watch temporary-file-directory
2 3))
156 '(wrong-type-argument 2)))
159 (file-notify-add-watch temporary-file-directory
'(change) 3))
160 '(wrong-type-argument 3))))
162 (file-notify--deftest-remote file-notify-test01-add-watch
163 "Check `file-notify-add-watch' for remote files.")
165 (defun file-notify--test-event-test ()
166 "Ert test function to be called by `file-notify--test-event-handler'.
167 We cannot pass arguments, so we assume that `file-notify--test-event'
169 ;(message "Event %S" file-notify--test-event)
170 ;; Check the descriptor.
171 (should (equal (car file-notify--test-event
) file-notify--test-desc
))
172 ;; Check the file name.
174 (string-equal (file-notify--event-file-name file-notify--test-event
)
175 file-notify--test-tmpfile
))
176 ;; Check the second file name if exists.
177 (when (eq (nth 1 file-notify--test-event
) 'renamed
)
180 (file-notify--event-file1-name file-notify--test-event
)
181 file-notify--test-tmpfile1
))))
183 (defun file-notify--test-event-handler (file-notify--test-event)
184 "Run a test over FILE-NOTIFY--TEST-EVENT.
185 Save the result in `file-notify--test-results', for later analysis."
187 (ert-run-test (make-ert-test :body
'file-notify--test-event-test
))))
188 (setq file-notify--test-results
189 (append file-notify--test-results
`(,result
)))))
191 (defun file-notify--test-make-temp-name ()
192 "Create a temporary file name for test."
194 (make-temp-name "file-notify-test") temporary-file-directory
))
196 (defmacro file-notify--wait-for-events
(timeout until
)
197 "Wait for file notification events until form UNTIL is true.
198 TIMEOUT is the maximum time to wait for, in seconds."
199 `(with-timeout (,timeout
(ignore))
201 (read-event nil nil
0.1))))
203 (ert-deftest file-notify-test02-events
()
204 "Check file creation/removal notifications."
205 (skip-unless (file-notify--test-local-enabled))
208 (setq file-notify--test-results nil
209 file-notify--test-tmpfile
(file-notify--test-make-temp-name)
210 file-notify--test-tmpfile1
(file-notify--test-make-temp-name)
211 file-notify--test-desc
212 (file-notify-add-watch
213 file-notify--test-tmpfile
214 '(change) 'file-notify--test-event-handler
))
215 (should file-notify--test-desc
)
217 ;; Check creation and removal.
219 "any text" nil file-notify--test-tmpfile nil
'no-message
)
220 (delete-file file-notify--test-tmpfile
)
223 ;; Check copy and rename.
225 "any text" nil file-notify--test-tmpfile nil
'no-message
)
226 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1
)
227 (delete-file file-notify--test-tmpfile
)
228 (delete-file file-notify--test-tmpfile1
)
232 "any text" nil file-notify--test-tmpfile nil
'no-message
)
233 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1
)
234 (delete-file file-notify--test-tmpfile1
)
237 ;; Wait for events, and exit.
238 (file-notify--wait-for-events 5 file-notify--test-results
)
239 (file-notify-rm-watch file-notify--test-desc
)
240 (ignore-errors (delete-file file-notify--test-tmpfile
))
241 (ignore-errors (delete-file file-notify--test-tmpfile1
)))
243 (should file-notify--test-results
)
244 (dolist (result file-notify--test-results
)
245 ;(message "%s" (ert-test-result-messages result))
246 (when (ert-test-failed-p result
)
247 (ert-fail (cadr (ert-test-result-with-condition-condition result
))))))
249 (file-notify--deftest-remote file-notify-test02-events
250 "Check file creation/removal notifications for remote files.")
252 (require 'autorevert
)
253 (setq auto-revert-notify-exclude-dir-regexp
"nothing-to-be-excluded"
254 auto-revert-remote-files t
255 auto-revert-stop-on-user-input nil
)
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 ((timeout (if (file-remote-p temporary-file-directory
) 60 10))
267 (setq file-notify--test-tmpfile
(file-notify--test-make-temp-name))
270 "any text" nil file-notify--test-tmpfile nil
'no-message
)
271 (setq buf
(find-file-noselect file-notify--test-tmpfile
))
272 (with-current-buffer buf
273 (should (string-equal (buffer-string) "any text"))
274 ;; `buffer-stale--default-function' checks for
275 ;; `verify-visited-file-modtime'. We must ensure that it
280 ;; `auto-revert-buffers' runs every 5".
281 (with-timeout (timeout (ignore))
282 (while (null auto-revert-notify-watch-descriptor
)
285 ;; Check, that file notification has been used.
286 (should auto-revert-mode
)
287 (should auto-revert-use-notify
)
288 (should auto-revert-notify-watch-descriptor
)
290 ;; Modify file. We wait for a second, in order to
291 ;; have another timestamp.
294 "another text" nil file-notify--test-tmpfile nil
'no-message
)
296 ;; Check, that the buffer has been reverted.
297 (with-current-buffer (get-buffer-create "*Messages*")
298 (file-notify--wait-for-events
300 (string-match (format "Reverting buffer `%s'." (buffer-name buf
))
302 (should (string-match "another text" (buffer-string)))))
305 (ignore-errors (kill-buffer buf
))
306 (ignore-errors (delete-file file-notify--test-tmpfile
)))))
308 (file-notify--deftest-remote file-notify-test03-autorevert
309 "Check autorevert via file notification for remote files.
310 This test is skipped in batch mode.")
312 (defun file-notify-test-all (&optional interactive
)
313 "Run all tests for \\[file-notify]."
316 (ert-run-tests-interactively "^file-notify-")
317 (ert-run-tests-batch "^file-notify-")))
319 (provide 'file-notify-tests
)
320 ;;; file-notify-tests.el ends here