1 ;;; file-notify-tests.el --- Tests of file notifications -*- lexical-binding: t; -*-
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)
63 (defvar file-notify--test-events nil)
65 (defun file-notify--test-timeout ()
66 "Timeout to wait for arriving events, in seconds."
68 ((file-remote-p temporary-file-directory) 6)
69 ((string-equal (file-notify--test-library) "w32notify") 20)
70 ((eq system-type 'cygwin) 10)
73 (defun file-notify--test-cleanup ()
74 "Cleanup after a test."
75 (file-notify-rm-watch file-notify--test-desc)
77 (when (and file-notify--test-tmpfile
78 (file-exists-p file-notify--test-tmpfile))
79 (if (file-directory-p file-notify--test-tmpfile)
80 (delete-directory file-notify--test-tmpfile 'recursive)
81 (delete-file file-notify--test-tmpfile)))
82 (when (and file-notify--test-tmpfile1
83 (file-exists-p file-notify--test-tmpfile1))
84 (if (file-directory-p file-notify--test-tmpfile1)
85 (delete-directory file-notify--test-tmpfile1 'recursive)
86 (delete-file file-notify--test-tmpfile1)))
87 (when (file-remote-p temporary-file-directory)
88 (tramp-cleanup-connection
89 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password))
91 (setq file-notify--test-tmpfile nil
92 file-notify--test-tmpfile1 nil
93 file-notify--test-desc nil
94 file-notify--test-results nil
95 file-notify--test-events nil)
96 (when file-notify--test-event
97 (error "file-notify--test-event should not be set but bound dynamically")))
99 (setq password-cache-expiry nil
101 tramp-message-show-message nil)
103 ;; This shall happen on hydra only.
104 (when (getenv "NIX_STORE")
105 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
107 ;; We do not want to try and fail `file-notify-add-watch'.
108 (defun file-notify--test-local-enabled ()
109 "Whether local file notification is enabled.
110 This is needed for local `temporary-file-directory' only, in the
111 remote case we return always t."
112 (or file-notify--library
113 (file-remote-p temporary-file-directory)))
115 (defvar file-notify--test-remote-enabled-checked nil
116 "Cached result of `file-notify--test-remote-enabled'.
117 If the function did run, the value is a cons cell, the `cdr'
120 (defun file-notify--test-remote-enabled ()
121 "Whether remote file notification is enabled."
122 (unless (consp file-notify--test-remote-enabled-checked)
126 (file-remote-p file-notify-test-remote-temporary-file-directory)
127 (file-directory-p file-notify-test-remote-temporary-file-directory)
128 (file-writable-p file-notify-test-remote-temporary-file-directory)
130 (file-notify-add-watch
131 file-notify-test-remote-temporary-file-directory
132 '(change) 'ignore))))
133 (setq file-notify--test-remote-enabled-checked (cons t desc))
134 (when desc (file-notify-rm-watch desc))))
136 (cdr file-notify--test-remote-enabled-checked))
138 (defun file-notify--test-library ()
139 "The used library for the test, as a string.
140 In the remote case, it is the process name which runs on the
141 remote host, or nil."
142 (if (null (file-remote-p temporary-file-directory))
143 (symbol-name file-notify--library)
144 (and (consp file-notify--test-remote-enabled-checked)
145 (processp (cdr file-notify--test-remote-enabled-checked))
146 (replace-regexp-in-string
147 "<[[:digit:]]+>\\'" ""
148 (process-name (cdr file-notify--test-remote-enabled-checked))))))
150 (defmacro file-notify--deftest-remote (test docstring)
151 "Define ert `TEST-remote' for remote files."
153 `(ert-deftest ,(intern (concat (symbol-name test) "-remote")) ()
155 (let* ((temporary-file-directory
156 file-notify-test-remote-temporary-file-directory)
157 (ert-test (ert-get-test ',test)))
158 (skip-unless (file-notify--test-remote-enabled))
159 (tramp-cleanup-connection
160 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password)
161 (funcall (ert-test-body ert-test)))))
163 (ert-deftest file-notify-test00-availability ()
164 "Test availability of `file-notify'."
165 (skip-unless (file-notify--test-local-enabled))
166 ;; Report the native library which has been used.
167 (message "Library: `%s'" (file-notify--test-library))
169 (setq file-notify--test-desc
170 (file-notify-add-watch temporary-file-directory '(change) 'ignore)))
173 (file-notify--test-cleanup))
175 (file-notify--deftest-remote file-notify-test00-availability
176 "Test availability of `file-notify' for remote files.")
178 (ert-deftest file-notify-test01-add-watch ()
179 "Check `file-notify-add-watch'."
180 (skip-unless (file-notify--test-local-enabled))
182 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
183 file-notify--test-tmpfile1
184 (format "%s/%s" file-notify--test-tmpfile (md5 (current-time-string))))
186 ;; Check, that different valid parameters are accepted.
188 (setq file-notify--test-desc
189 (file-notify-add-watch temporary-file-directory '(change) 'ignore)))
190 (file-notify-rm-watch file-notify--test-desc)
192 (setq file-notify--test-desc
193 (file-notify-add-watch
194 temporary-file-directory '(attribute-change) 'ignore)))
195 (file-notify-rm-watch file-notify--test-desc)
197 (setq file-notify--test-desc
198 (file-notify-add-watch
199 temporary-file-directory '(change attribute-change) 'ignore)))
200 (file-notify-rm-watch file-notify--test-desc)
201 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
203 (setq file-notify--test-desc
204 (file-notify-add-watch
205 file-notify--test-tmpfile '(change attribute-change) 'ignore)))
206 (file-notify-rm-watch file-notify--test-desc)
207 (delete-file file-notify--test-tmpfile)
209 ;; Check error handling.
210 (should-error (file-notify-add-watch 1 2 3 4)
211 :type 'wrong-number-of-arguments)
214 (file-notify-add-watch 1 2 3))
215 '(wrong-type-argument 1)))
218 (file-notify-add-watch temporary-file-directory 2 3))
219 '(wrong-type-argument 2)))
222 (file-notify-add-watch temporary-file-directory '(change) 3))
223 '(wrong-type-argument 3)))
224 ;; The upper directory of a file must exist.
227 (file-notify-add-watch
228 file-notify--test-tmpfile1 '(change attribute-change) 'ignore))
230 "Directory does not exist" ,file-notify--test-tmpfile)))
233 (file-notify--test-cleanup))
235 (file-notify--deftest-remote file-notify-test01-add-watch
236 "Check `file-notify-add-watch' for remote files.")
238 (defun file-notify--test-event-test ()
239 "Ert test function to be called by `file-notify--test-event-handler'.
240 We cannot pass arguments, so we assume that `file-notify--test-event'
242 ;; Check the descriptor.
243 (should (equal (car file-notify--test-event) file-notify--test-desc))
244 ;; Check the file name.
246 (or (string-equal (file-notify--event-file-name file-notify--test-event)
247 file-notify--test-tmpfile)
248 (string-equal (file-notify--event-file-name file-notify--test-event)
249 file-notify--test-tmpfile1)
250 (string-equal (file-notify--event-file-name file-notify--test-event)
251 temporary-file-directory)))
252 ;; Check the second file name if exists.
253 (when (eq (nth 1 file-notify--test-event) 'renamed)
255 (or (string-equal (file-notify--event-file1-name file-notify--test-event)
256 file-notify--test-tmpfile1)
257 (string-equal (file-notify--event-file1-name file-notify--test-event)
258 temporary-file-directory)))))
260 (defun file-notify--test-event-handler (event)
261 "Run a test over FILE-NOTIFY--TEST-EVENT.
262 For later analysis, append the test result to `file-notify--test-results'
263 and the event to `file-notify--test-events'."
264 (let* ((file-notify--test-event event)
266 (ert-run-test (make-ert-test :body 'file-notify--test-event-test))))
267 ;; Do not add lock files, this would confuse the checks.
268 (unless (string-match
270 (file-notify--event-file-name file-notify--test-event))
271 ;;(message "file-notify--test-event-handler %S" file-notify--test-event)
272 (setq file-notify--test-events
273 (append file-notify--test-events `(,file-notify--test-event))
274 file-notify--test-results
275 (append file-notify--test-results `(,result))))))
277 (defun file-notify--test-make-temp-name ()
278 "Create a temporary file name for test."
280 (make-temp-name "file-notify-test") temporary-file-directory))
282 (defmacro file-notify--wait-for-events (timeout until)
283 "Wait for and return file notification events until form UNTIL is true.
284 TIMEOUT is the maximum time to wait for, in seconds."
285 `(with-timeout (,timeout (ignore))
287 (read-event nil nil 0.1))))
289 (defmacro file-notify--test-with-events (events &rest body)
290 "Run BODY collecting events and then compare with EVENTS.
291 EVENTS is either a simple list of events, or a list of lists of
292 events, which represent different possible results. Don't wait
293 longer than timeout seconds for the events to be delivered."
295 (let ((outer (make-symbol "outer")))
296 `(let* ((,outer file-notify--test-events)
297 (events (if (consp (car ,events)) ,events (list ,events)))
298 (max-length (apply 'max (mapcar 'length events)))
299 create-lockfiles result)
300 ;; Flush pending events.
301 (file-notify--wait-for-events
302 (file-notify--test-timeout)
304 (let (file-notify--test-events)
306 (file-notify--wait-for-events
307 ;; More events need more time. Use some fudge factor.
308 (* (ceiling max-length 100) (file-notify--test-timeout))
309 (= max-length (length file-notify--test-events)))
310 ;; One of the possible results shall match.
312 (dolist (elt events result)
315 (equal elt (mapcar #'cadr file-notify--test-events))))))
316 (setq ,outer (append ,outer file-notify--test-events)))
317 (setq file-notify--test-events ,outer))))
319 (ert-deftest file-notify-test02-events ()
320 "Check file creation/change/removal notifications."
321 (skip-unless (file-notify--test-local-enabled))
325 ;; Check file creation, change and deletion. It doesn't work
326 ;; for cygwin and kqueue, because we don't use an implicit
327 ;; directory monitor (kqueue), or the timings are too bad (cygwin).
328 (unless (or (eq system-type 'cygwin)
329 (string-equal (file-notify--test-library) "kqueue"))
330 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
332 (setq file-notify--test-desc
333 (file-notify-add-watch
334 file-notify--test-tmpfile
335 '(change) 'file-notify--test-event-handler)))
336 (file-notify--test-with-events
338 ;; cygwin recognizes only `deleted' and `stopped' events.
339 ((eq system-type 'cygwin)
341 (t '(created changed deleted stopped)))
343 "another text" nil file-notify--test-tmpfile nil 'no-message)
344 (read-event nil nil 0.1)
345 (delete-file file-notify--test-tmpfile))
346 ;; `file-notify-rm-watch' fires the `stopped' event. Suppress it.
347 (let (file-notify--test-events)
348 (file-notify-rm-watch file-notify--test-desc)))
350 ;; Check file change and deletion.
351 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
352 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
354 (setq file-notify--test-desc
355 (file-notify-add-watch
356 file-notify--test-tmpfile
357 '(change) 'file-notify--test-event-handler)))
358 (file-notify--test-with-events
360 ;; cygwin recognizes only `deleted' and `stopped' events.
361 ((eq system-type 'cygwin)
363 ;; inotify and kqueue raise just one `changed' event.
364 ((or (string-equal "inotify" (file-notify--test-library))
365 (string-equal "kqueue" (file-notify--test-library)))
366 '(changed deleted stopped))
367 ;; gfilenotify raises one or two `changed' events
368 ;; randomly, no chance to test. So we accept both cases.
369 ((string-equal "gfilenotify" (file-notify--test-library))
370 '((changed deleted stopped)
371 (changed changed deleted stopped)))
372 (t '(changed changed deleted stopped)))
373 (read-event nil nil 0.1)
375 "another text" nil file-notify--test-tmpfile nil 'no-message)
376 (read-event nil nil 0.1)
377 (delete-file file-notify--test-tmpfile))
378 ;; `file-notify-rm-watch' fires the `stopped' event. Suppress it.
379 (let (file-notify--test-events)
380 (file-notify-rm-watch file-notify--test-desc))
382 ;; Check file creation, change and deletion when watching a
383 ;; directory. There must be a `stopped' event when deleting
385 (let ((temporary-file-directory
386 (make-temp-file "file-notify-test-parent" t)))
388 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
389 file-notify--test-desc
390 (file-notify-add-watch
391 temporary-file-directory
392 '(change) 'file-notify--test-event-handler)))
393 (file-notify--test-with-events
395 ;; w32notify does raise a `stopped' event when a
396 ;; watched directory is deleted.
397 ((string-equal (file-notify--test-library) "w32notify")
398 '(created changed deleted))
399 ;; cygwin recognizes only `deleted' and `stopped' events.
400 ((eq system-type 'cygwin)
402 ;; There are two `deleted' events, for the file and for
403 ;; the directory. Except for kqueue.
404 ((string-equal (file-notify--test-library) "kqueue")
405 '(created changed deleted stopped))
406 (t '(created changed deleted deleted stopped)))
407 (read-event nil nil 0.1)
409 "any text" nil file-notify--test-tmpfile nil 'no-message)
410 (read-event nil nil 0.1)
411 (delete-directory temporary-file-directory 'recursive))
412 ;; `file-notify-rm-watch' fires the `stopped' event. Suppress it.
413 (let (file-notify--test-events)
414 (file-notify-rm-watch file-notify--test-desc)))
416 ;; Check copy of files inside a directory.
417 (let ((temporary-file-directory
418 (make-temp-file "file-notify-test-parent" t)))
420 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
421 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
422 file-notify--test-desc
423 (file-notify-add-watch
424 temporary-file-directory
425 '(change) 'file-notify--test-event-handler)))
426 (file-notify--test-with-events
428 ;; w32notify does not distinguish between `changed' and
429 ;; `attribute-changed'.
430 ((string-equal (file-notify--test-library) "w32notify")
431 '(created changed created changed changed changed changed
433 ;; cygwin recognizes only `deleted' and `stopped' events.
434 ((eq system-type 'cygwin)
436 ;; There are three `deleted' events, for two files and
437 ;; for the directory. Except for kqueue.
438 ((string-equal (file-notify--test-library) "kqueue")
439 '(created changed created changed deleted stopped))
440 (t '(created changed created changed
441 deleted deleted deleted stopped)))
442 (read-event nil nil 0.1)
444 "any text" nil file-notify--test-tmpfile nil 'no-message)
445 (read-event nil nil 0.1)
446 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1)
447 ;; The next two events shall not be visible.
448 (read-event nil nil 0.1)
449 (set-file-modes file-notify--test-tmpfile 000)
450 (read-event nil nil 0.1)
451 (set-file-times file-notify--test-tmpfile '(0 0))
452 (read-event nil nil 0.1)
453 (delete-directory temporary-file-directory 'recursive))
454 ;; `file-notify-rm-watch' fires the `stopped' event. Suppress it.
455 (let (file-notify--test-events)
456 (file-notify-rm-watch file-notify--test-desc)))
458 ;; Check rename of files inside a directory.
459 (let ((temporary-file-directory
460 (make-temp-file "file-notify-test-parent" t)))
462 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
463 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
464 file-notify--test-desc
465 (file-notify-add-watch
466 temporary-file-directory
467 '(change) 'file-notify--test-event-handler)))
468 (file-notify--test-with-events
470 ;; w32notify does not distinguish between `changed' and
471 ;; `attribute-changed'.
472 ((string-equal (file-notify--test-library) "w32notify")
473 '(created changed renamed deleted))
474 ;; cygwin recognizes only `deleted' and `stopped' events.
475 ((eq system-type 'cygwin)
477 ;; There are two `deleted' events, for the file and for
478 ;; the directory. Except for kqueue.
479 ((string-equal (file-notify--test-library) "kqueue")
480 '(created changed renamed deleted stopped))
481 (t '(created changed renamed deleted deleted stopped)))
482 (read-event nil nil 0.1)
484 "any text" nil file-notify--test-tmpfile nil 'no-message)
485 (read-event nil nil 0.1)
486 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1)
487 ;; After the rename, we won't get events anymore.
488 (read-event nil nil 0.1)
489 (delete-directory temporary-file-directory 'recursive))
490 ;; `file-notify-rm-watch' fires the `stopped' event. Suppress it.
491 (let (file-notify--test-events)
492 (file-notify-rm-watch file-notify--test-desc)))
494 ;; Check attribute change. Does not work for cygwin.
495 (unless (eq system-type 'cygwin)
496 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
498 "any text" nil file-notify--test-tmpfile nil 'no-message)
500 (setq file-notify--test-desc
501 (file-notify-add-watch
502 file-notify--test-tmpfile
503 '(attribute-change) 'file-notify--test-event-handler)))
504 (file-notify--test-with-events
506 ;; w32notify does not distinguish between `changed' and
507 ;; `attribute-changed'.
508 ((string-equal (file-notify--test-library) "w32notify")
509 '(changed changed changed changed))
510 ;; For kqueue and in the remote case, `write-region'
511 ;; raises also an `attribute-changed' event.
512 ((or (string-equal (file-notify--test-library) "kqueue")
513 (file-remote-p temporary-file-directory))
514 '(attribute-changed attribute-changed attribute-changed))
515 (t '(attribute-changed attribute-changed)))
516 (read-event nil nil 0.1)
518 "any text" nil file-notify--test-tmpfile nil 'no-message)
519 (read-event nil nil 0.1)
520 (set-file-modes file-notify--test-tmpfile 000)
521 (read-event nil nil 0.1)
522 (set-file-times file-notify--test-tmpfile '(0 0))
523 (read-event nil nil 0.1)
524 (delete-file file-notify--test-tmpfile))
525 ;; `file-notify-rm-watch' fires the `stopped' event. Suppress it.
526 (let (file-notify--test-events)
527 (file-notify-rm-watch file-notify--test-desc)))
529 ;; Check the global sequence again just to make sure that
530 ;; `file-notify--test-events' has been set correctly.
531 (should file-notify--test-results)
532 (dolist (result file-notify--test-results)
533 (when (ert-test-failed-p result)
535 (cadr (ert-test-result-with-condition-condition result))))))
538 (file-notify--test-cleanup)))
540 (file-notify--deftest-remote file-notify-test02-events
541 "Check file creation/change/removal notifications for remote files.")
543 (require 'autorevert)
544 (setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
545 auto-revert-remote-files t
546 auto-revert-stop-on-user-input nil)
548 (ert-deftest file-notify-test03-autorevert ()
549 "Check autorevert via file notification."
550 (skip-unless (file-notify--test-local-enabled))
551 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
552 ;; file has been reverted.
553 (let ((timeout (if (file-remote-p temporary-file-directory) 60 10))
557 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
560 "any text" nil file-notify--test-tmpfile nil 'no-message)
561 (setq buf (find-file-noselect file-notify--test-tmpfile))
562 (with-current-buffer buf
563 (should (string-equal (buffer-string) "any text"))
564 ;; `buffer-stale--default-function' checks for
565 ;; `verify-visited-file-modtime'. We must ensure that it
570 ;; `auto-revert-buffers' runs every 5".
571 (with-timeout (timeout (ignore))
572 (while (null auto-revert-notify-watch-descriptor)
575 ;; Check, that file notification has been used.
576 (should auto-revert-mode)
577 (should auto-revert-use-notify)
578 (should auto-revert-notify-watch-descriptor)
580 ;; Modify file. We wait for a second, in order to have
581 ;; another timestamp.
582 (with-current-buffer (get-buffer-create "*Messages*")
583 (narrow-to-region (point-max) (point-max)))
586 "another text" nil file-notify--test-tmpfile nil 'no-message)
588 ;; Check, that the buffer has been reverted.
589 (with-current-buffer (get-buffer-create "*Messages*")
590 (file-notify--wait-for-events
593 (format-message "Reverting buffer `%s'." (buffer-name buf))
595 (should (string-match "another text" (buffer-string)))
597 ;; Stop file notification. Autorevert shall still work via polling.
598 ;; It doesn't work for `w32notify'.
599 (unless (string-equal (file-notify--test-library) "w32notify")
600 (file-notify-rm-watch auto-revert-notify-watch-descriptor)
601 (file-notify--wait-for-events
602 timeout (null auto-revert-use-notify))
603 (should-not auto-revert-use-notify)
604 (should-not auto-revert-notify-watch-descriptor)
606 ;; Modify file. We wait for two seconds, in order to
607 ;; have another timestamp. One second seems to be too
609 (with-current-buffer (get-buffer-create "*Messages*")
610 (narrow-to-region (point-max) (point-max)))
613 "foo bla" nil file-notify--test-tmpfile nil 'no-message)
615 ;; Check, that the buffer has been reverted.
616 (with-current-buffer (get-buffer-create "*Messages*")
617 (file-notify--wait-for-events
620 (format-message "Reverting buffer `%s'." (buffer-name buf))
622 (should (string-match "foo bla" (buffer-string))))))
625 (with-current-buffer "*Messages*" (widen))
626 (ignore-errors (kill-buffer buf))
627 (file-notify--test-cleanup))))
629 (file-notify--deftest-remote file-notify-test03-autorevert
630 "Check autorevert via file notification for remote files.")
632 (ert-deftest file-notify-test04-file-validity ()
633 "Check `file-notify-valid-p' for files."
634 (skip-unless (file-notify--test-local-enabled))
638 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
639 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
641 (setq file-notify--test-desc
642 (file-notify-add-watch
643 file-notify--test-tmpfile
644 '(change) #'file-notify--test-event-handler)))
645 (should (file-notify-valid-p file-notify--test-desc))
646 ;; After calling `file-notify-rm-watch', the descriptor is not
648 (file-notify-rm-watch file-notify--test-desc)
649 (should-not (file-notify-valid-p file-notify--test-desc))
650 (delete-file file-notify--test-tmpfile))
653 (file-notify--test-cleanup))
657 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
658 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
660 (setq file-notify--test-desc
661 (file-notify-add-watch
662 file-notify--test-tmpfile
663 '(change) #'file-notify--test-event-handler)))
664 (file-notify--test-with-events
666 ;; cygwin recognizes only `deleted' and `stopped' events.
667 ((eq system-type 'cygwin)
669 ;; inotify and kqueue raise just one `changed' event.
670 ((or (string-equal "inotify" (file-notify--test-library))
671 (string-equal "kqueue" (file-notify--test-library)))
672 '(changed deleted stopped))
673 ;; gfilenotify raises one or two `changed' events
674 ;; randomly, no chance to test. So we accept both cases.
675 ((string-equal "gfilenotify" (file-notify--test-library))
676 '((changed deleted stopped)
677 (changed changed deleted stopped)))
678 (t '(changed changed deleted stopped)))
679 (should (file-notify-valid-p file-notify--test-desc))
680 (read-event nil nil 0.1)
682 "another text" nil file-notify--test-tmpfile nil 'no-message)
683 (read-event nil nil 0.1)
684 (delete-file file-notify--test-tmpfile))
685 ;; After deleting the file, the descriptor is not valid anymore.
686 (should-not (file-notify-valid-p file-notify--test-desc))
687 (file-notify-rm-watch file-notify--test-desc))
690 (file-notify--test-cleanup))
693 ;; w32notify does not send a `stopped' event when deleting a
694 ;; directory. The test does not work, therefore.
695 (unless (string-equal (file-notify--test-library) "w32notify")
696 (let ((temporary-file-directory
697 (make-temp-file "file-notify-test-parent" t)))
699 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
700 file-notify--test-desc
701 (file-notify-add-watch
702 temporary-file-directory
703 '(change) #'file-notify--test-event-handler)))
704 (file-notify--test-with-events
706 ;; cygwin recognizes only `deleted' and `stopped' events.
707 ((eq system-type 'cygwin)
709 ;; There are two `deleted' events, for the file and for
710 ;; the directory. Except for kqueue.
711 ((string-equal (file-notify--test-library) "kqueue")
712 '(created changed deleted stopped))
713 (t '(created changed deleted deleted stopped)))
714 (should (file-notify-valid-p file-notify--test-desc))
715 (read-event nil nil 0.1)
717 "any text" nil file-notify--test-tmpfile nil 'no-message)
718 (read-event nil nil 0.1)
719 (delete-directory temporary-file-directory t))
720 ;; After deleting the parent directory, the descriptor must
721 ;; not be valid anymore.
722 (should-not (file-notify-valid-p file-notify--test-desc))))
725 (file-notify--test-cleanup)))
727 (file-notify--deftest-remote file-notify-test04-file-validity
728 "Check `file-notify-valid-p' via file notification for remote files.")
730 (ert-deftest file-notify-test05-dir-validity ()
731 "Check `file-notify-valid-p' for directories."
732 (skip-unless (file-notify--test-local-enabled))
736 (setq file-notify--test-tmpfile
737 (file-name-as-directory (file-notify--test-make-temp-name)))
738 (make-directory file-notify--test-tmpfile)
740 (setq file-notify--test-desc
741 (file-notify-add-watch
742 file-notify--test-tmpfile
743 '(change) #'file-notify--test-event-handler)))
744 (should (file-notify-valid-p file-notify--test-desc))
745 ;; After removing the watch, the descriptor must not be valid
747 (file-notify-rm-watch file-notify--test-desc)
748 (file-notify--wait-for-events
749 (file-notify--test-timeout)
750 (not (file-notify-valid-p file-notify--test-desc)))
751 (should-not (file-notify-valid-p file-notify--test-desc)))
754 (file-notify--test-cleanup))
757 ;; The batch-mode operation of w32notify is fragile (there's no
758 ;; input threads to send the message to).
759 (unless (and noninteractive
760 (string-equal (file-notify--test-library) "w32notify"))
761 (setq file-notify--test-tmpfile
762 (file-name-as-directory (file-notify--test-make-temp-name)))
763 (make-directory file-notify--test-tmpfile)
765 (setq file-notify--test-desc
766 (file-notify-add-watch
767 file-notify--test-tmpfile
768 '(change) #'file-notify--test-event-handler)))
769 (should (file-notify-valid-p file-notify--test-desc))
770 ;; After deleting the directory, the descriptor must not be
772 (delete-directory file-notify--test-tmpfile t)
773 (file-notify--wait-for-events
774 (file-notify--test-timeout)
775 (not (file-notify-valid-p file-notify--test-desc)))
776 (should-not (file-notify-valid-p file-notify--test-desc)))
779 (file-notify--test-cleanup)))
781 (file-notify--deftest-remote file-notify-test05-dir-validity
782 "Check `file-notify-valid-p' via file notification for remote directories.")
784 (ert-deftest file-notify-test06-many-events ()
785 "Check that events are not dropped."
786 (skip-unless (file-notify--test-local-enabled))
787 ;; Under cygwin events arrive in random order. Impossible to define a test.
788 (skip-unless (not (eq system-type 'cygwin)))
790 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
791 (make-directory file-notify--test-tmpfile)
793 (setq file-notify--test-desc
794 (file-notify-add-watch
795 file-notify--test-tmpfile
796 '(change) 'file-notify--test-event-handler)))
799 source-file-list target-file-list
800 (default-directory file-notify--test-tmpfile))
802 ;; It matters which direction we rename, at least for
803 ;; kqueue. This backend parses directories in alphabetic
804 ;; order (x%d before y%d). So we rename both directions.
805 (if (zerop (mod i 2))
807 (push (expand-file-name (format "x%d" i)) source-file-list)
808 (push (expand-file-name (format "y%d" i)) target-file-list))
809 (push (expand-file-name (format "y%d" i)) source-file-list)
810 (push (expand-file-name (format "x%d" i)) target-file-list)))
811 (file-notify--test-with-events (make-list (+ n n) 'created)
812 (let ((source-file-list source-file-list)
813 (target-file-list target-file-list))
814 (while (and source-file-list target-file-list)
815 (read-event nil nil 0.1)
816 (write-region "" nil (pop source-file-list) nil 'no-message)
817 (read-event nil nil 0.1)
818 (write-region "" nil (pop target-file-list) nil 'no-message))))
819 (file-notify--test-with-events
821 ;; w32notify fires both `deleted' and `renamed' events.
822 ((string-equal (file-notify--test-library) "w32notify")
825 (setq r (append '(deleted renamed) r)))))
826 (t (make-list n 'renamed)))
827 (let ((source-file-list source-file-list)
828 (target-file-list target-file-list))
829 (while (and source-file-list target-file-list)
830 (rename-file (pop source-file-list) (pop target-file-list) t))))
831 (file-notify--test-with-events (make-list n 'deleted)
832 (dolist (file target-file-list)
833 (delete-file file))))
834 (file-notify--test-cleanup)))
836 (file-notify--deftest-remote file-notify-test06-many-events
837 "Check that events are not dropped for remote directories.")
839 (defun file-notify-test-all (&optional interactive)
840 "Run all tests for \\[file-notify]."
843 (ert-run-tests-interactively "^file-notify-")
844 (ert-run-tests-batch "^file-notify-")))
848 ;; * For w32notify, no stopped events arrive when a directory is removed.
849 ;; * Check, why cygwin recognizes only `deleted' and `stopped' events.
851 (provide 'file-notify-tests)
852 ;;; file-notify-tests.el ends here