-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_test.go
73 lines (59 loc) · 1.3 KB
/
copy_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
package thuder
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/spf13/afero"
)
func TestCopy(t *testing.T) {
fs2 := fs
defer func() { fs = fs2 }()
afs := afero.NewMemMapFs()
fs = wrapAfero(afs)
source := "/source/"
dest := "/dest/"
fm := os.FileMode(0777)
fs.MkdirAll(source, fm)
fs.MkdirAll(dest, fm)
makeFile := func(name string) {
err := afero.WriteFile(afs, source+name, []byte(name), fm)
if err != nil {
t.Fatal(err)
}
}
makeFile("a")
makeFile("a1")
makeFile("not-a")
makeFile("a-dir/a/a")
makeFile("x/a")
type cs struct {
rx string
size int64
}
testCases := []cs{
{rx: "^a", size: 368}, //copy everything but not-a and x/a
{rx: "^.$", size: 368 + 42 + 3}, //update a, add x/a
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("copy %v", tc.rx), func(t *testing.T) {
err := Copy(source, dest, []Filter{
{Allow: true, NameRx: tc.rx},
})
if err != nil {
t.Fatal(err)
}
var totalSize int64
allfiles := bytes.NewBuffer(nil)
afero.Walk(afs, "/", func(path string, info os.FileInfo, err error) error {
fmt.Fprintln(allfiles, path, info.Size())
totalSize += info.Size()
return nil
})
if totalSize != tc.size {
t.Log(allfiles)
t.Fatal("wrong size of total files at the end, got", totalSize)
}
})
}
}