-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
all_test.go
478 lines (408 loc) · 14.6 KB
/
all_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package copy
import (
"embed"
"errors"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
. "github.com/otiai10/mint"
)
//go:embed test/data/case18/assets
var assets embed.FS
func TestMain(m *testing.M) {
setup(m)
code := m.Run()
teardown(m)
os.Exit(code)
}
func teardown(m *testing.M) {
os.RemoveAll("test/data/case03/case01")
os.Remove("test/data/case03/relative_foo")
os.RemoveAll("test/data.copy")
os.RemoveAll("test/data.copyTime")
os.RemoveAll("test/owned-by-root") // Do not check the error ;)
Copy("test/data/case18/assets.backup", "test/data/case18/assets")
os.RemoveAll("test/data/case18/assets.backup")
}
func TestCopy(t *testing.T) {
err := Copy("./test/data/case00", "./test/data.copy/case00")
Expect(t, err).ToBe(nil)
info, err := os.Stat("./test/data.copy/case00/README.md")
Expect(t, err).ToBe(nil)
Expect(t, info).Not().ToBe(nil)
Expect(t, info.IsDir()).ToBe(false)
When(t, "specified src doesn't exist", func(t *testing.T) {
err := Copy("NOT/EXISTING/SOURCE/PATH", "anywhere")
Expect(t, err).Not().ToBe(nil)
})
When(t, "specified src is just a file", func(t *testing.T) {
err := Copy("test/data/case01/README.md", "test/data.copy/case01/README.md")
Expect(t, err).ToBe(nil)
content, err := os.ReadFile("test/data.copy/case01/README.md")
Expect(t, err).ToBe(nil)
Expect(t, string(content)).ToBe("case01 - README.md")
})
When(t, "source directory includes symbolic link", func(t *testing.T) {
err := Copy("test/data/case03", "test/data.copy/case03")
Expect(t, err).ToBe(nil)
info, err := os.Lstat("test/data.copy/case03/case01")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeSymlink).Not().ToBe(0)
})
When(t, "try to copy to an existing path", func(t *testing.T) {
err := Copy("test/data/case03", "test/data.copy/case03")
Expect(t, err).ToBe(nil)
})
When(t, "try to copy READ-not-allowed source", func(t *testing.T) {
err := Copy("test/data/doesNotExist", "test/data.copy/doesNotExist")
Expect(t, err).Not().ToBe(nil)
})
When(t, "try to copy a file to existing path", func(t *testing.T) {
err := Copy("test/data/case04/README.md", "test/data/case04")
Expect(t, err).Not().ToBe(nil)
err = Copy("test/data/case04/README.md", "test/data/case04/README.md/foobar")
Expect(t, err).Not().ToBe(nil)
})
When(t, "try to copy a directory that has no write permission and copy file inside along with it", func(t *testing.T) {
src := "test/data/case05"
dest := "test/data.copy/case05"
err := os.Chmod(src, os.FileMode(0o555))
Expect(t, err).ToBe(nil)
err = Copy(src, dest)
Expect(t, err).ToBe(nil)
info, err := os.Lstat(dest)
Expect(t, err).ToBe(nil)
Expect(t, info.Mode().Perm()).ToBe(os.FileMode(0o555))
err = os.Chmod(dest, 0o755)
Expect(t, err).ToBe(nil)
})
When(t, "file is deleted while copying", func(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()
file := filepath.Join(src, "file")
f, err := os.Create(file)
Expect(t, err).ToBe(nil)
f.Close()
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
os.Remove(src)
return false, nil
}}
err = Copy(src, dest, opt)
Expect(t, err).ToBe(nil)
})
When(t, "symlink is deleted while copying", func(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()
Expect(t, os.Symlink(".", filepath.Join(src, "symlink"))).ToBe(nil)
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
os.Remove(src)
return false, nil
}}
err = Copy(src, dest, opt)
Expect(t, err).ToBe(nil)
})
When(t, "directory is deleted while copying", func(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()
Expect(t, os.Mkdir(filepath.Join(src, "dir"), 0755)).ToBe(nil)
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
os.Remove(src)
return false, nil
}}
err = Copy(src, dest, opt)
Expect(t, err).ToBe(nil)
})
}
func TestCopy_NamedPipe(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "js" {
t.Skip("See https://github.com/otiai10/copy/issues/47")
}
When(t, "specified src contains a folder with a named pipe", func(t *testing.T) {
dest := "test/data.copy/case11"
err := Copy("test/data/case11", dest)
Expect(t, err).ToBe(nil)
info, err := os.Lstat("test/data/case11/foo/bar")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeNamedPipe != 0).ToBe(true)
Expect(t, info.Mode().Perm()).ToBe(os.FileMode(0o555))
})
When(t, "specified src is a named pipe", func(t *testing.T) {
dest := "test/data.copy/case11/foo/bar.named"
err := Copy("test/data/case11/foo/bar", dest)
Expect(t, err).ToBe(nil)
info, err := os.Lstat(dest)
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()&os.ModeNamedPipe != 0).ToBe(true)
Expect(t, info.Mode().Perm()).ToBe(os.FileMode(0o555))
})
}
func TestOptions_Skip(t *testing.T) {
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
switch {
case strings.HasSuffix(src, "_skip"):
return true, nil
case strings.HasSuffix(src, ".gitfake"):
return true, nil
default:
return false, nil
}
}}
err := Copy("test/data/case06", "test/data.copy/case06", opt)
Expect(t, err).ToBe(nil)
info, err := os.Stat("./test/data.copy/case06/dir_skip")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)
info, err = os.Stat("./test/data.copy/case06/file_skip")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)
info, err = os.Stat("./test/data.copy/case06/README.md")
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)
info, err = os.Stat("./test/data.copy/case06/repo/.gitfake")
Expect(t, info).ToBe(nil)
Expect(t, os.IsNotExist(err)).ToBe(true)
info, err = os.Stat("./test/data.copy/case06/repo/README.md")
Expect(t, info).Not().ToBe(nil)
Expect(t, err).ToBe(nil)
Because(t, "if Skip func returns error, Copy should be interrupted", func(t *testing.T) {
errInsideSkipFunc := errors.New("Something wrong inside Skip")
opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
return false, errInsideSkipFunc
}}
err := Copy("test/data/case06", "test/data.copy/case06.01", opt)
Expect(t, err).ToBe(errInsideSkipFunc)
files, err := os.ReadDir("./test/data.copy/case06.01")
Expect(t, err).ToBe(nil)
Expect(t, len(files)).ToBe(0)
})
}
func TestOptions_Specials(t *testing.T) {
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
err := Copy("/dev/null", "test/data.copy/dev-null", Options{Specials: false})
Expect(t, err).ToBe(nil)
}
}
func TestOptions_PermissionControl(t *testing.T) {
info, err := os.Stat("test/data/case07/dir_0555")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0o555) | os.ModeDir)
info, err = os.Stat("test/data/case07/file_0444")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0o444))
opt := Options{PermissionControl: AddPermission(0o222)}
err = Copy("test/data/case07", "test/data.copy/case07", opt)
Expect(t, err).ToBe(nil)
info, err = os.Stat("test/data.copy/case07/dir_0555")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0o555|0o222) | os.ModeDir)
info, err = os.Stat("test/data.copy/case07/file_0444")
Expect(t, err).ToBe(nil)
Expect(t, info.Mode()).ToBe(os.FileMode(0o444 | 0o222))
When(t, "try to copy a dir owned by other users", func(t *testing.T) {
err := Copy("test/data/case14", "test/owned-by-root", Options{
PermissionControl: DoNothing, // ONLY docker tests fail when you comment out this line
})
Expect(t, err).ToBe(nil)
})
}
func TestOptions_Sync(t *testing.T) {
// With Sync option, each file will be flushed to storage on copying.
// TODO: Since it's a bit hard to simulate real usecases here. This testcase is nonsense.
opt := Options{Sync: true}
err := Copy("test/data/case08", "test/data.copy/case08", opt)
Expect(t, err).ToBe(nil)
}
func TestOptions_PreserveTimes(t *testing.T) {
err := Copy("test/data/case09", "test/data.copy/case09")
Expect(t, err).ToBe(nil)
opt := Options{PreserveTimes: true}
err = Copy("test/data/case09", "test/data.copy/case09-preservetimes", opt)
Expect(t, err).ToBe(nil)
for _, entry := range []string{"", "README.md", "symlink"} {
orig, err := os.Stat("test/data/case09/" + entry)
Expect(t, err).ToBe(nil)
plain, err := os.Stat("test/data.copy/case09/" + entry)
Expect(t, err).ToBe(nil)
preserved, err := os.Stat("test/data.copy/case09-preservetimes/" + entry)
Expect(t, err).ToBe(nil)
Expect(t, plain.ModTime().Unix()).Not().ToBe(orig.ModTime().Unix())
Expect(t, preserved.ModTime().Unix()).ToBe(orig.ModTime().Unix())
}
}
func TestOptions_OnDirExists(t *testing.T) {
err := Copy("test/data/case10/dest", "test/data.copy/case10/dest.1")
Expect(t, err).ToBe(nil)
err = Copy("test/data/case10/dest", "test/data.copy/case10/dest.2")
Expect(t, err).ToBe(nil)
err = Copy("test/data/case10/dest", "test/data.copy/case10/dest.3")
Expect(t, err).ToBe(nil)
opt := Options{}
opt.OnDirExists = func(src, dest string) DirExistsAction {
return Merge
}
err = Copy("test/data/case10/src", "test/data.copy/case10/dest.1", opt)
Expect(t, err).ToBe(nil)
err = Copy("test/data/case10/src", "test/data.copy/case10/dest.1", opt)
Expect(t, err).ToBe(nil)
b, err := os.ReadFile("test/data.copy/case10/dest.1/" + "foo/" + "text_aaa")
Expect(t, err).ToBe(nil)
Expect(t, string(b)).ToBe("This is text_aaa from src")
stat, err := os.Stat("test/data.copy/case10/dest.1/foo/text_eee")
Expect(t, err).ToBe(nil)
Expect(t, stat).Not().ToBe(nil)
opt.OnDirExists = func(src, dest string) DirExistsAction {
return Replace
}
err = Copy("test/data/case10/src", "test/data.copy/case10/dest.2", opt)
Expect(t, err).ToBe(nil)
err = Copy("test/data/case10/src", "test/data.copy/case10/dest.2", opt)
Expect(t, err).ToBe(nil)
b, err = os.ReadFile("test/data.copy/case10/dest.2/" + "foo/" + "text_aaa")
Expect(t, err).ToBe(nil)
Expect(t, string(b)).ToBe("This is text_aaa from src")
stat, err = os.Stat("test/data.copy/case10/dest.2/foo/text_eee")
Expect(t, os.IsNotExist(err)).ToBe(true)
Expect(t, stat).ToBe(nil)
opt.OnDirExists = func(src, dest string) DirExistsAction {
return Untouchable
}
err = Copy("test/data/case10/src", "test/data.copy/case10/dest.3", opt)
Expect(t, err).ToBe(nil)
b, err = os.ReadFile("test/data.copy/case10/dest.3/" + "foo/" + "text_aaa")
Expect(t, err).ToBe(nil)
Expect(t, string(b)).ToBe("This is text_aaa from dest")
When(t, "PreserveTimes is true with Untouchable", func(t *testing.T) {
opt := Options{
OnDirExists: func(src, dest string) DirExistsAction { return Untouchable },
PreserveTimes: true,
}
err = Copy("test/data/case10/src", "test/data.copy/case10/dest.3", opt)
Expect(t, err).ToBe(nil)
})
}
func TestOptions_CopyBufferSize(t *testing.T) {
opt := Options{
CopyBufferSize: 512,
}
err := Copy("test/data/case12", "test/data.copy/case12", opt)
Expect(t, err).ToBe(nil)
content, err := os.ReadFile("test/data.copy/case12/README.md")
Expect(t, err).ToBe(nil)
Expect(t, string(content)).ToBe("case12 - README.md")
}
func TestOptions_PreserveOwner(t *testing.T) {
opt := Options{PreserveOwner: true}
err := Copy("test/data/case13", "test/data.copy/case13", opt)
Expect(t, err).ToBe(nil)
}
func TestOptions_CopyRateLimit(t *testing.T) {
file, err := os.Create("test/data/case16/large.file")
if err != nil {
t.Errorf("failed to create test file: %v", err)
return
}
size := int64(100 * 1024) // 100 KB
if err := file.Truncate(size); err != nil {
t.Errorf("failed to truncate test file: %v", err)
t.SkipNow()
return
}
opt := Options{WrapReader: func(src io.Reader) io.Reader {
return &SleepyReader{src, 1}
}}
start := time.Now()
err = Copy("test/data/case16", "test/data.copy/case16", opt)
elapsed := time.Since(start)
Expect(t, err).ToBe(nil)
Expect(t, elapsed > 5*time.Second).ToBe(true)
}
func TestOptions_OnFileError(t *testing.T) {
opt := Options{
OnError: nil,
}
// existing, process normally
err := Copy("test/data/case17", "test/data.copy/case17", opt)
Expect(t, err).ToBe(nil)
// not existing, process err
err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt)
Expect(t, os.IsNotExist(err)).ToBe(true)
_, err = os.Stat("test/data.copy/case17/non-existing")
Expect(t, os.IsNotExist(err)).ToBe(true)
// existing, nil err not passed
opt.OnError = func(_, _ string, err error) error {
return err
}
err = Copy("test/data/case17", "test/data.copy/case17", opt)
Expect(t, err).ToBe(nil)
// not existing, process err
opt.OnError = func(_, _ string, err error) error { return err }
err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt)
Expect(t, os.IsNotExist(err)).ToBe(true)
_, err = os.Stat("test/data.copy/case17/non-existing")
Expect(t, os.IsNotExist(err)).ToBe(true)
// not existing, ignore err
opt.OnError = func(_, _ string, err error) error { return nil }
err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt)
Expect(t, err).ToBe(nil)
_, err = os.Stat("test/data.copy/case17/non-existing")
Expect(t, os.IsNotExist(err)).ToBe(true)
}
func TestOptions_FS(t *testing.T) {
os.RemoveAll("test/data/case18/assets")
err := Copy("test/data/case18/assets", "test/data.copy/case18/assets", Options{
FS: assets,
PermissionControl: AddPermission(200), // FIXME
})
Expect(t, err).ToBe(nil)
}
type SleepyReader struct {
src io.Reader
sec time.Duration
}
func (r *SleepyReader) Read(p []byte) (int, error) {
n, e := r.src.Read(p)
if e != nil && e != io.EOF {
return n, e
}
if n > 0 {
time.Sleep(time.Second * r.sec)
}
return n, e
}
func TestOptions_NumOfWorkers(t *testing.T) {
opt := Options{NumOfWorkers: 3}
err := Copy("test/data/case19", "test/data.copy/case19", opt)
Expect(t, err).ToBe(nil)
}
func TestOptions_PreferConcurrent(t *testing.T) {
opt := Options{NumOfWorkers: 4, PreferConcurrent: func(sd, dd string) (bool, error) {
return strings.HasSuffix(sd, "concurrent"), nil
}}
err := Copy("test/data/case19", "test/data.copy/case19_preferconcurrent", opt)
Expect(t, err).ToBe(nil)
}
func TestOptions_RenameDestination(t *testing.T) {
opt := Options{
RenameDestination: func(s, d string) (string, error) {
// Your own logic here
if strings.HasSuffix(s, ".template") {
return strings.TrimSuffix(d, ".template"), nil
} else if strings.HasSuffix(s, ".tpl") {
return strings.TrimSuffix(d, ".tpl"), nil
}
return d, nil // otherwise do nothing
},
}
err := Copy("test/data/case20", "test/data.copy/case20", opt)
Expect(t, err).ToBe(nil)
_, err = os.Stat("test/data.copy/case20/foo/main.go.template")
Expect(t, os.IsNotExist(err)).ToBe(true)
_, err = os.Stat("test/data.copy/case20/foo/main.go")
Expect(t, err).ToBe(nil)
_, err = os.Stat("test/data.copy/case20/foo/control.txt")
Expect(t, err).ToBe(nil)
}