-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathsuite-filewatch.janet
204 lines (176 loc) · 6.1 KB
/
suite-filewatch.janet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright (c) 2025 Calvin Rose & contributors
#
# 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))
(def is-win (or (= :mingw (os/which)) (= :windows (os/which))))
(def is-linux (= :linux (os/which)))
# 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))
# Test GC
(assert-no-error "filewatch/new" (filewatch/new chan))
(gccollect)
(defn- expect
[key value & more-kvs]
(ev/with-deadline
1
(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))
(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))))))
(defn- expect-empty
[]
(assert (zero? (ev/count chan)) "channel check empty")
(ev/sleep 0) # turn the event loop
(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))))
(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)))))
(defn spit-file
[dir name]
(def path (string dir "/" name))
(spit path "test text"))
# 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.
# Create a file watcher on two test directories
(def fw (filewatch/new chan))
(def td1 (randdir))
(def td2 (randdir))
(def td3 (randdir))
(rmrf td1)
(rmrf td2)
(os/mkdir td1)
(os/mkdir td2)
(os/mkdir td3)
(spit-file td3 "file3.txt")
(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
(filewatch/add fw (string td3 "/file3.txt") :close-write :create :delete)
(filewatch/add fw td1 :close-write :create :delete)
(filewatch/add fw td2 :close-write :create :delete :ignored))
(assert-no-error "filewatch/listen no error" (filewatch/listen fw))
#
# Windows file writing
#
(when is-win
(spit-file td1 "file1.txt")
(expect :type :added :file-name "file1.txt" :dir-name td1)
(expect :type :modified)
(expect-maybe :type :modified) # for mingw + wine
(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)
(expect-maybe :type :modified)
# 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)
(expect-maybe :type :modified)
(gccollect)
(spit-file td1 "file1.txt")
(expect :type :modified)
(expect :type :modified)
(expect-maybe :type :modified)
(gccollect))
#
# Linux file writing
#
(when is-linux
(spit-file td1 "file1.txt")
(expect :type :create :file-name "file1.txt" :dir-name td1)
(expect :type :close-write)
(expect-empty)
(gccollect)
(spit-file td1 "file1.txt")
(expect :type :close-write)
(expect-empty)
(gccollect)
# Check file3.txt
(spit-file td3 "file3.txt")
(expect :type :close-write :file-name "file3.txt" :dir-name td3)
(expect-empty)
# 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))
(assert-no-error "filewatch/unlisten no error" (filewatch/unlisten fw))
(assert-no-error "cleanup 1" (rmrf td1))
(assert-no-error "cleanup 2" (rmrf td2))
(assert-no-error "cleanup 3" (rmrf td3))
(end-suite)