-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsevent_monitor_test.go
137 lines (118 loc) · 3.38 KB
/
fsevent_monitor_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
// +build darwin
package piaas
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/sohoffice/piaas/stringarrays"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
)
func TestFSEventMonitor(t *testing.T) {
tempDir := prepareFSeventMonitorTestDir(t)
defer os.RemoveAll(tempDir)
completed := make(chan bool)
fsm := NewFSEventMonitor(tempDir)
var mon Monitor = &fsm
collected := make([]string, 0)
collectedCh := make(chan []string)
go func() {
events := <-collectedCh
collected = append(collected, events...)
// start validating the test results
completed <- true
}()
mon.Subscribe(collectedCh)
mon.Start(600)
expected := []string{
pathConvert(path.Join(tempDir, "dir-to-delete")),
pathConvert(path.Join(tempDir, "foo", "file-to-delete")),
}
// create a new directory
mkDir(t, path.Join(tempDir, "dir"))
// add a file in the newly created directory
touchFile(t, path.Join(tempDir, "dir", "foo"))
// delete a directory
removeFile(t, path.Join(tempDir, "dir-to-delete"))
// delete a file
removeFile(t, path.Join(tempDir, "foo", "file-to-delete"))
<-completed
log.Debugf("Collected FSEvents:\n%s\n", stringarrays.ToString(collected))
expected = append(expected, path.Join(tempDir, "dir"), path.Join(tempDir, "dir", "foo"))
validate(t, collected, expected, []string{}, pathConvert)
}
func validate(t *testing.T, all []string, positives []string, negatives []string, conv func(string) string) {
errFlag := false
for _, s := range positives {
s = conv(s)
if stringarrays.IndexOf(all, s) == -1 {
errFlag = true
t.Errorf("Expected %s to exists, but was not.", s)
}
}
for _, s := range negatives {
s = conv(s)
if stringarrays.IndexOf(all, s) != -1 {
errFlag = true
t.Errorf("Unexpected element %s.", s)
}
}
if errFlag {
fmt.Fprintf(os.Stderr, stringarrays.ToString(all))
}
}
func pathConvert(path string) string {
evaluated, err := filepath.EvalSymlinks(path)
if err != nil {
evaluated = path
}
return evaluated
}
func mkDir(t *testing.T, path string) {
err := os.MkdirAll(path, 0700)
if err != nil {
t.Fatalf("Error creating test directory %s: %s", path, err)
}
}
func touchFile(t *testing.T, path string) {
var bytes []byte
writeFile(t, path, &bytes)
}
// write something to file
func writeFile(t *testing.T, path string, bytes *[]byte) {
err := ioutil.WriteFile(path, *bytes, 0644)
if err != nil {
t.Fatalf("Error touching file %s: %s", path, err)
}
}
func removeFile(t *testing.T, path string) {
err := os.RemoveAll(path)
if err != nil {
t.Fatalf("Error deleting file %s: %s", path, err)
}
}
func rename(t *testing.T, oldName string, newName string) {
err := os.Rename(oldName, newName)
if err != nil {
t.Fatalf("Error renaming file %s to %s: %s", oldName, newName, err)
}
}
func prepareFSeventMonitorTestDir(t *testing.T) string {
tempDir, err := ioutil.TempDir("", "fsevents")
if err != nil {
t.Fatalf("Error creating temp dir: %s", err)
}
// Create the test tree hierarchy
mkDir(t, path.Join(tempDir, "foo", "bar"))
mkDir(t, path.Join(tempDir, "foo", "baz"))
touchFile(t, path.Join(tempDir, "foo", "file-to-delete"))
touchFile(t, path.Join(tempDir, "foo", "file-to-rename"))
mkDir(t, path.Join(tempDir, "dir-to-delete"))
mkDir(t, path.Join(tempDir, "dir-to-rename"))
mkDir(t, path.Join(tempDir, "foo1"))
touchFile(t, path.Join(tempDir, "foo_file"))
touchFile(t, path.Join(tempDir, "foo", "abc"))
return tempDir
}