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
)
94 (setq file-notify--test-desc
95 (file-notify-add-watch
96 file-notify-test-remote-temporary-file-directory
99 (setq file-notify--test-remote-enabled-checked
100 (cons t file-notify--test-desc
))
101 (when file-notify--test-desc
102 (file-notify-rm-watch file-notify--test-desc
))))
104 (cdr file-notify--test-remote-enabled-checked
))
106 (defmacro file-notify--deftest-remote
(test docstring
)
107 "Define ert `TEST-remote' for remote files."
109 `(ert-deftest ,(intern (concat (symbol-name test
) "-remote")) ()
112 (let* ((temporary-file-directory
113 file-notify-test-remote-temporary-file-directory
)
114 (ert-test (ert-get-test ',test
)))
115 (skip-unless (file-notify--test-remote-enabled))
116 (tramp-cleanup-connection
117 (tramp-dissect-file-name temporary-file-directory
)
119 (funcall (ert-test-body ert-test
)))
120 ((error quit
) (ert-fail err
)))))
122 (ert-deftest file-notify-test00-availability
()
123 "Test availability of `file-notify'."
124 (skip-unless (file-notify--test-local-enabled))
125 ;; Check, that different valid parameters are accepted.
127 (setq file-notify--test-desc
128 (file-notify-add-watch temporary-file-directory
'(change) 'ignore
)))
129 (file-notify-rm-watch file-notify--test-desc
))
131 (file-notify--deftest-remote file-notify-test00-availability
132 "Test availability of `file-notify' for remote files.")
134 (ert-deftest file-notify-test01-add-watch
()
135 "Check `file-notify-add-watch'."
136 (skip-unless (file-notify--test-local-enabled))
137 ;; Check, that different valid parameters are accepted.
139 (setq file-notify--test-desc
140 (file-notify-add-watch temporary-file-directory
'(change) 'ignore
)))
141 (file-notify-rm-watch file-notify--test-desc
)
143 (setq file-notify--test-desc
144 (file-notify-add-watch
145 temporary-file-directory
'(attribute-change) 'ignore
)))
146 (file-notify-rm-watch file-notify--test-desc
)
148 (setq file-notify--test-desc
149 (file-notify-add-watch
150 temporary-file-directory
'(change attribute-change
) 'ignore
)))
151 (file-notify-rm-watch file-notify--test-desc
)
153 ;; Check error handling.
154 (should-error (file-notify-add-watch 1 2 3 4)
155 :type
'wrong-number-of-arguments
)
158 (file-notify-add-watch 1 2 3))
159 '(wrong-type-argument 1)))
162 (file-notify-add-watch temporary-file-directory
2 3))
163 '(wrong-type-argument 2)))
166 (file-notify-add-watch temporary-file-directory
'(change) 3))
167 '(wrong-type-argument 3))))
169 (file-notify--deftest-remote file-notify-test01-add-watch
170 "Check `file-notify-add-watch' for remote files.")
172 (defun file-notify--test-event-test ()
173 "Ert test function to be called by `file-notify--test-event-handler'.
174 We cannot pass arguments, so we assume that `file-notify--test-event'
176 ;(message "Event %S" file-notify--test-event)
177 ;; Check the descriptor.
178 (should (equal (car file-notify--test-event
) file-notify--test-desc
))
179 ;; Check the file name.
181 (string-equal (file-notify--event-file-name file-notify--test-event
)
182 file-notify--test-tmpfile
))
183 ;; Check the second file name if exists.
184 (when (eq (nth 1 file-notify--test-event
) 'renamed
)
187 (file-notify--event-file1-name file-notify--test-event
)
188 file-notify--test-tmpfile1
))))
190 (defun file-notify--test-event-handler (file-notify--test-event)
191 "Run a test over FILE-NOTIFY--TEST-EVENT.
192 Save the result in `file-notify--test-results', for later analysis."
194 (ert-run-test (make-ert-test :body
'file-notify--test-event-test
))))
195 (setq file-notify--test-results
196 (append file-notify--test-results
`(,result
)))))
198 (defun file-notify--test-make-temp-name ()
199 "Create a temporary file name for test."
201 (make-temp-name "file-notify-test") temporary-file-directory
))
203 (defmacro file-notify--wait-for-events
(timeout until
)
204 "Wait for file notification events until form UNTIL is true.
205 TIMEOUT is the maximum time to wait for, in seconds."
206 `(with-timeout (,timeout
(ignore))
208 (read-event nil nil
0.1))))
210 (ert-deftest file-notify-test02-events
()
211 "Check file creation/removal notifications."
212 (skip-unless (file-notify--test-local-enabled))
215 (setq file-notify--test-results nil
216 file-notify--test-tmpfile
(file-notify--test-make-temp-name)
217 file-notify--test-tmpfile1
(file-notify--test-make-temp-name)
218 file-notify--test-desc
219 (file-notify-add-watch
220 file-notify--test-tmpfile
221 '(change) 'file-notify--test-event-handler
))
222 (should file-notify--test-desc
)
224 ;; Check creation and removal.
226 "any text" nil file-notify--test-tmpfile nil
'no-message
)
227 (delete-file file-notify--test-tmpfile
)
230 ;; Check copy and rename.
232 "any text" nil file-notify--test-tmpfile nil
'no-message
)
233 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1
)
234 (delete-file file-notify--test-tmpfile
)
235 (delete-file file-notify--test-tmpfile1
)
239 "any text" nil file-notify--test-tmpfile nil
'no-message
)
240 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1
)
241 (delete-file file-notify--test-tmpfile1
)
244 ;; Wait for events, and exit.
245 (file-notify--wait-for-events 5 file-notify--test-results
)
246 (file-notify-rm-watch file-notify--test-desc
)
247 (ignore-errors (delete-file file-notify--test-tmpfile
))
248 (ignore-errors (delete-file file-notify--test-tmpfile1
)))
250 (should file-notify--test-results
)
251 (dolist (result file-notify--test-results
)
252 ;(message "%s" (ert-test-result-messages result))
253 (when (ert-test-failed-p result
)
254 (ert-fail (cadr (ert-test-result-with-condition-condition result
))))))
256 (file-notify--deftest-remote file-notify-test02-events
257 "Check file creation/removal notifications for remote files.")
259 (require 'autorevert
)
260 (setq auto-revert-notify-exclude-dir-regexp
"nothing-to-be-excluded"
261 auto-revert-remote-files t
262 auto-revert-stop-on-user-input nil
)
264 (ert-deftest file-notify-test03-autorevert
()
265 "Check autorevert via file notification.
266 This test is skipped in batch mode."
267 (skip-unless (file-notify--test-local-enabled))
268 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
269 ;; file has been reverted.
270 (let ((timeout (if (file-remote-p temporary-file-directory
) 60 10))
274 (setq file-notify--test-tmpfile
(file-notify--test-make-temp-name))
277 "any text" nil file-notify--test-tmpfile nil
'no-message
)
278 (setq buf
(find-file-noselect file-notify--test-tmpfile
))
279 (with-current-buffer buf
280 (should (string-equal (buffer-string) "any text"))
281 ;; `buffer-stale--default-function' checks for
282 ;; `verify-visited-file-modtime'. We must ensure that it
287 ;; `auto-revert-buffers' runs every 5".
288 (with-timeout (timeout (ignore))
289 (while (null auto-revert-notify-watch-descriptor
)
292 ;; Check, that file notification has been used.
293 (should auto-revert-mode
)
294 (should auto-revert-use-notify
)
295 (should auto-revert-notify-watch-descriptor
)
297 ;; Modify file. We wait for a second, in order to
298 ;; have another timestamp.
301 "another text" nil file-notify--test-tmpfile nil
'no-message
)
303 ;; Check, that the buffer has been reverted.
304 (with-current-buffer (get-buffer-create "*Messages*")
305 (file-notify--wait-for-events
307 (string-match (format "Reverting buffer `%s'." (buffer-name buf
))
309 (should (string-match "another text" (buffer-string)))))
312 (ignore-errors (kill-buffer buf
))
313 (ignore-errors (delete-file file-notify--test-tmpfile
)))))
315 (file-notify--deftest-remote file-notify-test03-autorevert
316 "Check autorevert via file notification for remote files.
317 This test is skipped in batch mode.")
319 (defun file-notify-test-all (&optional interactive
)
320 "Run all tests for \\[file-notify]."
323 (ert-run-tests-interactively "^file-notify-")
324 (ert-run-tests-batch "^file-notify-")))
326 (provide 'file-notify-tests
)
327 ;;; file-notify-tests.el ends here