-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwatcher_test.go
87 lines (70 loc) · 1.76 KB
/
watcher_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
package fsevents
import "testing"
import "github.com/sdegutis/go.assert"
import (
"os"
"path/filepath"
"time"
)
func withCreate(action func(string)) {
dummyfile := "dummyfile.txt"
os.Create(dummyfile)
action(dummyfile)
os.Remove(dummyfile)
}
func TestFileChanges(t *testing.T) {
ch := WatchPaths([]string{"."})
withCreate(func(dummyfile string) {
select {
case <-ch:
case <-time.After(time.Second * 1):
t.Errorf("should have got some file event, but timed out")
}
})
}
func TestEventFlags(t *testing.T) {
ch := WatchPaths([]string{"."})
withCreate(func(dummyfile string) {
select {
case events := <-ch:
assert.Equals(t, len(events), 1)
assert.True(t, events[0].Flags&FlagItemCreated != 0)
case <-time.After(time.Second * 1):
t.Errorf("should have got some file event, but timed out")
}
})
}
func TestCanGetPath(t *testing.T) {
ch := WatchPaths([]string{"."})
withCreate(func(dummyfile string) {
select {
case events := <-ch:
assert.Equals(t, len(events), 1)
fullpath, _ := filepath.Abs(dummyfile)
assert.Equals(t, events[0].Path, fullpath)
case <-time.After(time.Second * 2):
t.Errorf("timed out")
}
})
}
func TestOnlyWatchesSpecifiedPaths(t *testing.T) {
ch := WatchPaths([]string{"imaginaryfile"})
withCreate(func(dummyfile string) {
select {
case <-ch:
t.Errorf("should have timed out, but got some file event")
case <-time.After(time.Second * 1):
}
})
}
func TestCanUnwatch(t *testing.T) {
ch := WatchPaths([]string{"."})
Unwatch(ch)
withCreate(func(dummyfile string) {
select {
case <-ch:
t.Errorf("should have timed out, but got some file event")
case <-time.After(time.Second * 1):
}
})
}