-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch_test.go
282 lines (217 loc) · 7.95 KB
/
watch_test.go
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
package path
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"time"
"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/assert"
)
func TestWatchDir(t *testing.T) {
t.Parallel()
var dir = "./testwatchdir"
if _, err := os.Lstat(dir); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(dir, os.ModePerm)
assert.NoError(t, err)
}
var files = make(chan WatchEvent)
var done = make(chan struct{})
var ctx, cancel = context.WithCancel(context.Background())
var regexFilter = NewRegexWatchFilter(regexp.MustCompile(".*.txt$"))
go func() {
var i int
for file := range files {
assert.True(t, strings.HasSuffix(file.AbsolutePath, ".txt"))
i++
}
assert.True(t, i >= 2 && i <= 3) // on linux we will get two CREATE events, on OSX we also get a WRITE event.
close(done)
}()
go func() {
var errors = make(chan error)
go func() {
for err := range errors {
assert.NoError(t, err)
}
}()
WatchDir(ctx, dir, 0, false, files, errors, regexFilter)
}()
time.Sleep(time.Millisecond * 250) // give time for WatchDir to start up
assert.NoError(t, os.WriteFile(filepath.Join(dir, "file1.txt"), []byte{}, fs.ModePerm))
assert.NoError(t, os.WriteFile(filepath.Join(dir, "file1.mp3"), []byte{}, fs.ModePerm))
assert.NoError(t, os.WriteFile(filepath.Join(dir, "file2.txt"), []byte{}, fs.ModePerm))
time.Sleep(time.Millisecond * 250) // give time for WatchDir to process event
cancel()
<-done
assert.NoError(t, os.RemoveAll(dir))
}
func TestWatchDirRecursive(t *testing.T) {
t.Parallel()
var dir = "./testwatchdirrecursive"
if _, err := os.Lstat(dir); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(filepath.Join(dir, "one"), os.ModePerm)
assert.NoError(t, err)
err = os.MkdirAll(filepath.Join(dir, "two"), os.ModePerm)
assert.NoError(t, err)
}
var files = make(chan WatchEvent)
var done = make(chan struct{})
var ctx, cancel = context.WithCancel(context.Background())
var regexFilter = NewRegexWatchFilter(regexp.MustCompile(".*.txt$"))
go func() {
var dirOne, dirTwo int
for file := range files {
assert.True(t, strings.HasSuffix(file.AbsolutePath, ".txt"))
if strings.Contains(file.AbsolutePath, "testwatchdirrecursive/one") || strings.Contains(file.AbsolutePath, `testwatchdirrecursive\one`) {
dirOne++
} else if strings.Contains(file.AbsolutePath, "testwatchdirrecursive/two") || strings.Contains(file.AbsolutePath, `testwatchdirrecursive\two`) {
dirTwo++
}
}
assert.Equal(t, 1, dirOne)
assert.Equal(t, 1, dirTwo)
close(done)
}()
go func() {
var errors = make(chan error)
go func() {
for err := range errors {
assert.NoError(t, err)
}
}()
WatchDir(ctx, dir, 2, false, files, errors, regexFilter)
}()
time.Sleep(time.Millisecond * 250) // give time for WatchDir to start up
assert.NoError(t, os.WriteFile(filepath.Join(filepath.Join(dir, "two"), "file1.txt"), []byte{}, fs.ModePerm))
assert.NoError(t, os.WriteFile(filepath.Join(filepath.Join(dir, "two"), "file1.mp3"), []byte{}, fs.ModePerm))
assert.NoError(t, os.WriteFile(filepath.Join(filepath.Join(dir, "one"), "file2.txt"), []byte{}, fs.ModePerm))
time.Sleep(time.Millisecond * 250) // give time for WatchDir to process event
cancel()
<-done
assert.NoError(t, os.RemoveAll(dir))
}
func TestSkipMapWatchFilter(t *testing.T) {
t.Parallel()
var testFile, err = NewEntry("./testdata/one/file.txt", 0)
assert.NoError(t, err)
var skipMapFilter = NewSkipMapWatchFilter(map[string]struct{}{testFile.AbsolutePath: {}})
accpet, err := skipMapFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.False(t, accpet)
testFileTwo, err := NewEntry("./testdata/one/file.mp3", 0)
assert.NoError(t, err)
accpet, err = skipMapFilter.filter(fsnotify.Event{Name: testFileTwo.AbsolutePath})
assert.NoError(t, err)
assert.True(t, accpet)
accpet, err = skipMapFilter.filter(fsnotify.Event{Name: "filenotexists"})
assert.Error(t, err)
assert.False(t, accpet)
}
func TestDateWatchFilter(t *testing.T) {
t.Parallel()
// set the mod time because in ci/cd the mod time is the time of `git checkout` for the build
// i.e. "now"
var err = os.Chtimes("./testdata/one/file.mp4", time.Date(2022, 06, 01, 0, 0, 0, 0, time.UTC), time.Date(2022, 06, 01, 0, 0, 0, 0, time.UTC))
assert.NoError(t, err)
testFile, err := NewEntry("./testdata/one/file.mp4", 0)
assert.NoError(t, err)
var fromTime = time.Date(2022, 07, 01, 0, 0, 0, 0, time.UTC)
var dateFilter = NewDateWatchFilter(fromTime, time.Now())
accpet, err := dateFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.False(t, accpet)
testFile, err = NewEntry("./testdata/one/file.mp3", 0)
assert.NoError(t, err)
accpet, err = dateFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.True(t, accpet)
accpet, err = dateFilter.filter(fsnotify.Event{Name: "filenotexists"})
assert.Error(t, err)
assert.False(t, accpet)
}
func TestPermissionsWatchFilter(t *testing.T) {
t.Parallel()
// set the perms because the checkout in ci/cd doest match local
assert.NoError(t, os.Chmod("./testdata/one/file.mp3", fs.ModePerm))
var testFile, err = NewEntry("./testdata/one/file.mp3", 0)
assert.NoError(t, err)
var permsFilter = NewPermissionsWatchFilter(uint32(fs.ModePerm), uint32(fs.ModePerm))
accpet, err := permsFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
if runtime.GOOS != "windows" { // i give up trying to figure out how windows does perms
assert.True(t, accpet)
}
testFile, err = NewEntry("./testdata/one/file.mp4", 0)
assert.NoError(t, err)
accpet, err = permsFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.False(t, accpet)
}
func TestSizeWatchFilter(t *testing.T) {
t.Parallel()
var testFile, err = NewEntry("./testdata/one/file.mp4", 0)
assert.NoError(t, err)
var sizeFilter = NewSizeWatchFilter(4000, 6000)
accpet, err := sizeFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.True(t, accpet)
testFile, err = NewEntry("./testdata/one/file.mp3", 0)
assert.NoError(t, err)
accpet, err = sizeFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.False(t, accpet)
testFile, err = NewEntry("./testdata/one", 0)
assert.NoError(t, err)
accpet, err = sizeFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath})
assert.NoError(t, err)
assert.True(t, accpet)
accpet, err = sizeFilter.filter(fsnotify.Event{Name: "filenotexists"})
assert.Error(t, err)
assert.False(t, accpet)
}
func TestOpWatchFilter(t *testing.T) {
t.Parallel()
var testFile, err = NewEntry("./testdata/one/file.mp4", 0)
assert.NoError(t, err)
var opFilter = NewOpWatchFilter(fsnotify.Create)
accpet, err := opFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath, Op: fsnotify.Create})
assert.NoError(t, err)
assert.True(t, accpet)
accpet, err = opFilter.filter(fsnotify.Event{Name: testFile.AbsolutePath, Op: fsnotify.Remove})
assert.NoError(t, err)
assert.False(t, accpet)
}
func TestDirWatchFilter(t *testing.T) {
t.Parallel()
var entry, err = NewEntry("./testdata/one", 1)
assert.NoError(t, err)
var dirFilter = NewDirWatchFilter()
accpet, err := dirFilter.filter(entry)
assert.NoError(t, err)
assert.True(t, accpet)
entry, err = NewEntry("./testdata/ogCGs91VSA5FBjJdgE8eeLSngbebPXyDCICZ7I~tplv-f5insbecw7-1 720 720.jpg", 1)
assert.NoError(t, err)
accpet, err = dirFilter.filter(entry)
assert.NoError(t, err)
assert.False(t, accpet)
}
func TestFileWatchFilter(t *testing.T) {
t.Parallel()
var entry, err = NewEntry("./testdata/one", 1)
assert.NoError(t, err)
var dirFilter = NewFileWatchFilter()
accpet, err := dirFilter.filter(entry)
assert.NoError(t, err)
assert.False(t, accpet)
entry, err = NewEntry("./testdata/ogCGs91VSA5FBjJdgE8eeLSngbebPXyDCICZ7I~tplv-f5insbecw7-1 720 720.jpg", 1)
assert.NoError(t, err)
accpet, err = dirFilter.filter(entry)
assert.NoError(t, err)
assert.True(t, accpet)
}