-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnotify_test.go
158 lines (131 loc) · 3.12 KB
/
rnotify_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
package rnotify_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/fsnotify/fsnotify"
"github.com/y-yagi/rnotify"
)
func TestWatchDirRecursive(t *testing.T) {
tempDir, err := os.MkdirTemp("", "rnotify_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
if err := os.Mkdir(filepath.Join(tempDir, "foo"), 0777); err != nil {
t.Fatal(err)
}
if tempDir, err = filepath.EvalSymlinks(tempDir); err != nil {
t.Fatal(err)
}
watcher, _ := rnotify.NewWatcher()
defer watcher.Close()
done := make(chan bool)
fileA := filepath.Join(tempDir, "a")
dirB := filepath.Join(tempDir, "b")
fileC := filepath.Join(tempDir, "foo", "c")
dirD := filepath.Join(tempDir, "foo", "d")
fileE := filepath.Join(tempDir, "foo", "d", "e")
events := map[string]fsnotify.Op{
fileA: fsnotify.Create,
dirB: fsnotify.Create,
fileC: fsnotify.Create,
dirD: fsnotify.Create,
fileE: fsnotify.Create,
dirB: fsnotify.Remove,
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if op, found := events[event.Name]; found && op == event.Op&op {
delete(events, event.Name)
}
if len(events) == 0 {
done <- true
}
case <-time.After(3 * time.Second):
done <- true
}
}
}()
if err = watcher.Add(tempDir); err != nil {
t.Fatal(err)
}
os.WriteFile(fileA, []byte{'a'}, 0600)
os.Mkdir(dirB, 0777)
os.WriteFile(fileC, []byte{'c'}, 0600)
os.Mkdir(dirD, 0777)
time.Sleep(100 * time.Millisecond)
os.WriteFile(fileE, []byte{'e'}, 0600)
os.RemoveAll(dirB)
<-done
if len(events) != 0 {
t.Fatalf("%+v events didn't occur", events)
}
}
func TestIgnore(t *testing.T) {
tempDir, err := os.MkdirTemp("", "rnotify_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
if err := os.Mkdir(filepath.Join(tempDir, "foo"), 0777); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(tempDir, ".git"), 0777); err != nil {
t.Fatal(err)
}
if tempDir, err = filepath.EvalSymlinks(tempDir); err != nil {
t.Fatal(err)
}
watcher, _ := rnotify.NewWatcher()
watcher.Ignore([]string{".git"})
defer watcher.Close()
done := make(chan bool)
fileA := filepath.Join(tempDir, "a")
fileB := filepath.Join(tempDir, "foo", "b")
fileC := filepath.Join(tempDir, ".git", "c")
events := map[string]fsnotify.Op{
fileA: fsnotify.Create,
fileB: fsnotify.Create,
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if op, found := events[event.Name]; found && op == event.Op&op {
delete(events, event.Name)
} else if strings.Contains(event.Name, ".git") {
fmt.Printf("unexpected %+v occurred\n", event)
done <- true
}
if len(events) == 0 {
done <- true
}
case <-time.After(3 * time.Second):
done <- true
}
}
}()
if err = watcher.Add(tempDir); err != nil {
t.Fatal(err)
}
os.WriteFile(fileC, []byte{'c'}, 0600)
os.WriteFile(fileA, []byte{'a'}, 0600)
os.WriteFile(fileB, []byte{'b'}, 0600)
time.Sleep(100 * time.Millisecond)
<-done
if len(events) != 0 {
t.Fatalf("%+v events didn't occur", events)
}
}