-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinker_test.go
287 lines (267 loc) · 6.09 KB
/
linker_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"errors"
"os"
"reflect"
"testing"
)
func TestLinker_Move(t *testing.T) {
cases := []struct {
os *FakeOS
oldpath string
newpath string
expectedError error
}{
{
// Failure when IsExists fails
os: &FakeOS{
StatError: errors.New("Some error"),
},
expectedError: errors.New("Some error"),
},
{
// Failure when dest file not exists
os: &FakeOS{
IsNotExistResult: true,
},
oldpath: "/path/dest",
expectedError: errors.New("File /path/dest not exists"),
},
{
// Failure when MkdirAll fails
os: &FakeOS{
IsNotExistResult: false,
MkdirAllError: errors.New("MkdirAll error"),
},
expectedError: errors.New("MkdirAll error"),
},
{
// Failure when Rename fails
os: &FakeOS{
IsNotExistResult: false,
RenameError: errors.New("Rename error"),
},
expectedError: errors.New("Rename error"),
},
{
// Success
os: &FakeOS{
IsNotExistResult: false,
},
expectedError: nil,
},
}
for _, c := range cases {
linker := NewLinker(&FakeOutputer{}, c.os)
err := linker.Move(c.oldpath, c.newpath)
if !reflect.DeepEqual(err, c.expectedError) {
t.Errorf("Expected err to be %v but it was %v\n", c.expectedError, err)
}
}
}
func TestLinker_SetSymlink(t *testing.T) {
cases := []struct {
os *FakeOS
srcAbs string
destAbs string
expectedError error
}{
{
os: &FakeOS{
MkdirAllError: nil,
SymlinkError: nil,
},
srcAbs: "/src/path",
destAbs: "/dest/path",
expectedError: nil,
},
{
os: &FakeOS{
MkdirAllError: errors.New("Permission denied"),
SymlinkError: nil,
},
srcAbs: "/src/path",
destAbs: "/dest/path",
expectedError: errors.New("Permission denied"),
},
{
os: &FakeOS{
MkdirAllError: nil,
SymlinkError: errors.New("File exists"),
},
srcAbs: "/src/path",
destAbs: "/dest/path",
expectedError: errors.New("File exists"),
},
}
for _, c := range cases {
linker := NewLinker(&FakeOutputer{}, c.os)
err := linker.SetSymlink(c.srcAbs, c.destAbs)
if !reflect.DeepEqual(err, c.expectedError) {
t.Errorf("Expected err to be %v but it was %v\n", c.expectedError, err)
}
}
}
func TestLinker_NeedSymlink(t *testing.T) {
cases := []struct {
os *FakeOS
src string
dest string
expectedResult bool
expectedError error
}{
{
os: &FakeOS{
LstatError: os.ErrNotExist,
IsNotExistResult: true,
},
src: "/src/path",
dest: "/some/non-existent/path",
expectedResult: true,
expectedError: nil,
},
{
os: &FakeOS{
LstatError: errors.New("Some error"),
},
src: "/src/path",
dest: "/dest/path",
expectedResult: false,
expectedError: errors.New("Some error"),
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: 0, // regular file
},
},
src: "/src/path",
dest: "/dest/path",
expectedResult: true,
expectedError: nil,
},
{
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: os.ModeSymlink,
},
ReadlinkError: errors.New("Failed to read a link"),
},
src: "/src/path",
dest: "/dest/path",
expectedResult: false,
expectedError: errors.New("Failed to read a link"),
},
{
// dest is already a correct symlink to src
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: os.ModeSymlink,
},
ReadlinkResult: "/src/path",
},
src: "/src/path",
dest: "/dest/path",
expectedResult: false,
expectedError: nil,
},
{
// dest is wrong symlink but failed to remove it
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: os.ModeSymlink,
},
ReadlinkResult: "/some/incorrect/path",
RemoveError: errors.New("Cannot remove"),
},
src: "/src/path",
dest: "/dest/path",
expectedResult: false,
expectedError: errors.New("Cannot remove"),
},
{
// dest is wrong symlink and it removes successfully
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: os.ModeSymlink,
},
ReadlinkResult: "/some/incorrect/path",
RemoveError: nil,
},
src: "/src/path",
dest: "/dest/path",
expectedResult: true,
expectedError: nil,
},
}
for _, c := range cases {
linker := NewLinker(&FakeOutputer{}, c.os)
result, err := linker.NeedSymlink(c.src, c.dest)
if result != c.expectedResult {
t.Errorf("Expected %v but got %v\n", c.expectedResult, result)
}
if !reflect.DeepEqual(err, c.expectedError) {
t.Errorf("Expected err to be %v but it was %v\n", c.expectedError, err)
}
}
}
func TestLinker_NeedBackup(t *testing.T) {
cases := []struct {
os *FakeOS
dest string
expectedResult bool
expectedError error
}{
{
// dest path not exists
os: &FakeOS{
LstatError: os.ErrNotExist,
IsNotExistResult: true,
},
dest: "/some/non-existent/path",
expectedResult: false,
expectedError: nil,
},
{
// Lstat returned error
os: &FakeOS{
LstatError: errors.New("Some error"),
},
dest: "/dest/path",
expectedResult: false,
expectedError: errors.New("Some error"),
},
{
// dest path is not a symlink
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: 0, // regular file
},
},
dest: "/dest/path",
expectedResult: true,
expectedError: nil,
},
{
// dest path is a symlink
os: &FakeOS{
LstatFileInfo: &FakeFileInfo{
ModeValue: os.ModeSymlink,
},
},
dest: "/dest/path",
expectedResult: false,
expectedError: nil,
},
}
for _, c := range cases {
linker := NewLinker(&FakeOutputer{}, c.os)
result, err := linker.NeedBackup(c.dest)
if result != c.expectedResult {
t.Errorf("Expected %v but got %v\n", c.expectedResult, result)
}
if !reflect.DeepEqual(err, c.expectedError) {
t.Errorf("Expected err to be %v but it was %v\n", c.expectedError, err)
}
}
}