-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
144 lines (122 loc) · 3.37 KB
/
client_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
package fsync
import (
"context"
"os"
"reflect"
"testing"
"time"
)
func TestLoadTargetDir(t *testing.T) {
mt, err := LoadTargetDir("./testdir")
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
if len(mt.files) != 2 {
t.Fatalf("results are not equal, got: %v, want: 2", len(mt.files))
}
}
// TestListenTarget covers a case of adding a new file to listen dir
func TestListenTargetOnAdd(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
mt, err := LoadTargetDir("./testdir")
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
done := make(chan bool)
errors := make(chan error, 1)
go func() {
if err := ListenTarget(ctx, mt); err != nil {
errors <- err
}
done <- true
errors <- nil
}()
// bring the change
// create tmp file in testdir
file, err := os.Create("./testdir/newfile.yml")
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
// delete tmp file in testdir in teardown
defer func() {
_ = file.Close()
_ = os.Remove("./testdir/newfile.yml")
}()
<-time.After(2 * time.Second)
if len(mt.files) != 3 {
// error must be called without Fatal which has os.Exit, so we trigger cancel flow
t.Errorf("results are not equal, got: %d, want: 3", len(mt.files))
}
// stop ListTargetDir
cancel()
// wait for go-routine
<-done
if err := <-errors; err != nil {
t.Errorf("want no error, got err: %s", err)
}
}
// helper
func pp(list []*File) []File {
newList := []File{}
for _, file := range list {
newList = append(newList, *file)
}
return newList
}
// TestListenTargetOnDelete
// plan is:
// ensure metadata is correct
// add tmp file to testdir
// get the new file deteced by updateTarget
// verify the change
// remove that file from testdir (simulate user)
// get deleted file detected by updateTarget
// verify the change
func TestUpdateTargetDir(t *testing.T) {
// load metadata
mt, err := LoadTargetDir("./testdir")
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
// ensure metadata is correct
f1 := File{Path: "./testdir", Root: true, Visited: true}
f2 := File{Path: "testdir/testfile.yml", Visited: true}
wantFiles := []*File{&f1, &f2}
if !reflect.DeepEqual(mt.files, wantFiles) {
t.Fatalf("results are not equal, got: %#v, want: %#v", pp(mt.files), pp(wantFiles))
}
// create tmp file in testdir (candidate for delete)
file, err := os.Create("./testdir/tobedeleted.yml")
if err != nil {
t.Fatalf("failed to create tmp file in testdir, got: %s", err)
}
defer func() {
_ = file.Close()
}()
// run UpdateTargetDir to detect a new file
err = UpdateTargetDir(mt)
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
// verify new file is there
f3 := File{Path: "testdir/tobedeleted.yml", Visited: true}
wantFiles = []*File{&f1, &f2, &f3}
if !reflect.DeepEqual(mt.files, wantFiles) {
t.Fatalf("results are not equal, got: %v, want: %v", mt.files, wantFiles)
}
// delete (simulate user)
err = os.Remove("./testdir/tobedeleted.yml")
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
// run UpdateTargetDir to detect the deletion
err = UpdateTargetDir(mt)
if err != nil {
t.Fatalf("want no error, got err: %s", err)
}
// verify no tobedeted.yml in metedata
wantFiles = []*File{&f1, &f2}
if !reflect.DeepEqual(mt.files, wantFiles) {
t.Fatalf("results are not equal, got: %#v, want: %#v", pp(mt.files), pp(wantFiles))
}
}