2024-08-18 19:42:44 +00:00
|
|
|
# Copyright (c) 2024 Calvin Rose & contributors
|
2024-08-18 16:45:21 +00:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to
|
|
|
|
# deal in the Software without restriction, including without limitation the
|
|
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
# IN THE SOFTWARE.
|
|
|
|
|
|
|
|
(import ./helper :prefix "" :exit true)
|
|
|
|
(start-suite)
|
|
|
|
|
|
|
|
(assert true)
|
|
|
|
|
|
|
|
(def chan (ev/chan 1000))
|
2024-08-18 21:18:09 +00:00
|
|
|
(def is-win (or (= :mingw (os/which)) (= :windows (os/which))))
|
2024-08-18 20:44:59 +00:00
|
|
|
(def is-linux (= :linux (os/which)))
|
2024-08-18 16:45:21 +00:00
|
|
|
|
2024-08-18 21:25:47 +00:00
|
|
|
# If not supported, exit early
|
|
|
|
(def [supported msg] (protect (filewatch/new chan)))
|
|
|
|
(when (and (not supported) (string/find "filewatch not supported" msg))
|
|
|
|
(end-suite)
|
|
|
|
(quit))
|
|
|
|
|
2024-08-18 16:45:21 +00:00
|
|
|
# Test GC
|
|
|
|
(assert-no-error "filewatch/new" (filewatch/new chan))
|
|
|
|
(gccollect)
|
|
|
|
|
|
|
|
(defn- expect
|
2024-08-31 16:42:16 +00:00
|
|
|
[key value & more-kvs]
|
2024-08-18 16:45:21 +00:00
|
|
|
(ev/with-deadline
|
|
|
|
1
|
|
|
|
(def event (ev/take chan))
|
|
|
|
(when is-verbose (pp event))
|
|
|
|
(assert event "check event")
|
2024-08-31 16:42:16 +00:00
|
|
|
(assert (= value (get event key)) (string/format "got %p, expected %p" (get event key) value))
|
|
|
|
(when (next more-kvs)
|
|
|
|
(each [k v] (partition 2 more-kvs)
|
|
|
|
(assert (= v (get event k)) (string/format "got %p, expected %p" (get event k) v))))))
|
2024-08-18 16:45:21 +00:00
|
|
|
|
|
|
|
(defn- expect-empty
|
|
|
|
[]
|
|
|
|
(assert (zero? (ev/count chan)) "channel check empty")
|
|
|
|
(ev/sleep 0) # turn the event loop
|
2024-08-20 23:06:57 +00:00
|
|
|
(assert (zero? (ev/count chan)) "channel check empty")
|
|
|
|
# Drain if not empty, help with failures after this
|
|
|
|
(while (pos? (ev/count chan)) (printf "extra: %p" (ev/take chan))))
|
2024-08-18 16:45:21 +00:00
|
|
|
|
2024-08-20 23:30:56 +00:00
|
|
|
(defn- expect-maybe
|
|
|
|
"On wine + mingw, we get an extra event. This is a wine peculiarity."
|
|
|
|
[key value]
|
|
|
|
(ev/with-deadline
|
|
|
|
1
|
|
|
|
(ev/sleep 0)
|
|
|
|
(when (pos? (ev/count chan))
|
|
|
|
(def event (ev/take chan))
|
|
|
|
(when is-verbose (pp event))
|
|
|
|
(assert event "check event")
|
|
|
|
(assert (= value (get event key)) (string/format "got %p, expected %p" (get event key) value)))))
|
|
|
|
|
2024-08-18 16:45:21 +00:00
|
|
|
(defn spit-file
|
|
|
|
[dir name]
|
|
|
|
(def path (string dir "/" name))
|
|
|
|
(spit path "test text"))
|
|
|
|
|
2024-08-18 20:44:59 +00:00
|
|
|
# Different operating systems report events differently. While it would be nice to
|
|
|
|
# normalize this, each system has very large limitations in what can be reported when
|
|
|
|
# compared with other systems. As such, the maximum subset of common functionality here
|
|
|
|
# is quite small. Instead, test the capabilities of each system.
|
|
|
|
|
2024-08-18 16:45:21 +00:00
|
|
|
# Create a file watcher on two test directories
|
|
|
|
(def fw (filewatch/new chan))
|
|
|
|
(def td1 (randdir))
|
|
|
|
(def td2 (randdir))
|
2024-08-31 16:42:16 +00:00
|
|
|
(def td3 (randdir))
|
2024-08-18 20:44:59 +00:00
|
|
|
(rmrf td1)
|
|
|
|
(rmrf td2)
|
2024-08-18 16:45:21 +00:00
|
|
|
(os/mkdir td1)
|
|
|
|
(os/mkdir td2)
|
2024-08-31 16:42:16 +00:00
|
|
|
(os/mkdir td3)
|
|
|
|
(spit-file td3 "file3.txt")
|
2024-08-18 21:18:09 +00:00
|
|
|
(when is-win
|
|
|
|
(filewatch/add fw td1 :last-write :last-access :file-name :dir-name :size :attributes :recursive)
|
|
|
|
(filewatch/add fw td2 :last-write :last-access :file-name :dir-name :size :attributes))
|
|
|
|
(when is-linux
|
2024-08-31 16:42:16 +00:00
|
|
|
(filewatch/add fw (string td3 "/file3.txt") :close-write :create :delete)
|
2024-08-18 21:18:09 +00:00
|
|
|
(filewatch/add fw td1 :close-write :create :delete)
|
|
|
|
(filewatch/add fw td2 :close-write :create :delete :ignored))
|
2024-08-18 16:45:21 +00:00
|
|
|
(assert-no-error "filewatch/listen no error" (filewatch/listen fw))
|
|
|
|
|
2024-08-18 20:44:59 +00:00
|
|
|
#
|
|
|
|
# Windows file writing
|
|
|
|
#
|
2024-08-18 16:45:21 +00:00
|
|
|
|
2024-08-18 20:44:59 +00:00
|
|
|
(when is-win
|
|
|
|
(spit-file td1 "file1.txt")
|
2024-08-31 16:42:16 +00:00
|
|
|
(expect :type :added :file-name "file1.txt" :dir-name td1)
|
2024-08-18 20:44:59 +00:00
|
|
|
(expect :type :modified)
|
2024-08-20 23:30:56 +00:00
|
|
|
(expect-maybe :type :modified) # for mingw + wine
|
2024-08-18 20:44:59 +00:00
|
|
|
(gccollect)
|
|
|
|
(spit-file td1 "file1.txt")
|
|
|
|
(expect :type :modified)
|
|
|
|
(expect :type :modified)
|
|
|
|
(expect-empty)
|
|
|
|
(gccollect)
|
|
|
|
|
|
|
|
# Check td2
|
|
|
|
(spit-file td2 "file2.txt")
|
|
|
|
(expect :type :added)
|
|
|
|
(expect :type :modified)
|
2024-08-20 23:30:56 +00:00
|
|
|
(expect-maybe :type :modified)
|
2024-08-18 20:44:59 +00:00
|
|
|
|
|
|
|
# Remove a file, then wait for remove event
|
|
|
|
(rmrf (string td1 "/file1.txt"))
|
|
|
|
(expect :type :removed)
|
|
|
|
(expect-empty)
|
|
|
|
|
|
|
|
# Unlisten to some events
|
|
|
|
(filewatch/remove fw td2)
|
|
|
|
|
|
|
|
# Check that we don't get anymore events from test directory 2
|
|
|
|
(spit-file td2 "file2.txt")
|
|
|
|
(expect-empty)
|
|
|
|
|
|
|
|
# Repeat and things should still work with test directory 1
|
|
|
|
(spit-file td1 "file1.txt")
|
|
|
|
(expect :type :added)
|
|
|
|
(expect :type :modified)
|
2024-08-20 23:30:56 +00:00
|
|
|
(expect-maybe :type :modified)
|
2024-08-18 20:44:59 +00:00
|
|
|
(gccollect)
|
|
|
|
(spit-file td1 "file1.txt")
|
|
|
|
(expect :type :modified)
|
|
|
|
(expect :type :modified)
|
2024-08-20 23:30:56 +00:00
|
|
|
(expect-maybe :type :modified)
|
2024-08-18 20:44:59 +00:00
|
|
|
(gccollect))
|
|
|
|
|
|
|
|
#
|
|
|
|
# Linux file writing
|
|
|
|
#
|
|
|
|
|
|
|
|
(when is-linux
|
|
|
|
(spit-file td1 "file1.txt")
|
2024-08-31 16:42:16 +00:00
|
|
|
(expect :type :create :file-name "file1.txt" :dir-name td1)
|
2024-08-18 20:44:59 +00:00
|
|
|
(expect :type :close-write)
|
|
|
|
(expect-empty)
|
|
|
|
(gccollect)
|
|
|
|
(spit-file td1 "file1.txt")
|
|
|
|
(expect :type :close-write)
|
|
|
|
(expect-empty)
|
|
|
|
(gccollect)
|
|
|
|
|
2024-08-31 16:42:16 +00:00
|
|
|
# Check file3.txt
|
|
|
|
(spit-file td3 "file3.txt")
|
|
|
|
(expect :type :close-write :file-name "file3.txt" :dir-name td3)
|
|
|
|
(expect-empty)
|
|
|
|
|
2024-08-18 20:44:59 +00:00
|
|
|
# Check td2
|
|
|
|
(spit-file td2 "file2.txt")
|
|
|
|
(expect :type :create)
|
|
|
|
(expect :type :close-write)
|
|
|
|
(expect-empty)
|
|
|
|
|
|
|
|
# Remove a file, then wait for remove event
|
|
|
|
(rmrf (string td1 "/file1.txt"))
|
|
|
|
(expect :type :delete)
|
|
|
|
(expect-empty)
|
|
|
|
|
|
|
|
# Unlisten to some events
|
|
|
|
(filewatch/remove fw td2)
|
|
|
|
(expect :type :ignored)
|
|
|
|
(expect-empty)
|
|
|
|
|
|
|
|
# Check that we don't get anymore events from test directory 2
|
|
|
|
(spit-file td2 "file2.txt")
|
|
|
|
(expect-empty)
|
|
|
|
|
|
|
|
# Repeat and things should still work with test directory 1
|
|
|
|
(spit-file td1 "file1.txt")
|
|
|
|
(expect :type :create)
|
|
|
|
(expect :type :close-write)
|
|
|
|
(expect-empty)
|
|
|
|
(gccollect)
|
|
|
|
(spit-file td1 "file1.txt")
|
|
|
|
(expect :type :close-write)
|
|
|
|
(expect-empty)
|
|
|
|
(gccollect))
|
2024-08-18 16:45:21 +00:00
|
|
|
|
|
|
|
(assert-no-error "filewatch/unlisten no error" (filewatch/unlisten fw))
|
|
|
|
(assert-no-error "cleanup 1" (rmrf td1))
|
|
|
|
(assert-no-error "cleanup 2" (rmrf td2))
|
2024-08-31 16:42:16 +00:00
|
|
|
(assert-no-error "cleanup 3" (rmrf td3))
|
2024-08-18 16:45:21 +00:00
|
|
|
|
|
|
|
(end-suite)
|