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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// Package filenotify is adapted from https://github.com/moby/moby/tree/master/pkg/filenotify, Apache-2.0 License.
// Hopefully this can be replaced with an external package sometime in the future, see https://github.com/fsnotify/fsnotify/issues/9
package filenotify
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"time"
qt "github.com/frankban/quicktest"
"github.com/fsnotify/fsnotify"
"github.com/gohugoio/hugo/htesting"
)
const (
subdir1 = "subdir1"
subdir2 = "subdir2"
watchWaitTime = 200 * time.Millisecond
)
var (
isMacOs = runtime.GOOS == "darwin"
isWindows = runtime.GOOS == "windows"
isCI = htesting.IsCI()
)
func TestPollerAddRemove(t *testing.T) {
c := qt.New(t)
w := NewPollingWatcher(watchWaitTime)
c.Assert(w.Add("foo"), qt.Not(qt.IsNil))
c.Assert(w.Remove("foo"), qt.Not(qt.IsNil))
f, err := os.CreateTemp("", "asdf")
if err != nil {
t.Fatal(err)
}
c.Cleanup(func() {
c.Assert(w.Close(), qt.IsNil)
os.Remove(f.Name())
})
c.Assert(w.Add(f.Name()), qt.IsNil)
c.Assert(w.Remove(f.Name()), qt.IsNil)
}
func TestPollerEvent(t *testing.T) {
c := qt.New(t)
for _, poll := range []bool{true, false} {
if !(poll || isMacOs) || isCI {
// Only run the fsnotify tests on MacOS locally.
continue
}
method := "fsnotify"
if poll {
method = "poll"
}
c.Run(fmt.Sprintf("%s, Watch dir", method), func(c *qt.C) {
dir, w := preparePollTest(c, poll)
subdir := filepath.Join(dir, subdir1)
c.Assert(w.Add(subdir), qt.IsNil)
filename := filepath.Join(subdir, "file1")
// Write to one file.
c.Assert(os.WriteFile(filename, []byte("changed"), 0600), qt.IsNil)
var expected []fsnotify.Event
if poll {
expected = append(expected, fsnotify.Event{Name: filename, Op: fsnotify.Write})
assertEvents(c, w, expected...)
} else {
// fsnotify sometimes emits Chmod before Write,
// which is hard to test, so skip it here.
drainEvents(c, w)
}
// Remove one file.
filename = filepath.Join(subdir, "file2")
c.Assert(os.Remove(filename), qt.IsNil)
assertEvents(c, w, fsnotify.Event{Name: filename, Op: fsnotify.Remove})
// Add one file.
filename = filepath.Join(subdir, "file3")
c.Assert(os.WriteFile(filename, []byte("new"), 0600), qt.IsNil)
assertEvents(c, w, fsnotify.Event{Name: filename, Op: fsnotify.Create})
// Remove entire directory.
subdir = filepath.Join(dir, subdir2)
c.Assert(w.Add(subdir), qt.IsNil)
c.Assert(os.RemoveAll(subdir), qt.IsNil)
expected = expected[:0]
// This looks like a bug in fsnotify on MacOS. There are
// 3 files in this directory, yet we get Remove events
// for one of them + the directory.
if !poll {
expected = append(expected, fsnotify.Event{Name: filepath.Join(subdir, "file2"), Op: fsnotify.Remove})
}
expected = append(expected, fsnotify.Event{Name: subdir, Op: fsnotify.Remove})
assertEvents(c, w, expected...)
})
c.Run(fmt.Sprintf("%s, Add should not trigger event", method), func(c *qt.C) {
dir, w := preparePollTest(c, poll)
subdir := filepath.Join(dir, subdir1)
w.Add(subdir)
assertEvents(c, w)
// Create a new sub directory and add it to the watcher.
subdir = filepath.Join(dir, subdir1, subdir2)
c.Assert(os.Mkdir(subdir, 0777), qt.IsNil)
w.Add(subdir)
// This should create only one event.
assertEvents(c, w, fsnotify.Event{Name: subdir, Op: fsnotify.Create})
})
}
}
func TestPollerClose(t *testing.T) {
c := qt.New(t)
w := NewPollingWatcher(watchWaitTime)
f1, err := os.CreateTemp("", "f1")
c.Assert(err, qt.IsNil)
defer os.Remove(f1.Name())
f2, err := os.CreateTemp("", "f2")
c.Assert(err, qt.IsNil)
filename1 := f1.Name()
filename2 := f2.Name()
f1.Close()
f2.Close()
c.Assert(w.Add(filename1), qt.IsNil)
c.Assert(w.Add(filename2), qt.IsNil)
c.Assert(w.Close(), qt.IsNil)
c.Assert(w.Close(), qt.IsNil)
c.Assert(os.WriteFile(filename1, []byte("new"), 0600), qt.IsNil)
c.Assert(os.WriteFile(filename2, []byte("new"), 0600), qt.IsNil)
// No more event as the watchers are closed.
assertEvents(c, w)
f2, err = os.CreateTemp("", "f2")
c.Assert(err, qt.IsNil)
defer os.Remove(f2.Name())
c.Assert(w.Add(f2.Name()), qt.Not(qt.IsNil))
}
func TestCheckChange(t *testing.T) {
c := qt.New(t)
dir := prepareTestDirWithSomeFiles(c, "check-change")
stat := func(s ...string) os.FileInfo {
fi, err := os.Stat(filepath.Join(append([]string{dir}, s...
|