-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_test.go
85 lines (67 loc) · 1.88 KB
/
find_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
package watchman
import (
"io/ioutil"
"testing"
)
func TestFind(t *testing.T) {
cl := &Client{Sockname: sock}
t.Run("empty path", func(t *testing.T) {
_, err := cl.Find("")
expectErrEqual(t, err, "invalid argument")
})
t.Run("invalid path", func(t *testing.T) {
_, err := cl.Find("/stupid dumb i guess this could exist")
expectErrRegex(t, err, "^lstat .*: no such file or directory$")
})
t.Run("not watched", func(t *testing.T) {
path, err := ioutil.TempDir("", "watchmantest")
if err != nil {
t.Fatalf("Error creating temp dir %s", err)
}
_, err = cl.Find(path)
expectErrRegex(t, err, "^unable to resolve root .*: directory .* is not watched$")
})
t.Run("success - no patterns", func(t *testing.T) {
path, err := ioutil.TempDir("", "watchmantest")
if err != nil {
t.Fatalf("Error creating temp dir %s", err)
}
w, err := cl.Watch(path)
if err != nil {
t.Fatalf("Error watching path %s: %s", path, err)
}
defer cl.WatchDel(w.Watch)
files, err := cl.Find(path)
if err != nil {
t.Errorf("Unexpected error calling find: %s", err)
}
if files.Clock == "" {
t.Errorf("Expected non-empty Clock field")
}
if len(files.Files) > 0 {
t.Errorf("Expected no files, found %d", len(files.Files))
}
})
t.Run("success", func(t *testing.T) {
path, err := ioutil.TempDir("", "watchmantest")
if err != nil {
t.Fatalf("Error creating temp dir %s", err)
}
err = ioutil.WriteFile(path+"/hey.txt", []byte("hey"), 0700)
if err != nil {
t.Fatalf("Error creating temp dir %s", err)
}
w, err := cl.Watch(path)
if err != nil {
t.Fatalf("Error watching path %s: %s", path, err)
}
defer cl.WatchDel(w.Watch)
files, err := cl.Find(path, "*.txt")
if err != nil {
t.Fatalf("Unexpected error calling find: %s", err)
}
if len(files.Files) != 1 {
t.Errorf("Expected to find one file, found %d", len(files.Files))
}
})
}