- (progn
- (setq file-notify--test-tmpfile
- (make-temp-file "dir" t))
- (setq file-notify--test-tmpfile1
- (let ((temporary-file-directory file-notify--test-tmpfile))
- (make-temp-file "file")))
- (cl-flet ((dir-callback (event)
- (let ((file-notify--test-desc file-notify--test-desc1)
- (file-notify--test-tmpfile
- (file-notify--event-file-name event)))
- (file-notify--test-event-handler event)))
- (file-callback (event)
- (let ((file-notify--test-desc file-notify--test-desc2))
- (file-notify--test-event-handler event))))
- (should
- (setq file-notify--test-desc1
- (file-notify-add-watch
- file-notify--test-tmpfile
- '(change attribute-change) #'dir-callback)))
- (should
- (setq file-notify--test-desc2
- (file-notify-add-watch
- file-notify--test-tmpfile1
- '(change attribute-change) #'file-callback)))
- (should (file-notify-valid-p file-notify--test-desc1))
- (should (file-notify-valid-p file-notify--test-desc2))
- (dotimes (i 100)
- (read-event nil nil file-notify--test-read-event-timeout)
- (if (< 0 (random))
- (write-region
- "any text" nil file-notify--test-tmpfile1 t 'no-message)
- (let ((temporary-file-directory file-notify--test-tmpfile))
- (make-temp-file "fileX"))))
- (should (file-notify-valid-p file-notify--test-desc1))
- (should (file-notify-valid-p file-notify--test-desc2))
- (delete-file file-notify--test-tmpfile1)
- (delete-directory file-notify--test-tmpfile 'recursive))
+ (cl-flet (;; Directory monitor.
+ (dir-callback (event)
+ (let ((file-notify--test-desc file-notify--test-desc1))
+ (file-notify--test-event-handler event)))
+ ;; File monitor.
+ (file-callback (event)
+ (let ((file-notify--test-desc file-notify--test-desc2))
+ (file-notify--test-event-handler event))))
+ (should
+ (setq file-notify--test-desc1
+ (file-notify-add-watch
+ file-notify--test-tmpfile
+ '(change) #'dir-callback)))
+ (should
+ (setq file-notify--test-desc2
+ (file-notify-add-watch
+ file-notify--test-tmpfile1
+ '(change) #'file-callback)))
+ (should (file-notify-valid-p file-notify--test-desc1))
+ (should (file-notify-valid-p file-notify--test-desc2))
+ (should-not (equal file-notify--test-desc1 file-notify--test-desc2))
+ ;; gfilenotify raises one or two `changed' events randomly in
+ ;; the file monitor, no chance to test.
+ (unless (string-equal "gfilenotify" (file-notify--test-library))
+ (let ((n 100) events)
+ ;; Compute the expected events.
+ (dotimes (_i (/ n 2))
+ (setq events
+ (append
+ (append
+ ;; Directory monitor and file monitor.
+ (cond
+ ;; In the remote case, there are two `changed'
+ ;; events.
+ ((file-remote-p temporary-file-directory)
+ '(changed changed changed changed))
+ ;; The directory monitor in kqueue does not
+ ;; raise any `changed' event. Just the file
+ ;; monitor event is received.
+ ((string-equal (file-notify--test-library) "kqueue")
+ '(changed))
+ ;; Otherwise, both monitors report the
+ ;; `changed' event.
+ (t '(changed changed)))
+ ;; Just the directory monitor.
+ (cond
+ ;; In kqueue, there is an additional `changed'
+ ;; event. Why?
+ ((string-equal (file-notify--test-library) "kqueue")
+ '(changed created changed))
+ (t '(created changed))))
+ events)))
+
+ ;; Run the test.
+ (file-notify--test-with-events events
+ (dotimes (i n)
+ (read-event nil nil file-notify--test-read-event-timeout)
+ (if (zerop (mod i 2))
+ (write-region
+ "any text" nil file-notify--test-tmpfile1 t 'no-message)
+ (let ((temporary-file-directory file-notify--test-tmpfile))
+ (write-region
+ "any text" nil
+ (file-notify--test-make-temp-name) nil 'no-message)))))))
+
+ ;; If we delete the file, the directory monitor shall still be
+ ;; active. We receive the `deleted' event from both the
+ ;; directory and the file monitor. The `stopped' event is
+ ;; from the file monitor. It's undecided in which order the
+ ;; the directory and the file monitor are triggered.
+ (file-notify--test-with-events
+ '((deleted deleted stopped)
+ (deleted stopped deleted))
+ (delete-file file-notify--test-tmpfile1))
+ (should (file-notify-valid-p file-notify--test-desc1))
+ (should-not (file-notify-valid-p file-notify--test-desc2))