-
Notifications
You must be signed in to change notification settings - Fork 4
/
dir_test.go
77 lines (60 loc) · 1.46 KB
/
dir_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
package watchgopher
import (
// "github.com/howeyc/fsnotify"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
var (
_, filename, _, _ = runtime.Caller(0)
fixtures = filepath.Dir(filename) + "/fixtures"
)
func TestFiles(t *testing.T) {
dir := NewDir(fixtures)
err := dir.Listen()
checkErr(t, err)
_, ok := dir.Files[fixtures+"/foobar.txt"]
if !ok {
t.Errorf("dir.Files does not include right files")
}
_, ok = dir.Files[fixtures+"/hello_world.txt"]
if !ok {
t.Errorf("dir.Files does not include right files")
}
}
func TestEvents(t *testing.T) {
testfilepath := fixtures + "/testfile.txt"
dir := NewDir(fixtures)
err := dir.Listen()
checkErr(t, err)
err = ioutil.WriteFile(testfilepath, []byte("Hello World!"), 0644)
checkErr(t, err)
createev := <-dir.Events
if createev.Name != testfilepath && !createev.IsCreate() {
t.Fatal("Did not receive the right event")
}
time.Sleep(50 * time.Millisecond)
_, ok := dir.Files[testfilepath]
if !ok {
t.Errorf("Did not add the created file to dir.Files")
}
err = os.Remove(testfilepath)
checkErr(t, err)
time.Sleep(50 * time.Millisecond)
deleteev := <-dir.Events
if deleteev.Name != testfilepath && !deleteev.IsDelete() {
t.Fatal("Did not receive the right event")
}
file, ok := dir.Files[testfilepath]
if file != nil && ok {
t.Errorf("Did not remove the deleted file from dir.Files")
}
}
func checkErr(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}