-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_test.go
193 lines (176 loc) · 3.49 KB
/
sync_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
"time"
)
func TestRecursive(t *testing.T) {
// Recursive is depth first
input := [][]int{{1, 0, 6}, {0, 4, 5}, {2, 3}}
ch := make(chan File)
go func() {
recursive(File{}, ch, fakeIndex(input))
close(ch)
}()
expN := 1
LOOP:
for {
select {
case f, ok := <-ch:
if !ok {
break LOOP
}
n, _ := strconv.Atoi(f.Path)
if n != expN {
t.Errorf("%d should have been %d", n, expN)
}
expN++
case <-time.After(100 * time.Millisecond):
t.Error("timeout expected:", expN)
break LOOP
}
}
expected := flattenInts(input)
if expN < len(expected) {
t.Error("Missing:", expected[expN:])
}
}
func fakeIndex(nums [][]int) IndexFn {
i := 0
return func(f File, ch chan File) {
for _, n := range nums[i] {
nf := f
nf.Path = strconv.Itoa(n)
if n == 0 {
i++
} else {
nf = nf.SetLeaf()
}
ch <- nf
}
}
}
func flattenInts(ints [][]int) []int {
res := []int{}
for _, g := range ints {
for _, n := range g {
if n == 0 {
continue
}
res = append(res, n)
}
}
return res
}
func TestLocalNew(t *testing.T) {
testLocal(t, func(f File, r io.ReadCloser) File {
return f
}, true)
}
func TestLocalOverwriteOlder(t *testing.T) {
testLocal(t, func(f File, r io.ReadCloser) File {
err := local(f, r)
if err != nil {
t.Error(err)
}
f.Mtime = f.Mtime.Add(time.Second)
return f
}, true)
}
func TestLocalNotOverwriteNewer(t *testing.T) {
testLocal(t, func(f File, r io.ReadCloser) File {
err := local(f, r)
if err != nil {
t.Error(err)
}
f.Mtime = f.Mtime.Add(-time.Second)
return f
}, false)
}
func TestLocalCreateDirs(t *testing.T) {
testLocal(t, func(f File, r io.ReadCloser) File {
f.Path += "a/dir/oh/uh/hi/ho"
return f
}, true)
}
// Trying to overwrite a directory fails
func TestLocalOverwriteDir(t *testing.T) {
tmp, rm := TempDir()
defer rm()
f, r := someTestFile(tmp)
err := os.Mkdir(f.Path, 777)
if err != nil {
t.Fatal(err)
}
err = os.Chtimes(f.Path, time.Now(), time.Now().Add(-time.Hour))
if err != nil {
t.Fatal(err)
}
if local(f, r) == nil {
t.Error("should have failed")
}
}
func testLocal(t *testing.T, init func(File, io.ReadCloser) File, overwrite bool) {
tmp, rm := TempDir()
of, r := someTestFile(tmp)
f := init(of, r)
err := local(f, r)
if err != nil {
t.Error(err)
}
if overwrite {
of = f
}
checkFile(t, of)
filepath.Walk(tmp, func(path string, info os.FileInfo, err error) error {
t.Log(path, info.ModTime(), err)
return nil
})
rm()
}
func someTestFile(tmp string) (File, io.ReadCloser) {
f := File{Mtime: time.Now(), Path: tmp + "/a"}
return f, newFakeCloser("test")
}
func checkFile(t *testing.T, f File) {
st, err := os.Stat(f.Path)
f.Mtime = removeSubSecond(f.Mtime)
if err != nil && os.IsNotExist(err) {
t.Error("File does not exist:", f.Path)
} else {
mtime := removeSubSecond(st.ModTime())
if !(mtime.Equal(f.Mtime)) {
t.Error(mtime, "should be", f.Mtime)
}
}
}
// OSX does not store time resolutions below seconds
func removeSubSecond(in time.Time) time.Time {
return time.Date(
in.Year(),
in.Month(),
in.Day(),
in.Hour(),
in.Minute(),
in.Second(),
0, in.Location())
}
func testTmp(t *testing.T) (name string, stop func()) {
name, err := ioutil.TempDir("", "websync")
if err != nil {
t.Fatal(err)
}
name, err = filepath.EvalSymlinks(name)
if err != nil {
t.Fatal(err)
}
return name, func() {
if err = os.RemoveAll(name); err != nil {
t.Fatal(err)
}
}
}