1 ;;; file-notify-tests.el --- Tests of file notifications -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2016 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-desc1 nil)
62 (defvar file-notify--test-desc2 nil)
63 (defvar file-notify--test-results nil)
64 (defvar file-notify--test-event nil)
65 (defvar file-notify--test-events nil)
67 (defconst file-notify--test-read-event-timeout 0.02
68 "Timeout for `read-event' calls.
69 It is different for local and remote file notification libraries.")
71 (defun file-notify--test-timeout ()
72 "Timeout to wait for arriving events, in seconds."
74 ((file-remote-p temporary-file-directory) 6)
75 ((string-equal (file-notify--test-library) "w32notify") 10)
76 ((eq system-type 'cygwin) 10)
79 (defun file-notify--test-cleanup ()
80 "Cleanup after a test."
81 (file-notify-rm-watch file-notify--test-desc)
82 (file-notify-rm-watch file-notify--test-desc1)
83 (file-notify-rm-watch file-notify--test-desc2)
86 (delete-file (file-newest-backup file-notify--test-tmpfile)))
88 (if (file-directory-p file-notify--test-tmpfile)
89 (delete-directory file-notify--test-tmpfile 'recursive)
90 (delete-file file-notify--test-tmpfile)))
92 (if (file-directory-p file-notify--test-tmpfile1)
93 (delete-directory file-notify--test-tmpfile1 'recursive)
94 (delete-file file-notify--test-tmpfile1)))
96 (when (file-remote-p temporary-file-directory)
97 (tramp-cleanup-connection
98 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password)))
100 (setq file-notify--test-tmpfile nil
101 file-notify--test-tmpfile1 nil
102 file-notify--test-desc nil
103 file-notify--test-desc1 nil
104 file-notify--test-desc2 nil
105 file-notify--test-results nil
106 file-notify--test-events nil)
107 (when file-notify--test-event
108 (error "file-notify--test-event should not be set but bound dynamically")))
110 (setq password-cache-expiry nil
112 tramp-message-show-message nil)
114 ;; This shall happen on hydra only.
115 (when (getenv "NIX_STORE")
116 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
118 ;; We do not want to try and fail `file-notify-add-watch'.
119 (defun file-notify--test-local-enabled ()
120 "Whether local file notification is enabled.
121 This is needed for local `temporary-file-directory' only, in the
122 remote case we return always t."
123 (or file-notify--library
124 (file-remote-p temporary-file-directory)))
126 (defvar file-notify--test-remote-enabled-checked nil
127 "Cached result of `file-notify--test-remote-enabled'.
128 If the function did run, the value is a cons cell, the `cdr'
131 (defun file-notify--test-remote-enabled ()
132 "Whether remote file notification is enabled."
133 (unless (consp file-notify--test-remote-enabled-checked)
137 (file-remote-p file-notify-test-remote-temporary-file-directory)
138 (file-directory-p file-notify-test-remote-temporary-file-directory)
139 (file-writable-p file-notify-test-remote-temporary-file-directory)
141 (file-notify-add-watch
142 file-notify-test-remote-temporary-file-directory
143 '(change) 'ignore))))
144 (setq file-notify--test-remote-enabled-checked (cons t desc))
145 (when desc (file-notify-rm-watch desc))))
147 (cdr file-notify--test-remote-enabled-checked))
149 (defun file-notify--test-library ()
150 "The used library for the test, as a string.
151 In the remote case, it is the process name which runs on the
152 remote host, or nil."
153 (if (null (file-remote-p temporary-file-directory))
154 (symbol-name file-notify--library)
155 (and (consp file-notify--test-remote-enabled-checked)
156 (processp (cdr file-notify--test-remote-enabled-checked))
157 (replace-regexp-in-string
158 "<[[:digit:]]+>\\'" ""
159 (process-name (cdr file-notify--test-remote-enabled-checked))))))
161 (defmacro file-notify--deftest-remote (test docstring)
162 "Define ert `TEST-remote' for remote files."
164 `(ert-deftest ,(intern (concat (symbol-name test) "-remote")) ()
166 :tags '(:expensive-test)
167 (let* ((temporary-file-directory
168 file-notify-test-remote-temporary-file-directory)
169 (file-notify--test-read-event-timeout 0.1)
170 (ert-test (ert-get-test ',test)))
171 (skip-unless (file-notify--test-remote-enabled))
172 (tramp-cleanup-connection
173 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password)
174 (funcall (ert-test-body ert-test)))))
176 (ert-deftest file-notify-test00-availability ()
177 "Test availability of `file-notify'."
178 (skip-unless (file-notify--test-local-enabled))
179 ;; Report the native library which has been used.
180 (message "Library: `%s'" (file-notify--test-library))
182 (setq file-notify--test-desc
183 (file-notify-add-watch temporary-file-directory '(change) 'ignore)))
186 (file-notify--test-cleanup))
188 (file-notify--deftest-remote file-notify-test00-availability
189 "Test availability of `file-notify' for remote files.")
191 (ert-deftest file-notify-test01-add-watch ()
192 "Check `file-notify-add-watch'."
193 (skip-unless (file-notify--test-local-enabled))
195 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
196 file-notify--test-tmpfile1
197 (format "%s/%s" file-notify--test-tmpfile (md5 (current-time-string))))
199 ;; Check, that different valid parameters are accepted.
201 (setq file-notify--test-desc
202 (file-notify-add-watch temporary-file-directory '(change) 'ignore)))
203 (file-notify-rm-watch file-notify--test-desc)
205 (setq file-notify--test-desc
206 (file-notify-add-watch
207 temporary-file-directory '(attribute-change) 'ignore)))
208 (file-notify-rm-watch file-notify--test-desc)
210 (setq file-notify--test-desc
211 (file-notify-add-watch
212 temporary-file-directory '(change attribute-change) 'ignore)))
213 (file-notify-rm-watch file-notify--test-desc)
214 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
216 (setq file-notify--test-desc
217 (file-notify-add-watch
218 file-notify--test-tmpfile '(change attribute-change) 'ignore)))
219 (file-notify-rm-watch file-notify--test-desc)
220 (delete-file file-notify--test-tmpfile)
222 ;; Check error handling.
223 (should-error (file-notify-add-watch 1 2 3 4)
224 :type 'wrong-number-of-arguments)
227 (file-notify-add-watch 1 2 3))
228 '(wrong-type-argument 1)))
231 (file-notify-add-watch temporary-file-directory 2 3))
232 '(wrong-type-argument 2)))
235 (file-notify-add-watch temporary-file-directory '(change) 3))
236 '(wrong-type-argument 3)))
237 ;; The upper directory of a file must exist.
240 (file-notify-add-watch
241 file-notify--test-tmpfile1 '(change attribute-change) 'ignore))
243 "Directory does not exist" ,file-notify--test-tmpfile)))
246 (file-notify--test-cleanup))
248 (file-notify--deftest-remote file-notify-test01-add-watch
249 "Check `file-notify-add-watch' for remote files.")
251 (defun file-notify--test-event-test ()
252 "Ert test function to be called by `file-notify--test-event-handler'.
253 We cannot pass arguments, so we assume that `file-notify--test-event'
255 ;; Check the descriptor.
256 (should (equal (car file-notify--test-event) file-notify--test-desc))
257 ;; Check the file name.
260 (file-notify--event-watched-file file-notify--test-event)
261 (file-notify--event-file-name file-notify--test-event)))
262 ;; Check the second file name if exists.
263 (when (eq (nth 1 file-notify--test-event) 'renamed)
266 (file-notify--event-watched-file file-notify--test-event)
267 (file-notify--event-file1-name file-notify--test-event)))))
269 (defun file-notify--test-event-handler (event)
270 "Run a test over FILE-NOTIFY--TEST-EVENT.
271 For later analysis, append the test result to `file-notify--test-results'
272 and the event to `file-notify--test-events'."
273 (let* ((file-notify--test-event event)
275 (ert-run-test (make-ert-test :body 'file-notify--test-event-test))))
276 ;; Do not add lock files, this would confuse the checks.
277 (unless (string-match
279 (file-notify--event-file-name file-notify--test-event))
280 ;;(message "file-notify--test-event-handler result: %s event: %S"
281 ;;(null (ert-test-failed-p result)) file-notify--test-event)
282 (setq file-notify--test-events
283 (append file-notify--test-events `(,file-notify--test-event))
284 file-notify--test-results
285 (append file-notify--test-results `(,result))))))
287 (defun file-notify--test-make-temp-name ()
288 "Create a temporary file name for test."
290 (make-temp-name "file-notify-test") temporary-file-directory))
292 (defmacro file-notify--wait-for-events (timeout until)
293 "Wait for and return file notification events until form UNTIL is true.
294 TIMEOUT is the maximum time to wait for, in seconds."
295 `(with-timeout (,timeout (ignore))
297 (read-event nil nil file-notify--test-read-event-timeout))))
299 (defun file-notify--test-with-events-check (events)
300 "Check whether received events match one of the EVENTS alternatives."
302 (dolist (elt events result)
305 (equal elt (mapcar #'cadr file-notify--test-events)))))))
307 (defun file-notify--test-with-events-explainer (events)
308 "Explain why `file-notify--test-with-events-check' fails."
309 (if (null (cdr events))
310 (format "Received events `%s' do not match expected events `%s'"
311 (mapcar #'cadr file-notify--test-events) (car events))
313 "Received events `%s' do not match any sequence of expected events `%s'"
314 (mapcar #'cadr file-notify--test-events) events)))
316 (put 'file-notify--test-with-events-check 'ert-explainer
317 'file-notify--test-with-events-explainer)
319 (defmacro file-notify--test-with-events (events &rest body)
320 "Run BODY collecting events and then compare with EVENTS.
321 EVENTS is either a simple list of events, or a list of lists of
322 events, which represent different possible results. Don't wait
323 longer than timeout seconds for the events to be delivered."
325 `(let* ((events (if (consp (car ,events)) ,events (list ,events)))
326 (max-length (apply 'max (mapcar 'length events)))
328 ;; Flush pending events.
329 (file-notify--wait-for-events
330 (file-notify--test-timeout)
332 (setq file-notify--test-events nil
333 file-notify--test-results nil)
335 (file-notify--wait-for-events
336 ;; More events need more time. Use some fudge factor.
337 (* (ceiling max-length 100) (file-notify--test-timeout))
338 (= max-length (length file-notify--test-events)))
339 ;; Check the result sequence just to make sure that all events
341 (dolist (result file-notify--test-results)
342 (when (ert-test-failed-p result)
344 (cadr (ert-test-result-with-condition-condition result)))))
345 ;; One of the possible event sequences shall match.
346 (should (file-notify--test-with-events-check events))))
348 (ert-deftest file-notify-test02-events ()
349 "Check file creation/change/removal notifications."
350 (skip-unless (file-notify--test-local-enabled))
354 ;; Check file creation, change and deletion. It doesn't work
355 ;; for cygwin and kqueue, because we don't use an implicit
356 ;; directory monitor (kqueue), or the timings are too bad (cygwin).
357 (unless (or (eq system-type 'cygwin)
358 (string-equal (file-notify--test-library) "kqueue"))
359 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
361 (setq file-notify--test-desc
362 (file-notify-add-watch
363 file-notify--test-tmpfile
364 '(change) 'file-notify--test-event-handler)))
365 (file-notify--test-with-events
367 ;; cygwin recognizes only `deleted' and `stopped' events.
368 ((eq system-type 'cygwin)
370 (t '(created changed deleted stopped)))
372 "another text" nil file-notify--test-tmpfile nil 'no-message)
373 (read-event nil nil file-notify--test-read-event-timeout)
374 (delete-file file-notify--test-tmpfile))
375 (file-notify-rm-watch file-notify--test-desc))
377 ;; Check file change and deletion.
378 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
379 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
381 (setq file-notify--test-desc
382 (file-notify-add-watch
383 file-notify--test-tmpfile
384 '(change) 'file-notify--test-event-handler)))
385 (file-notify--test-with-events
387 ;; cygwin recognizes only `deleted' and `stopped' events.
388 ((eq system-type 'cygwin)
390 ;; inotify and kqueue raise just one `changed' event.
391 ((or (string-equal "inotify" (file-notify--test-library))
392 (string-equal "kqueue" (file-notify--test-library)))
393 '(changed deleted stopped))
394 ;; gfilenotify raises one or two `changed' events
395 ;; randomly, no chance to test. So we accept both cases.
396 ((string-equal "gfilenotify" (file-notify--test-library))
397 '((changed deleted stopped)
398 (changed changed deleted stopped)))
399 (t '(changed changed deleted stopped)))
400 (read-event nil nil file-notify--test-read-event-timeout)
402 "another text" nil file-notify--test-tmpfile nil 'no-message)
403 (read-event nil nil file-notify--test-read-event-timeout)
404 (delete-file file-notify--test-tmpfile))
405 (file-notify-rm-watch file-notify--test-desc)
407 ;; Check file creation, change and deletion when watching a
408 ;; directory. There must be a `stopped' event when deleting
410 (let ((temporary-file-directory
411 (make-temp-file "file-notify-test-parent" t)))
413 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
414 file-notify--test-desc
415 (file-notify-add-watch
416 temporary-file-directory
417 '(change) 'file-notify--test-event-handler)))
418 (file-notify--test-with-events
420 ;; w32notify does raise a `stopped' event when a
421 ;; watched directory is deleted.
422 ((string-equal (file-notify--test-library) "w32notify")
423 '(created changed deleted))
424 ;; cygwin recognizes only `deleted' and `stopped' events.
425 ((eq system-type 'cygwin)
427 ;; There are two `deleted' events, for the file and for
428 ;; the directory. Except for kqueue.
429 ((string-equal (file-notify--test-library) "kqueue")
430 '(created changed deleted stopped))
431 (t '(created changed deleted deleted stopped)))
432 (read-event nil nil file-notify--test-read-event-timeout)
434 "any text" nil file-notify--test-tmpfile nil 'no-message)
435 (read-event nil nil file-notify--test-read-event-timeout)
436 (delete-directory temporary-file-directory 'recursive))
437 (file-notify-rm-watch file-notify--test-desc))
439 ;; Check copy of files inside a directory.
440 (let ((temporary-file-directory
441 (make-temp-file "file-notify-test-parent" t)))
443 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
444 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
445 file-notify--test-desc
446 (file-notify-add-watch
447 temporary-file-directory
448 '(change) 'file-notify--test-event-handler)))
449 (file-notify--test-with-events
451 ;; w32notify does not distinguish between `changed' and
452 ;; `attribute-changed'.
453 ((string-equal (file-notify--test-library) "w32notify")
454 '(created changed created changed changed changed changed
456 ;; cygwin recognizes only `deleted' and `stopped' events.
457 ((eq system-type 'cygwin)
459 ;; There are three `deleted' events, for two files and
460 ;; for the directory. Except for kqueue.
461 ((string-equal (file-notify--test-library) "kqueue")
462 '(created changed created changed deleted stopped))
463 (t '(created changed created changed
464 deleted deleted deleted stopped)))
465 (read-event nil nil file-notify--test-read-event-timeout)
467 "any text" nil file-notify--test-tmpfile nil 'no-message)
468 (read-event nil nil file-notify--test-read-event-timeout)
469 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1)
470 ;; The next two events shall not be visible.
471 (read-event nil nil file-notify--test-read-event-timeout)
472 (set-file-modes file-notify--test-tmpfile 000)
473 (read-event nil nil file-notify--test-read-event-timeout)
474 (set-file-times file-notify--test-tmpfile '(0 0))
475 (read-event nil nil file-notify--test-read-event-timeout)
476 (delete-directory temporary-file-directory 'recursive))
477 (file-notify-rm-watch file-notify--test-desc))
479 ;; Check rename of files inside a directory.
480 (let ((temporary-file-directory
481 (make-temp-file "file-notify-test-parent" t)))
483 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
484 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
485 file-notify--test-desc
486 (file-notify-add-watch
487 temporary-file-directory
488 '(change) 'file-notify--test-event-handler)))
489 (file-notify--test-with-events
491 ;; w32notify does not distinguish between `changed' and
492 ;; `attribute-changed'.
493 ((string-equal (file-notify--test-library) "w32notify")
494 '(created changed renamed deleted))
495 ;; cygwin recognizes only `deleted' and `stopped' events.
496 ((eq system-type 'cygwin)
498 ;; There are two `deleted' events, for the file and for
499 ;; the directory. Except for kqueue.
500 ((string-equal (file-notify--test-library) "kqueue")
501 '(created changed renamed deleted stopped))
502 (t '(created changed renamed deleted deleted stopped)))
503 (read-event nil nil file-notify--test-read-event-timeout)
505 "any text" nil file-notify--test-tmpfile nil 'no-message)
506 (read-event nil nil file-notify--test-read-event-timeout)
507 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1)
508 ;; After the rename, we won't get events anymore.
509 (read-event nil nil file-notify--test-read-event-timeout)
510 (delete-directory temporary-file-directory 'recursive))
511 (file-notify-rm-watch file-notify--test-desc))
513 ;; Check attribute change. Does not work for cygwin.
514 (unless (eq system-type 'cygwin)
515 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
517 "any text" nil file-notify--test-tmpfile nil 'no-message)
519 (setq file-notify--test-desc
520 (file-notify-add-watch
521 file-notify--test-tmpfile
522 '(attribute-change) 'file-notify--test-event-handler)))
523 (file-notify--test-with-events
525 ;; w32notify does not distinguish between `changed' and
526 ;; `attribute-changed'.
527 ((string-equal (file-notify--test-library) "w32notify")
528 '(changed changed changed changed))
529 ;; For kqueue and in the remote case, `write-region'
530 ;; raises also an `attribute-changed' event.
531 ((or (string-equal (file-notify--test-library) "kqueue")
532 (file-remote-p temporary-file-directory))
533 '(attribute-changed attribute-changed attribute-changed))
534 (t '(attribute-changed attribute-changed)))
535 (read-event nil nil file-notify--test-read-event-timeout)
537 "any text" nil file-notify--test-tmpfile nil 'no-message)
538 (read-event nil nil file-notify--test-read-event-timeout)
539 (set-file-modes file-notify--test-tmpfile 000)
540 (read-event nil nil file-notify--test-read-event-timeout)
541 (set-file-times file-notify--test-tmpfile '(0 0))
542 (read-event nil nil file-notify--test-read-event-timeout)
543 (delete-file file-notify--test-tmpfile))
544 (file-notify-rm-watch file-notify--test-desc)))
547 (file-notify--test-cleanup)))
549 (file-notify--deftest-remote file-notify-test02-events
550 "Check file creation/change/removal notifications for remote files.")
552 (require 'autorevert)
553 (setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
554 auto-revert-remote-files t
555 auto-revert-stop-on-user-input nil)
557 (ert-deftest file-notify-test03-autorevert ()
558 "Check autorevert via file notification."
559 (skip-unless (file-notify--test-local-enabled))
560 ;; `auto-revert-buffers' runs every 5". And we must wait, until the
561 ;; file has been reverted.
562 (let ((timeout (if (file-remote-p temporary-file-directory) 60 10))
566 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
569 "any text" nil file-notify--test-tmpfile nil 'no-message)
570 (setq buf (find-file-noselect file-notify--test-tmpfile))
571 (with-current-buffer buf
572 (should (string-equal (buffer-string) "any text"))
573 ;; `buffer-stale--default-function' checks for
574 ;; `verify-visited-file-modtime'. We must ensure that it
579 ;; `auto-revert-buffers' runs every 5".
580 (with-timeout (timeout (ignore))
581 (while (null auto-revert-notify-watch-descriptor)
584 ;; Check, that file notification has been used.
585 (should auto-revert-mode)
586 (should auto-revert-use-notify)
587 (should auto-revert-notify-watch-descriptor)
589 ;; Modify file. We wait for a second, in order to have
590 ;; another timestamp.
591 (with-current-buffer (get-buffer-create "*Messages*")
592 (narrow-to-region (point-max) (point-max)))
595 "another text" nil file-notify--test-tmpfile nil 'no-message)
597 ;; Check, that the buffer has been reverted.
598 (with-current-buffer (get-buffer-create "*Messages*")
599 (file-notify--wait-for-events
602 (format-message "Reverting buffer `%s'." (buffer-name buf))
604 (should (string-match "another text" (buffer-string)))
606 ;; Stop file notification. Autorevert shall still work via polling.
607 ;; It doesn't work for w32notify.
608 (unless (string-equal (file-notify--test-library) "w32notify")
609 (file-notify-rm-watch auto-revert-notify-watch-descriptor)
610 (file-notify--wait-for-events
611 timeout (null auto-revert-use-notify))
612 (should-not auto-revert-use-notify)
613 (should-not auto-revert-notify-watch-descriptor)
615 ;; Modify file. We wait for two seconds, in order to
616 ;; have another timestamp. One second seems to be too
618 (with-current-buffer (get-buffer-create "*Messages*")
619 (narrow-to-region (point-max) (point-max)))
622 "foo bla" nil file-notify--test-tmpfile nil 'no-message)
624 ;; Check, that the buffer has been reverted.
625 (with-current-buffer (get-buffer-create "*Messages*")
626 (file-notify--wait-for-events
629 (format-message "Reverting buffer `%s'." (buffer-name buf))
631 (should (string-match "foo bla" (buffer-string))))))
634 (with-current-buffer "*Messages*" (widen))
635 (ignore-errors (kill-buffer buf))
636 (file-notify--test-cleanup))))
638 (file-notify--deftest-remote file-notify-test03-autorevert
639 "Check autorevert via file notification for remote files.")
641 (ert-deftest file-notify-test04-file-validity ()
642 "Check `file-notify-valid-p' for files."
643 (skip-unless (file-notify--test-local-enabled))
647 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
648 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
650 (setq file-notify--test-desc
651 (file-notify-add-watch
652 file-notify--test-tmpfile
653 '(change) #'file-notify--test-event-handler)))
654 (should (file-notify-valid-p file-notify--test-desc))
655 ;; After calling `file-notify-rm-watch', the descriptor is not
657 (file-notify-rm-watch file-notify--test-desc)
658 (should-not (file-notify-valid-p file-notify--test-desc))
659 (delete-file file-notify--test-tmpfile))
662 (file-notify--test-cleanup))
666 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
667 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
669 (setq file-notify--test-desc
670 (file-notify-add-watch
671 file-notify--test-tmpfile
672 '(change) #'file-notify--test-event-handler)))
673 (file-notify--test-with-events
675 ;; cygwin recognizes only `deleted' and `stopped' events.
676 ((eq system-type 'cygwin)
678 ;; inotify and kqueue raise just one `changed' event.
679 ((or (string-equal "inotify" (file-notify--test-library))
680 (string-equal "kqueue" (file-notify--test-library)))
681 '(changed deleted stopped))
682 ;; gfilenotify raises one or two `changed' events
683 ;; randomly, no chance to test. So we accept both cases.
684 ((string-equal "gfilenotify" (file-notify--test-library))
685 '((changed deleted stopped)
686 (changed changed deleted stopped)))
687 (t '(changed changed deleted stopped)))
688 (should (file-notify-valid-p file-notify--test-desc))
689 (read-event nil nil file-notify--test-read-event-timeout)
691 "another text" nil file-notify--test-tmpfile nil 'no-message)
692 (read-event nil nil file-notify--test-read-event-timeout)
693 (delete-file file-notify--test-tmpfile))
694 ;; After deleting the file, the descriptor is not valid anymore.
695 (should-not (file-notify-valid-p file-notify--test-desc))
696 (file-notify-rm-watch file-notify--test-desc))
699 (file-notify--test-cleanup))
702 ;; w32notify does not send a `stopped' event when deleting a
703 ;; directory. The test does not work, therefore.
704 (unless (string-equal (file-notify--test-library) "w32notify")
705 (let ((temporary-file-directory
706 (make-temp-file "file-notify-test-parent" t)))
708 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
709 file-notify--test-desc
710 (file-notify-add-watch
711 temporary-file-directory
712 '(change) #'file-notify--test-event-handler)))
713 (file-notify--test-with-events
715 ;; cygwin recognizes only `deleted' and `stopped' events.
716 ((eq system-type 'cygwin)
718 ;; There are two `deleted' events, for the file and for
719 ;; the directory. Except for kqueue.
720 ((string-equal (file-notify--test-library) "kqueue")
721 '(created changed deleted stopped))
722 (t '(created changed deleted deleted stopped)))
723 (should (file-notify-valid-p file-notify--test-desc))
724 (read-event nil nil file-notify--test-read-event-timeout)
726 "any text" nil file-notify--test-tmpfile nil 'no-message)
727 (read-event nil nil file-notify--test-read-event-timeout)
728 (delete-directory temporary-file-directory t))
729 ;; After deleting the parent directory, the descriptor must
730 ;; not be valid anymore.
731 (should-not (file-notify-valid-p file-notify--test-desc))))
734 (file-notify--test-cleanup)))
736 (file-notify--deftest-remote file-notify-test04-file-validity
737 "Check `file-notify-valid-p' via file notification for remote files.")
739 (ert-deftest file-notify-test05-dir-validity ()
740 "Check `file-notify-valid-p' for directories."
741 (skip-unless (file-notify--test-local-enabled))
745 (setq file-notify--test-tmpfile
746 (file-name-as-directory (file-notify--test-make-temp-name)))
747 (make-directory file-notify--test-tmpfile)
749 (setq file-notify--test-desc
750 (file-notify-add-watch
751 file-notify--test-tmpfile
752 '(change) #'file-notify--test-event-handler)))
753 (should (file-notify-valid-p file-notify--test-desc))
754 ;; After removing the watch, the descriptor must not be valid
756 (file-notify-rm-watch file-notify--test-desc)
757 (file-notify--wait-for-events
758 (file-notify--test-timeout)
759 (not (file-notify-valid-p file-notify--test-desc)))
760 (should-not (file-notify-valid-p file-notify--test-desc)))
763 (file-notify--test-cleanup))
766 ;; The batch-mode operation of w32notify is fragile (there's no
767 ;; input threads to send the message to).
768 (unless (and noninteractive
769 (string-equal (file-notify--test-library) "w32notify"))
770 (setq file-notify--test-tmpfile
771 (file-name-as-directory (file-notify--test-make-temp-name)))
772 (make-directory file-notify--test-tmpfile)
774 (setq file-notify--test-desc
775 (file-notify-add-watch
776 file-notify--test-tmpfile
777 '(change) #'file-notify--test-event-handler)))
778 (should (file-notify-valid-p file-notify--test-desc))
779 ;; After deleting the directory, the descriptor must not be
781 (delete-directory file-notify--test-tmpfile t)
782 (file-notify--wait-for-events
783 (file-notify--test-timeout)
784 (not (file-notify-valid-p file-notify--test-desc)))
785 (should-not (file-notify-valid-p file-notify--test-desc)))
788 (file-notify--test-cleanup)))
790 (file-notify--deftest-remote file-notify-test05-dir-validity
791 "Check `file-notify-valid-p' via file notification for remote directories.")
793 (ert-deftest file-notify-test06-many-events ()
794 "Check that events are not dropped."
795 :tags '(:expensive-test)
796 (skip-unless (file-notify--test-local-enabled))
797 ;; Under cygwin events arrive in random order. Impossible to define a test.
798 (skip-unless (not (eq system-type 'cygwin)))
800 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
801 (make-directory file-notify--test-tmpfile)
803 (setq file-notify--test-desc
804 (file-notify-add-watch
805 file-notify--test-tmpfile
806 '(change) 'file-notify--test-event-handler)))
809 source-file-list target-file-list
810 (default-directory file-notify--test-tmpfile))
812 ;; It matters which direction we rename, at least for
813 ;; kqueue. This backend parses directories in alphabetic
814 ;; order (x%d before y%d). So we rename into both directions.
815 (if (zerop (mod i 2))
817 (push (expand-file-name (format "x%d" i)) source-file-list)
818 (push (expand-file-name (format "y%d" i)) target-file-list))
819 (push (expand-file-name (format "y%d" i)) source-file-list)
820 (push (expand-file-name (format "x%d" i)) target-file-list)))
821 (file-notify--test-with-events (make-list (+ n n) 'created)
822 (let ((source-file-list source-file-list)
823 (target-file-list target-file-list))
824 (while (and source-file-list target-file-list)
825 (read-event nil nil file-notify--test-read-event-timeout)
826 (write-region "" nil (pop source-file-list) nil 'no-message)
827 (read-event nil nil file-notify--test-read-event-timeout)
828 (write-region "" nil (pop target-file-list) nil 'no-message))))
829 (file-notify--test-with-events
831 ;; w32notify fires both `deleted' and `renamed' events.
832 ((string-equal (file-notify--test-library) "w32notify")
835 (setq r (append '(deleted renamed) r)))))
836 (t (make-list n 'renamed)))
837 (let ((source-file-list source-file-list)
838 (target-file-list target-file-list))
839 (while (and source-file-list target-file-list)
840 (read-event nil nil file-notify--test-read-event-timeout)
841 (rename-file (pop source-file-list) (pop target-file-list) t))))
842 (file-notify--test-with-events (make-list n 'deleted)
843 (dolist (file target-file-list)
844 (read-event nil nil file-notify--test-read-event-timeout)
845 (delete-file file) file-notify--test-read-event-timeout)))
848 (file-notify--test-cleanup)))
850 (file-notify--deftest-remote file-notify-test06-many-events
851 "Check that events are not dropped for remote directories.")
853 (ert-deftest file-notify-test07-backup ()
854 "Check that backup keeps file notification."
855 (skip-unless (file-notify--test-local-enabled))
859 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
860 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
862 (setq file-notify--test-desc
863 (file-notify-add-watch
864 file-notify--test-tmpfile
865 '(change) #'file-notify--test-event-handler)))
866 (should (file-notify-valid-p file-notify--test-desc))
867 (file-notify--test-with-events
869 ;; For w32notify and in the remote case, there are two
871 ((or (string-equal (file-notify--test-library) "w32notify")
872 (file-remote-p temporary-file-directory))
874 ;; gfilenotify raises one or two `changed' events
875 ;; randomly, no chance to test. So we accept both cases.
876 ((string-equal "gfilenotify" (file-notify--test-library))
880 ;; There shouldn't be any problem, because the file is kept.
882 (let ((buffer-file-name file-notify--test-tmpfile)
883 (make-backup-files t)
884 (backup-by-copying t)
885 (kept-new-versions 1)
886 (delete-old-versions t))
887 (insert "another text")
889 ;; After saving the buffer, the descriptor is still valid.
890 (should (file-notify-valid-p file-notify--test-desc))
891 (delete-file file-notify--test-tmpfile))
894 (file-notify--test-cleanup))
898 ;; It doesn't work for kqueue, because we don't use an
899 ;; implicit directory monitor.
900 (unless (string-equal (file-notify--test-library) "kqueue")
901 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
903 "any text" nil file-notify--test-tmpfile nil 'no-message)
905 (setq file-notify--test-desc
906 (file-notify-add-watch
907 file-notify--test-tmpfile
908 '(change) #'file-notify--test-event-handler)))
909 (should (file-notify-valid-p file-notify--test-desc))
910 (file-notify--test-with-events '(renamed created changed)
911 ;; The file is renamed when creating a backup. It shall
914 (let ((buffer-file-name file-notify--test-tmpfile)
915 (make-backup-files t)
916 (backup-by-copying nil)
917 (backup-by-copying-when-mismatch nil)
918 (kept-new-versions 1)
919 (delete-old-versions t))
920 (insert "another text")
922 ;; After saving the buffer, the descriptor is still valid.
923 (should (file-notify-valid-p file-notify--test-desc))
924 (delete-file file-notify--test-tmpfile)))
927 (file-notify--test-cleanup)))
929 (file-notify--deftest-remote file-notify-test07-backup
930 "Check that backup keeps file notification for remote files.")
932 (ert-deftest file-notify-test08-watched-file-in-watched-dir ()
933 "Watches a directory and a file in that directory separately.
934 Checks that the callbacks are only called with events with
935 descriptors that were issued when registering the watches. This
936 test caters for the situation in bug#22736 where the callback for
937 the directory received events for the file with the descriptor of
939 :tags '(:expensive-test)
940 (skip-unless (file-notify--test-local-enabled))
942 ;; A directory to be watched.
944 (setq file-notify--test-tmpfile
945 (make-temp-file "file-notify-test-parent" t)))
946 ;; A file to be watched.
948 (setq file-notify--test-tmpfile1
949 (let ((temporary-file-directory file-notify--test-tmpfile))
950 (file-notify--test-make-temp-name))))
951 (write-region "any text" nil file-notify--test-tmpfile1 nil 'no-message)
953 (cl-flet (;; Directory monitor.
954 (dir-callback (event)
955 (let ((file-notify--test-desc file-notify--test-desc1))
956 (file-notify--test-event-handler event)))
958 (file-callback (event)
959 (let ((file-notify--test-desc file-notify--test-desc2))
960 (file-notify--test-event-handler event))))
962 (setq file-notify--test-desc1
963 (file-notify-add-watch
964 file-notify--test-tmpfile
965 '(change) #'dir-callback)))
967 (setq file-notify--test-desc2
968 (file-notify-add-watch
969 file-notify--test-tmpfile1
970 '(change) #'file-callback)))
971 (should (file-notify-valid-p file-notify--test-desc1))
972 (should (file-notify-valid-p file-notify--test-desc2))
973 (should-not (equal file-notify--test-desc1 file-notify--test-desc2))
974 ;; gfilenotify raises one or two `changed' events randomly in
975 ;; the file monitor, no chance to test.
976 (unless (string-equal "gfilenotify" (file-notify--test-library))
977 (let ((n 100) events)
978 ;; Compute the expected events.
979 (dotimes (_i (/ n 2))
983 ;; Directory monitor and file monitor.
985 ;; In the remote case, there are two `changed'
987 ((file-remote-p temporary-file-directory)
988 '(changed changed changed changed))
989 ;; The directory monitor in kqueue does not
990 ;; raise any `changed' event. Just the file
991 ;; monitor event is received.
992 ((string-equal (file-notify--test-library) "kqueue")
994 ;; Otherwise, both monitors report the
996 (t '(changed changed)))
997 ;; Just the directory monitor.
999 ;; In kqueue, there is an additional `changed'
1001 ((string-equal (file-notify--test-library) "kqueue")
1002 '(changed created changed))
1003 (t '(created changed))))
1007 (file-notify--test-with-events events
1009 (read-event nil nil file-notify--test-read-event-timeout)
1010 (if (zerop (mod i 2))
1012 "any text" nil file-notify--test-tmpfile1 t 'no-message)
1013 (let ((temporary-file-directory file-notify--test-tmpfile))
1016 (file-notify--test-make-temp-name) nil 'no-message)))))))
1018 ;; If we delete the file, the directory monitor shall still be
1019 ;; active. We receive the `deleted' event from both the
1020 ;; directory and the file monitor. The `stopped' event is
1021 ;; from the file monitor. It's undecided in which order the
1022 ;; the directory and the file monitor are triggered.
1023 (file-notify--test-with-events
1024 '((deleted deleted stopped)
1025 (deleted stopped deleted))
1026 (delete-file file-notify--test-tmpfile1))
1027 (should (file-notify-valid-p file-notify--test-desc1))
1028 (should-not (file-notify-valid-p file-notify--test-desc2))
1030 ;; Now we delete the directory.
1031 (file-notify--test-with-events
1033 ;; In kqueue, just one `deleted' event for the directory
1035 ((string-equal (file-notify--test-library) "kqueue")
1038 ;; The directory monitor raises a `deleted' event for
1039 ;; every file contained in the directory, we must
1044 file-notify--test-tmpfile nil
1045 directory-files-no-dot-files-regexp 'nosort))
1047 ;; The events of the directory itself.
1048 '(deleted stopped))))
1049 (delete-directory file-notify--test-tmpfile 'recursive))
1050 (should-not (file-notify-valid-p file-notify--test-desc1))
1051 (should-not (file-notify-valid-p file-notify--test-desc2)))
1054 (file-notify--test-cleanup)))
1056 (file-notify--deftest-remote file-notify-test08-watched-file-in-watched-dir
1057 "Check `file-notify-test08-watched-file-in-watched-dir' for remote files.")
1059 (defun file-notify-test-all (&optional interactive)
1060 "Run all tests for \\[file-notify]."
1063 (ert-run-tests-interactively "^file-notify-")
1064 (ert-run-tests-batch "^file-notify-")))
1068 ;; * For w32notify, no stopped events arrive when a directory is removed.
1069 ;; * Check, why cygwin recognizes only `deleted' and `stopped' events.
1071 (provide 'file-notify-tests)
1072 ;;; file-notify-tests.el ends here