]> code.delx.au - gnu-emacs/blob - test/lisp/filenotify-tests.el
Merge from origin/emacs-25
[gnu-emacs] / test / lisp / filenotify-tests.el
1 ;;; file-notify-tests.el --- Tests of file notifications -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6
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.
11 ;;
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.
16 ;;
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/'.
19
20 ;;; Commentary:
21
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.
27
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.
33
34 ;; A whole test run can be performed calling the command `file-notify-test-all'.
35
36 ;;; Code:
37
38 (require 'ert)
39 (require 'filenotify)
40 (require 'tramp)
41
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
44 (cond
45 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
46 ((eq system-type 'windows-nt) null-device)
47 (t (add-to-list
48 'tramp-methods
49 '("mock"
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.")
57
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)
66
67 (defconst file-notify--test-read-event-timeout 0.01
68 "Timeout for `read-event' calls.
69 It is different for local and remote file notification libraries.")
70
71 (defun file-notify--test-timeout ()
72 "Timeout to wait for arriving events, in seconds."
73 (cond
74 ((file-remote-p temporary-file-directory) 6)
75 ((string-equal (file-notify--test-library) "w32notify") 4)
76 ((eq system-type 'cygwin) 10)
77 (t 3)))
78
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)
84
85 (ignore-errors
86 (delete-file (file-newest-backup file-notify--test-tmpfile)))
87 (ignore-errors
88 (if (file-directory-p file-notify--test-tmpfile)
89 (delete-directory file-notify--test-tmpfile 'recursive)
90 (delete-file file-notify--test-tmpfile)))
91 (ignore-errors
92 (if (file-directory-p file-notify--test-tmpfile1)
93 (delete-directory file-notify--test-tmpfile1 'recursive)
94 (delete-file file-notify--test-tmpfile1)))
95 (ignore-errors
96 (when (file-remote-p temporary-file-directory)
97 (tramp-cleanup-connection
98 (tramp-dissect-file-name temporary-file-directory) nil 'keep-password)))
99
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")))
109
110 (setq password-cache-expiry nil
111 tramp-verbose 0
112 tramp-message-show-message nil)
113
114 ;; This shall happen on hydra only.
115 (when (getenv "NIX_STORE")
116 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
117
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)))
125
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'
129 being the result.")
130
131 (defun file-notify--test-remote-enabled ()
132 "Whether remote file notification is enabled."
133 (unless (consp file-notify--test-remote-enabled-checked)
134 (let (desc)
135 (ignore-errors
136 (and
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)
140 (setq desc
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))))
146 ;; Return result.
147 (cdr file-notify--test-remote-enabled-checked))
148
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))))))
160
161 (defmacro file-notify--deftest-remote (test docstring)
162 "Define ert `TEST-remote' for remote files."
163 (declare (indent 1))
164 `(ert-deftest ,(intern (concat (symbol-name test) "-remote")) ()
165 ,docstring
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)))))
175
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))
181 (should
182 (setq file-notify--test-desc
183 (file-notify-add-watch temporary-file-directory '(change) #'ignore)))
184
185 ;; Cleanup.
186 (file-notify--test-cleanup))
187
188 (file-notify--deftest-remote file-notify-test00-availability
189 "Test availability of `file-notify' for remote files.")
190
191 (ert-deftest file-notify-test01-add-watch ()
192 "Check `file-notify-add-watch'."
193 (skip-unless (file-notify--test-local-enabled))
194
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))))
198
199 ;; Check, that different valid parameters are accepted.
200 (should
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)
204 (should
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)
209 (should
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)
215 (should
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)
221
222 ;; Check error handling.
223 (should-error (file-notify-add-watch 1 2 3 4)
224 :type 'wrong-number-of-arguments)
225 (should
226 (equal (should-error
227 (file-notify-add-watch 1 2 3))
228 '(wrong-type-argument 1)))
229 (should
230 (equal (should-error
231 (file-notify-add-watch temporary-file-directory 2 3))
232 '(wrong-type-argument 2)))
233 (should
234 (equal (should-error
235 (file-notify-add-watch temporary-file-directory '(change) 3))
236 '(wrong-type-argument 3)))
237 ;; The upper directory of a file must exist.
238 (should
239 (equal (should-error
240 (file-notify-add-watch
241 file-notify--test-tmpfile1 '(change attribute-change) #'ignore))
242 `(file-notify-error
243 "Directory does not exist" ,file-notify--test-tmpfile)))
244
245 ;; Cleanup.
246 (file-notify--test-cleanup))
247
248 (file-notify--deftest-remote file-notify-test01-add-watch
249 "Check `file-notify-add-watch' for remote files.")
250
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'
254 is bound somewhere."
255 ;; Check the descriptor.
256 (should (equal (car file-notify--test-event) file-notify--test-desc))
257 ;; Check the file name.
258 (should
259 (string-prefix-p
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)
264 (should
265 (string-prefix-p
266 (file-notify--event-watched-file file-notify--test-event)
267 (file-notify--event-file1-name file-notify--test-event)))))
268
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)
274 (result
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
278 (regexp-quote ".#")
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))))))
286
287 (defun file-notify--test-make-temp-name ()
288 "Create a temporary file name for test."
289 (expand-file-name
290 (make-temp-name "file-notify-test") temporary-file-directory))
291
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))
296 (while (null ,until)
297 (read-event nil nil file-notify--test-read-event-timeout))))
298
299 (defun file-notify--test-with-events-check (events)
300 "Check whether received events match one of the EVENTS alternatives."
301 (let (result)
302 (dolist (elt events result)
303 (setq result
304 (or result
305 (equal elt (mapcar #'cadr file-notify--test-events)))))))
306
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))
312 (format
313 "Received events `%s' do not match any sequence of expected events `%s'"
314 (mapcar #'cadr file-notify--test-events) events)))
315
316 (put 'file-notify--test-with-events-check 'ert-explainer
317 'file-notify--test-with-events-explainer)
318
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."
324 (declare (indent 1))
325 `(let* ((events (if (consp (car ,events)) ,events (list ,events)))
326 (max-length (apply 'max (mapcar 'length events)))
327 create-lockfiles)
328 ;; Flush pending events.
329 (file-notify--wait-for-events
330 (file-notify--test-timeout)
331 (input-pending-p))
332 (setq file-notify--test-events nil
333 file-notify--test-results nil)
334 ,@body
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
340 ;; are as expected.
341 (dolist (result file-notify--test-results)
342 (when (ert-test-failed-p result)
343 (ert-fail
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))))
347
348 (ert-deftest file-notify-test02-events ()
349 "Check file creation/change/removal notifications."
350 (skip-unless (file-notify--test-local-enabled))
351
352 (unwind-protect
353 (progn
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))
360 (should
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
366 (cond
367 ;; cygwin recognizes only `deleted' and `stopped' events.
368 ((eq system-type 'cygwin)
369 '(deleted stopped))
370 (t '(created changed deleted stopped)))
371 (write-region
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))
376
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)
380 (should
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
386 (cond
387 ;; cygwin recognizes only `deleted' and `stopped' events.
388 ((eq system-type 'cygwin)
389 '(deleted stopped))
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 (write-region
401 "another text" nil file-notify--test-tmpfile nil 'no-message)
402 (read-event nil nil file-notify--test-read-event-timeout)
403 (delete-file file-notify--test-tmpfile))
404 (file-notify-rm-watch file-notify--test-desc)
405
406 ;; Check file creation, change and deletion when watching a
407 ;; directory. There must be a `stopped' event when deleting
408 ;; the directory.
409 (let ((temporary-file-directory
410 (make-temp-file "file-notify-test-parent" t)))
411 (should
412 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
413 file-notify--test-desc
414 (file-notify-add-watch
415 temporary-file-directory
416 '(change) #'file-notify--test-event-handler)))
417 (file-notify--test-with-events
418 (cond
419 ;; w32notify does not raise `deleted' and `stopped'
420 ;; events for the watched directory.
421 ((string-equal (file-notify--test-library) "w32notify")
422 '(created changed deleted))
423 ;; cygwin recognizes only `deleted' and `stopped' events.
424 ((eq system-type 'cygwin)
425 '(deleted stopped))
426 ;; There are two `deleted' events, for the file and for
427 ;; the directory. Except for kqueue.
428 ((string-equal (file-notify--test-library) "kqueue")
429 '(created changed deleted stopped))
430 (t '(created changed deleted deleted stopped)))
431 (write-region
432 "any text" nil file-notify--test-tmpfile nil 'no-message)
433 (read-event nil nil file-notify--test-read-event-timeout)
434 (delete-directory temporary-file-directory 'recursive))
435 (file-notify-rm-watch file-notify--test-desc))
436
437 ;; Check copy of files inside a directory.
438 (let ((temporary-file-directory
439 (make-temp-file "file-notify-test-parent" t)))
440 (should
441 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
442 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
443 file-notify--test-desc
444 (file-notify-add-watch
445 temporary-file-directory
446 '(change) #'file-notify--test-event-handler)))
447 (file-notify--test-with-events
448 (cond
449 ;; w32notify does not distinguish between `changed' and
450 ;; `attribute-changed'. It does not raise `deleted'
451 ;; and `stopped' events for the watched directory.
452 ((string-equal (file-notify--test-library) "w32notify")
453 '(created changed created changed
454 changed changed changed
455 deleted deleted))
456 ;; cygwin recognizes only `deleted' and `stopped' events.
457 ((eq system-type 'cygwin)
458 '(deleted stopped))
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 (write-region
466 "any text" nil file-notify--test-tmpfile nil 'no-message)
467 (read-event nil nil file-notify--test-read-event-timeout)
468 (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1)
469 ;; The next two events shall not be visible.
470 (read-event nil nil file-notify--test-read-event-timeout)
471 (set-file-modes file-notify--test-tmpfile 000)
472 (read-event nil nil file-notify--test-read-event-timeout)
473 (set-file-times file-notify--test-tmpfile '(0 0))
474 (read-event nil nil file-notify--test-read-event-timeout)
475 (delete-directory temporary-file-directory 'recursive))
476 (file-notify-rm-watch file-notify--test-desc))
477
478 ;; Check rename of files inside a directory.
479 (let ((temporary-file-directory
480 (make-temp-file "file-notify-test-parent" t)))
481 (should
482 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
483 file-notify--test-tmpfile1 (file-notify--test-make-temp-name)
484 file-notify--test-desc
485 (file-notify-add-watch
486 temporary-file-directory
487 '(change) #'file-notify--test-event-handler)))
488 (file-notify--test-with-events
489 (cond
490 ;; w32notify does not raise `deleted' and `stopped'
491 ;; events for the watched directory.
492 ((string-equal (file-notify--test-library) "w32notify")
493 '(created changed renamed deleted))
494 ;; cygwin recognizes only `deleted' and `stopped' events.
495 ((eq system-type 'cygwin)
496 '(deleted stopped))
497 ;; There are two `deleted' events, for the file and for
498 ;; the directory. Except for kqueue.
499 ((string-equal (file-notify--test-library) "kqueue")
500 '(created changed renamed deleted stopped))
501 (t '(created changed renamed deleted deleted stopped)))
502 (write-region
503 "any text" nil file-notify--test-tmpfile nil 'no-message)
504 (read-event nil nil file-notify--test-read-event-timeout)
505 (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1)
506 ;; After the rename, we won't get events anymore.
507 (read-event nil nil file-notify--test-read-event-timeout)
508 (delete-directory temporary-file-directory 'recursive))
509 (file-notify-rm-watch file-notify--test-desc))
510
511 ;; Check attribute change. Does not work for cygwin.
512 (unless (eq system-type 'cygwin)
513 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
514 (write-region
515 "any text" nil file-notify--test-tmpfile nil 'no-message)
516 (should
517 (setq file-notify--test-desc
518 (file-notify-add-watch
519 file-notify--test-tmpfile
520 '(attribute-change) #'file-notify--test-event-handler)))
521 (file-notify--test-with-events
522 (cond
523 ;; w32notify does not distinguish between `changed' and
524 ;; `attribute-changed'. Under MS Windows 7, we get
525 ;; four `changed' events, and under MS Windows 10 just
526 ;; two. Strange.
527 ((string-equal (file-notify--test-library) "w32notify")
528 '((changed changed)
529 (changed changed changed changed)))
530 ;; For kqueue and in the remote case, `write-region'
531 ;; raises also an `attribute-changed' event.
532 ((or (string-equal (file-notify--test-library) "kqueue")
533 (file-remote-p temporary-file-directory))
534 '(attribute-changed attribute-changed attribute-changed))
535 (t '(attribute-changed attribute-changed)))
536 (write-region
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)))
545
546 ;; Cleanup.
547 (file-notify--test-cleanup)))
548
549 (file-notify--deftest-remote file-notify-test02-events
550 "Check file creation/change/removal notifications for remote files.")
551
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)
556
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))
563 buf)
564 (unwind-protect
565 (progn
566 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
567
568 (write-region
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
575 ;; returns nil.
576 (sleep-for 1)
577 (auto-revert-mode 1)
578
579 ;; `auto-revert-buffers' runs every 5".
580 (with-timeout (timeout (ignore))
581 (while (null auto-revert-notify-watch-descriptor)
582 (sleep-for 1)))
583
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)
588
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)))
593 (sleep-for 1)
594 (write-region
595 "another text" nil file-notify--test-tmpfile nil 'no-message)
596
597 ;; Check, that the buffer has been reverted.
598 (with-current-buffer (get-buffer-create "*Messages*")
599 (file-notify--wait-for-events
600 timeout
601 (string-match
602 (format-message "Reverting buffer `%s'." (buffer-name buf))
603 (buffer-string))))
604 (should (string-match "another text" (buffer-string)))
605
606 ;; Stop file notification. Autorevert shall still work via polling.
607 (file-notify-rm-watch auto-revert-notify-watch-descriptor)
608 (file-notify--wait-for-events
609 timeout (null auto-revert-use-notify))
610 (should-not auto-revert-use-notify)
611 (should-not auto-revert-notify-watch-descriptor)
612
613 ;; Modify file. We wait for two seconds, in order to
614 ;; have another timestamp. One second seems to be too
615 ;; short.
616 (with-current-buffer (get-buffer-create "*Messages*")
617 (narrow-to-region (point-max) (point-max)))
618 (sleep-for 2)
619 (write-region
620 "foo bla" nil file-notify--test-tmpfile nil 'no-message)
621
622 ;; Check, that the buffer has been reverted.
623 (with-current-buffer (get-buffer-create "*Messages*")
624 (file-notify--wait-for-events
625 timeout
626 (string-match
627 (format-message "Reverting buffer `%s'." (buffer-name buf))
628 (buffer-string))))
629 (should (string-match "foo bla" (buffer-string)))))
630
631 ;; Cleanup.
632 (with-current-buffer "*Messages*" (widen))
633 (ignore-errors (kill-buffer buf))
634 (file-notify--test-cleanup))))
635
636 (file-notify--deftest-remote file-notify-test03-autorevert
637 "Check autorevert via file notification for remote files.")
638
639 (ert-deftest file-notify-test04-file-validity ()
640 "Check `file-notify-valid-p' for files."
641 (skip-unless (file-notify--test-local-enabled))
642
643 (unwind-protect
644 (progn
645 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
646 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
647 (should
648 (setq file-notify--test-desc
649 (file-notify-add-watch
650 file-notify--test-tmpfile
651 '(change) #'file-notify--test-event-handler)))
652 (should (file-notify-valid-p file-notify--test-desc))
653 ;; After calling `file-notify-rm-watch', the descriptor is not
654 ;; valid anymore.
655 (file-notify-rm-watch file-notify--test-desc)
656 (should-not (file-notify-valid-p file-notify--test-desc))
657 (delete-file file-notify--test-tmpfile))
658
659 ;; Cleanup.
660 (file-notify--test-cleanup))
661
662 (unwind-protect
663 (progn
664 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
665 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
666 (should
667 (setq file-notify--test-desc
668 (file-notify-add-watch
669 file-notify--test-tmpfile
670 '(change) #'file-notify--test-event-handler)))
671 (should (file-notify-valid-p file-notify--test-desc))
672 (file-notify--test-with-events
673 (cond
674 ;; cygwin recognizes only `deleted' and `stopped' events.
675 ((eq system-type 'cygwin)
676 '(deleted stopped))
677 ;; inotify and kqueue raise just one `changed' event.
678 ((or (string-equal "inotify" (file-notify--test-library))
679 (string-equal "kqueue" (file-notify--test-library)))
680 '(changed deleted stopped))
681 ;; gfilenotify raises one or two `changed' events
682 ;; randomly, no chance to test. So we accept both cases.
683 ((string-equal "gfilenotify" (file-notify--test-library))
684 '((changed deleted stopped)
685 (changed changed deleted stopped)))
686 (t '(changed changed deleted stopped)))
687 (write-region
688 "another text" nil file-notify--test-tmpfile nil 'no-message)
689 (read-event nil nil file-notify--test-read-event-timeout)
690 (delete-file file-notify--test-tmpfile))
691 ;; After deleting the file, the descriptor is not valid anymore.
692 (should-not (file-notify-valid-p file-notify--test-desc))
693 (file-notify-rm-watch file-notify--test-desc))
694
695 ;; Cleanup.
696 (file-notify--test-cleanup))
697
698 (unwind-protect
699 (let ((temporary-file-directory
700 (make-temp-file "file-notify-test-parent" t)))
701 (should
702 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name)
703 file-notify--test-desc
704 (file-notify-add-watch
705 temporary-file-directory
706 '(change) #'file-notify--test-event-handler)))
707 (should (file-notify-valid-p file-notify--test-desc))
708 (file-notify--test-with-events
709 (cond
710 ;; w32notify does not raise `deleted' and `stopped' events
711 ;; for the watched directory.
712 ((string-equal (file-notify--test-library) "w32notify")
713 '(created changed deleted))
714 ;; cygwin recognizes only `deleted' and `stopped' events.
715 ((eq system-type 'cygwin)
716 '(deleted stopped))
717 ;; There are two `deleted' events, for the file and for the
718 ;; directory. Except for kqueue.
719 ((string-equal (file-notify--test-library) "kqueue")
720 '(created changed deleted stopped))
721 (t '(created changed deleted deleted stopped)))
722 (write-region
723 "any text" nil file-notify--test-tmpfile nil 'no-message)
724 (read-event nil nil file-notify--test-read-event-timeout)
725 (delete-directory temporary-file-directory t))
726 ;; After deleting the parent directory, the descriptor must
727 ;; not be valid anymore.
728 (should-not (file-notify-valid-p file-notify--test-desc)))
729
730 ;; Cleanup.
731 (file-notify--test-cleanup)))
732
733 (file-notify--deftest-remote file-notify-test04-file-validity
734 "Check `file-notify-valid-p' via file notification for remote files.")
735
736 (ert-deftest file-notify-test05-dir-validity ()
737 "Check `file-notify-valid-p' for directories."
738 (skip-unless (file-notify--test-local-enabled))
739
740 (unwind-protect
741 (progn
742 (should
743 (setq file-notify--test-tmpfile
744 (make-temp-file "file-notify-test-parent" t)))
745 (should
746 (setq file-notify--test-desc
747 (file-notify-add-watch
748 file-notify--test-tmpfile
749 '(change) #'file-notify--test-event-handler)))
750 (should (file-notify-valid-p file-notify--test-desc))
751 ;; After removing the watch, the descriptor must not be valid
752 ;; anymore.
753 (file-notify-rm-watch file-notify--test-desc)
754 (file-notify--wait-for-events
755 (file-notify--test-timeout)
756 (not (file-notify-valid-p file-notify--test-desc)))
757 (should-not (file-notify-valid-p file-notify--test-desc)))
758
759 ;; Cleanup.
760 (file-notify--test-cleanup))
761
762 (unwind-protect
763 (progn
764 (should
765 (setq file-notify--test-tmpfile
766 (make-temp-file "file-notify-test-parent" t)))
767 (should
768 (setq file-notify--test-desc
769 (file-notify-add-watch
770 file-notify--test-tmpfile
771 '(change) #'file-notify--test-event-handler)))
772 (should (file-notify-valid-p file-notify--test-desc))
773 ;; After deleting the directory, the descriptor must not be
774 ;; valid anymore.
775 (delete-directory file-notify--test-tmpfile t)
776 (file-notify--wait-for-events
777 (file-notify--test-timeout)
778 (not (file-notify-valid-p file-notify--test-desc)))
779 (should-not (file-notify-valid-p file-notify--test-desc)))
780
781 ;; Cleanup.
782 (file-notify--test-cleanup)))
783
784 (file-notify--deftest-remote file-notify-test05-dir-validity
785 "Check `file-notify-valid-p' via file notification for remote directories.")
786
787 (ert-deftest file-notify-test06-many-events ()
788 "Check that events are not dropped."
789 :tags '(:expensive-test)
790 (skip-unless (file-notify--test-local-enabled))
791 ;; Under cygwin events arrive in random order. Impossible to define a test.
792 (skip-unless (not (eq system-type 'cygwin)))
793
794 (should
795 (setq file-notify--test-tmpfile
796 (make-temp-file "file-notify-test-parent" t)))
797 (should
798 (setq file-notify--test-desc
799 (file-notify-add-watch
800 file-notify--test-tmpfile
801 '(change) #'file-notify--test-event-handler)))
802 (unwind-protect
803 (let ((n 1000)
804 source-file-list target-file-list
805 (default-directory file-notify--test-tmpfile))
806 (dotimes (i n)
807 ;; It matters which direction we rename, at least for
808 ;; kqueue. This backend parses directories in alphabetic
809 ;; order (x%d before y%d). So we rename into both directions.
810 (if (zerop (mod i 2))
811 (progn
812 (push (expand-file-name (format "x%d" i)) source-file-list)
813 (push (expand-file-name (format "y%d" i)) target-file-list))
814 (push (expand-file-name (format "y%d" i)) source-file-list)
815 (push (expand-file-name (format "x%d" i)) target-file-list)))
816 (file-notify--test-with-events (make-list (+ n n) 'created)
817 (let ((source-file-list source-file-list)
818 (target-file-list target-file-list))
819 (while (and source-file-list target-file-list)
820 (read-event nil nil file-notify--test-read-event-timeout)
821 (write-region "" nil (pop source-file-list) nil 'no-message)
822 (read-event nil nil file-notify--test-read-event-timeout)
823 (write-region "" nil (pop target-file-list) nil 'no-message))))
824 (file-notify--test-with-events
825 (cond
826 ;; w32notify fires both `deleted' and `renamed' events.
827 ((string-equal (file-notify--test-library) "w32notify")
828 (let (r)
829 (dotimes (_i n r)
830 (setq r (append '(deleted renamed) r)))))
831 (t (make-list n 'renamed)))
832 (let ((source-file-list source-file-list)
833 (target-file-list target-file-list))
834 (while (and source-file-list target-file-list)
835 (read-event nil nil file-notify--test-read-event-timeout)
836 (rename-file (pop source-file-list) (pop target-file-list) t))))
837 (file-notify--test-with-events (make-list n 'deleted)
838 (dolist (file target-file-list)
839 (read-event nil nil file-notify--test-read-event-timeout)
840 (delete-file file) file-notify--test-read-event-timeout)))
841
842 ;; Cleanup.
843 (file-notify--test-cleanup)))
844
845 (file-notify--deftest-remote file-notify-test06-many-events
846 "Check that events are not dropped for remote directories.")
847
848 (ert-deftest file-notify-test07-backup ()
849 "Check that backup keeps file notification."
850 (skip-unless (file-notify--test-local-enabled))
851
852 (unwind-protect
853 (progn
854 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
855 (write-region "any text" nil file-notify--test-tmpfile nil 'no-message)
856 (should
857 (setq file-notify--test-desc
858 (file-notify-add-watch
859 file-notify--test-tmpfile
860 '(change) #'file-notify--test-event-handler)))
861 (should (file-notify-valid-p file-notify--test-desc))
862 (file-notify--test-with-events
863 (cond
864 ;; For w32notify and in the remote case, there are two
865 ;; `changed' events.
866 ((or (string-equal (file-notify--test-library) "w32notify")
867 (file-remote-p temporary-file-directory))
868 '(changed changed))
869 ;; gfilenotify raises one or two `changed' events
870 ;; randomly, no chance to test. So we accept both cases.
871 ((string-equal "gfilenotify" (file-notify--test-library))
872 '((changed)
873 (changed changed)))
874 (t '(changed)))
875 ;; There shouldn't be any problem, because the file is kept.
876 (with-temp-buffer
877 (let ((buffer-file-name file-notify--test-tmpfile)
878 (make-backup-files t)
879 (backup-by-copying t)
880 (kept-new-versions 1)
881 (delete-old-versions t))
882 (insert "another text")
883 (save-buffer))))
884 ;; After saving the buffer, the descriptor is still valid.
885 (should (file-notify-valid-p file-notify--test-desc))
886 (delete-file file-notify--test-tmpfile))
887
888 ;; Cleanup.
889 (file-notify--test-cleanup))
890
891 (unwind-protect
892 (progn
893 ;; It doesn't work for kqueue, because we don't use an
894 ;; implicit directory monitor.
895 (unless (string-equal (file-notify--test-library) "kqueue")
896 (setq file-notify--test-tmpfile (file-notify--test-make-temp-name))
897 (write-region
898 "any text" nil file-notify--test-tmpfile nil 'no-message)
899 (should
900 (setq file-notify--test-desc
901 (file-notify-add-watch
902 file-notify--test-tmpfile
903 '(change) #'file-notify--test-event-handler)))
904 (should (file-notify-valid-p file-notify--test-desc))
905 (file-notify--test-with-events '(renamed created changed)
906 ;; The file is renamed when creating a backup. It shall
907 ;; still be watched.
908 (with-temp-buffer
909 (let ((buffer-file-name file-notify--test-tmpfile)
910 (make-backup-files t)
911 (backup-by-copying nil)
912 (backup-by-copying-when-mismatch nil)
913 (kept-new-versions 1)
914 (delete-old-versions t))
915 (insert "another text")
916 (save-buffer))))
917 ;; After saving the buffer, the descriptor is still valid.
918 (should (file-notify-valid-p file-notify--test-desc))
919 (delete-file file-notify--test-tmpfile)))
920
921 ;; Cleanup.
922 (file-notify--test-cleanup)))
923
924 (file-notify--deftest-remote file-notify-test07-backup
925 "Check that backup keeps file notification for remote files.")
926
927 (ert-deftest file-notify-test08-watched-file-in-watched-dir ()
928 "Watches a directory and a file in that directory separately.
929 Checks that the callbacks are only called with events with
930 descriptors that were issued when registering the watches. This
931 test caters for the situation in bug#22736 where the callback for
932 the directory received events for the file with the descriptor of
933 the file watch."
934 :tags '(:expensive-test)
935 (skip-unless (file-notify--test-local-enabled))
936
937 ;; A directory to be watched.
938 (should
939 (setq file-notify--test-tmpfile
940 (make-temp-file "file-notify-test-parent" t)))
941 ;; A file to be watched.
942 (should
943 (setq file-notify--test-tmpfile1
944 (let ((temporary-file-directory file-notify--test-tmpfile))
945 (file-notify--test-make-temp-name))))
946 (write-region "any text" nil file-notify--test-tmpfile1 nil 'no-message)
947 (unwind-protect
948 (cl-flet (;; Directory monitor.
949 (dir-callback (event)
950 (let ((file-notify--test-desc file-notify--test-desc1))
951 (file-notify--test-event-handler event)))
952 ;; File monitor.
953 (file-callback (event)
954 (let ((file-notify--test-desc file-notify--test-desc2))
955 (file-notify--test-event-handler event))))
956 (should
957 (setq file-notify--test-desc1
958 (file-notify-add-watch
959 file-notify--test-tmpfile
960 '(change) #'dir-callback)))
961 (should
962 (setq file-notify--test-desc2
963 (file-notify-add-watch
964 file-notify--test-tmpfile1
965 '(change) #'file-callback)))
966 (should (file-notify-valid-p file-notify--test-desc1))
967 (should (file-notify-valid-p file-notify--test-desc2))
968 (should-not (equal file-notify--test-desc1 file-notify--test-desc2))
969 ;; gfilenotify raises one or two `changed' events randomly in
970 ;; the file monitor, no chance to test.
971 (unless (string-equal "gfilenotify" (file-notify--test-library))
972 (let ((n 100) events)
973 ;; Compute the expected events.
974 (dotimes (_i (/ n 2))
975 (setq events
976 (append
977 (append
978 ;; Directory monitor and file monitor.
979 (cond
980 ;; In the remote case, there are two `changed'
981 ;; events.
982 ((file-remote-p temporary-file-directory)
983 '(changed changed changed changed))
984 ;; The directory monitor in kqueue does not
985 ;; raise any `changed' event. Just the file
986 ;; monitor event is received.
987 ((string-equal (file-notify--test-library) "kqueue")
988 '(changed))
989 ;; Otherwise, both monitors report the
990 ;; `changed' event.
991 (t '(changed changed)))
992 ;; Just the directory monitor.
993 (cond
994 ;; In kqueue, there is an additional `changed'
995 ;; event. Why?
996 ((string-equal (file-notify--test-library) "kqueue")
997 '(changed created changed))
998 (t '(created changed))))
999 events)))
1000
1001 ;; Run the test.
1002 (file-notify--test-with-events events
1003 (dotimes (i n)
1004 (read-event nil nil file-notify--test-read-event-timeout)
1005 (if (zerop (mod i 2))
1006 (write-region
1007 "any text" nil file-notify--test-tmpfile1 t 'no-message)
1008 (let ((temporary-file-directory file-notify--test-tmpfile))
1009 (write-region
1010 "any text" nil
1011 (file-notify--test-make-temp-name) nil 'no-message)))))))
1012
1013 ;; If we delete the file, the directory monitor shall still be
1014 ;; active. We receive the `deleted' event from both the
1015 ;; directory and the file monitor. The `stopped' event is
1016 ;; from the file monitor. It's undecided in which order the
1017 ;; the directory and the file monitor are triggered.
1018 (file-notify--test-with-events
1019 '((deleted deleted stopped)
1020 (deleted stopped deleted))
1021 (delete-file file-notify--test-tmpfile1))
1022 (should (file-notify-valid-p file-notify--test-desc1))
1023 (should-not (file-notify-valid-p file-notify--test-desc2))
1024
1025 ;; Now we delete the directory.
1026 (file-notify--test-with-events
1027 (cond
1028 ;; In kqueue, just one `deleted' event for the directory
1029 ;; is received.
1030 ((string-equal (file-notify--test-library) "kqueue")
1031 '(deleted stopped))
1032 (t (append
1033 ;; The directory monitor raises a `deleted' event for
1034 ;; every file contained in the directory, we must
1035 ;; count them.
1036 (make-list
1037 (length
1038 (directory-files
1039 file-notify--test-tmpfile nil
1040 directory-files-no-dot-files-regexp 'nosort))
1041 'deleted)
1042 ;; The events of the directory itself.
1043 (cond
1044 ;; w32notify does not raise `deleted' and `stopped'
1045 ;; events for the watched directory.
1046 ((string-equal (file-notify--test-library) "w32notify") '())
1047 (t '(deleted stopped))))))
1048 (delete-directory file-notify--test-tmpfile 'recursive))
1049 (should-not (file-notify-valid-p file-notify--test-desc1))
1050 (should-not (file-notify-valid-p file-notify--test-desc2)))
1051
1052 ;; Cleanup.
1053 (file-notify--test-cleanup)))
1054
1055 (file-notify--deftest-remote file-notify-test08-watched-file-in-watched-dir
1056 "Check `file-notify-test08-watched-file-in-watched-dir' for remote files.")
1057
1058 (ert-deftest file-notify-test09-sufficient-resources ()
1059 "Check that file notification does not use too many resources."
1060 :tags '(:expensive-test)
1061 (skip-unless (file-notify--test-local-enabled))
1062 ;; This test is intended for kqueue only.
1063 (skip-unless (string-equal (file-notify--test-library) "kqueue"))
1064
1065 (should
1066 (setq file-notify--test-tmpfile
1067 (make-temp-file "file-notify-test-parent" t)))
1068 (unwind-protect
1069 (let ((temporary-file-directory file-notify--test-tmpfile)
1070 descs)
1071 (should-error
1072 (while t
1073 ;; We watch directories, because we want to reach the upper
1074 ;; limit. Watching a file might not be sufficient, because
1075 ;; most of the libraries implement this as watching the
1076 ;; upper directory.
1077 (setq file-notify--test-tmpfile1
1078 (make-temp-file "file-notify-test-parent" t)
1079 descs
1080 (cons
1081 (should
1082 (file-notify-add-watch
1083 file-notify--test-tmpfile1 '(change) #'ignore))
1084 descs)))
1085 :type 'file-notify-error)
1086 ;; Remove watches. If we don't do it prior removing
1087 ;; directories, Emacs crashes in batch mode.
1088 (dolist (desc descs)
1089 (file-notify-rm-watch desc))
1090 ;; Remove directories.
1091 (delete-directory file-notify--test-tmpfile 'recursive))
1092
1093 ;; Cleanup.
1094 (file-notify--test-cleanup)))
1095
1096 (file-notify--deftest-remote file-notify-test09-sufficient-resources
1097 "Check `file-notify-test09-sufficient-resources' for remote files.")
1098
1099 (defun file-notify-test-all (&optional interactive)
1100 "Run all tests for \\[file-notify]."
1101 (interactive "p")
1102 (if interactive
1103 (ert-run-tests-interactively "^file-notify-")
1104 (ert-run-tests-batch "^file-notify-")))
1105
1106 ;; TODO:
1107
1108 ;; * kqueue does not send all expected `deleted' events. Maybe due to
1109 ;; the missing directory monitor.
1110 ;; * For w32notify, no `deleted' and `stopped' events arrive when a
1111 ;; directory is removed.
1112 ;; * For w32notify, no `attribute-changed' events arrive. Its sends
1113 ;; `changed' events instead.
1114 ;; * Check, why cygwin recognizes only `deleted' and `stopped' events.
1115
1116 (provide 'file-notify-tests)
1117 ;;; file-notify-tests.el ends here