1 ;;; file-notify-tests.el --- Tests of file notifications
3 ;; Copyright (C) 2013 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. 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'.
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
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
)
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 (not (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'
77 (defun file-notify--test-remote-enabled ()
78 "Whether remote file notification is enabled."
79 (unless (consp file-notify--test-remote-enabled-checked
)
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
)
88 (file-notify-add-watch
89 file-notify-test-remote-temporary-file-directory
92 (setq file-notify--test-remote-enabled-checked
(cons t desc
))
93 (when desc
(file-notify-rm-watch desc
)))))
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")) ()
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.
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))
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))
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
)
147 (equal (should-error (file-notify-add-watch 1 2 3))
148 '(wrong-type-argument 1)))
150 (equal (should-error (file-notify-add-watch
151 temporary-file-directory
2 3))
152 '(wrong-type-argument 2)))
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'
165 ;(message "Event %S" file-notify--test-event)
166 ;; Check the file name.
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
)
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."
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."
188 (make-temp-name "file-notify-test") temporary-file-directory
))
190 (ert-deftest file-notify-test02-events
()
191 "Check file creation/removal notifications."
192 (skip-unless (file-notify--test-local-enabled))
196 (setq file-notify--test-results nil
197 file-notify--test-tmpfile
(file-notify--test-make-temp-name)
198 file-notify--test-tmpfile1
(file-notify--test-make-temp-name)
200 (file-notify-add-watch
201 file-notify--test-tmpfile
202 '(change) 'file-notify--test-event-handler
))
204 ;; Check creation and removal.
206 "any text" nil file-notify--test-tmpfile nil
'no-message
)
207 (delete-file file-notify--test-tmpfile
)
209 ;; Check copy and rename.
211 "any text" nil file-notify--test-tmpfile nil
'no-message
)
212 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1
)
213 (delete-file file-notify--test-tmpfile
)
214 (delete-file file-notify--test-tmpfile1
)
217 "any text" nil file-notify--test-tmpfile nil
'no-message
)
218 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1
)
219 (delete-file file-notify--test-tmpfile1
))
221 ;; Wait for events, and exit.
222 (sit-for 5 'nodisplay
)
223 (file-notify-rm-watch desc
)
224 (ignore-errors (delete-file file-notify--test-tmpfile
))
225 (ignore-errors (delete-file file-notify--test-tmpfile1
))))
227 (dolist (result file-notify--test-results
)
228 ;(message "%s" (ert-test-result-messages result))
229 (when (ert-test-failed-p result
)
230 (ert-fail (cadr (ert-test-result-with-condition-condition result
))))))
232 (file-notify--deftest-remote file-notify-test02-events
233 "Check file creation/removal notifications for remote files.")
235 ;; autorevert runs only in interactive mode.
236 (defvar auto-revert-remote-files
)
237 (setq auto-revert-remote-files t
)
238 (require 'autorevert
)
240 (ert-deftest file-notify-test03-autorevert
()
241 "Check autorevert via file notification.
242 This test is skipped in batch mode."
243 (skip-unless (file-notify--test-local-enabled))
244 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
245 ;; file has been reverted.
246 (let* ((remote (file-remote-p temporary-file-directory
))
247 (timeout (if remote
60 10))
251 (setq file-notify--test-tmpfile
(file-notify--test-make-temp-name))
254 "any text" nil file-notify--test-tmpfile nil
'no-message
)
255 (setq buf
(find-file-noselect file-notify--test-tmpfile
))
256 (with-current-buffer buf
257 (should (string-equal (buffer-string) "any text"))
260 ;; `auto-revert-buffers' runs every 5".
261 (with-timeout (timeout (ignore))
262 (while (null auto-revert-notify-watch-descriptor
)
263 (sit-for 1 'nodisplay
)))
265 ;; Check, that file notification has been used.
266 (should auto-revert-mode
)
267 (should auto-revert-use-notify
)
268 (should auto-revert-notify-watch-descriptor
)
270 ;; Modify file. We wait for a second, in order to
271 ;; have another timestamp.
274 (format "echo -n 'another text' >%s"
275 (or (file-remote-p file-notify--test-tmpfile
'localname
)
276 file-notify--test-tmpfile
)))
278 ;; Check, that the buffer has been reverted.
279 (with-current-buffer (get-buffer-create "*Messages*")
280 (with-timeout (timeout (ignore))
283 (format "Reverting buffer `%s'." (buffer-name buf
))
285 ;; We must trigger the process filter to run.
286 (when remote
(accept-process-output nil
1))
287 (sit-for 1 'nodisplay
))))
288 (should (string-match "another text" (buffer-string)))))
291 (ignore-errors (kill-buffer buf
))
292 (ignore-errors (delete-file file-notify--test-tmpfile
)))))
294 (file-notify--deftest-remote file-notify-test03-autorevert
295 "Check autorevert via file notification for remote files.
296 This test is skipped in batch mode.")
298 (defun file-notify-test-all (&optional interactive
)
299 "Run all tests for \\[file-notify]."
302 (ert-run-tests-interactively "^file-notify-")
303 (ert-run-tests-batch "^file-notify-")))
305 (provide 'file-notify-tests
)
306 ;;; file-notify-tests.el ends here