-
Notifications
You must be signed in to change notification settings - Fork 1
/
zip_test.go
109 lines (96 loc) · 3.44 KB
/
zip_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
package building
import (
"fmt"
"path/filepath"
"testing"
"gotest.tools/assert"
"gotest.tools/fs"
)
func TestZipTree(t *testing.T) {
rootDirectory := fs.NewDir(t, "root",
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))))
defer rootDirectory.Remove()
dst := filepath.Join(rootDirectory.Path(), "destination", "dst.zip")
err := compress(Zip{}, nil, -1, dst,
fileset{dir: rootDirectory.Path() + "/source", includes: []string{"foo.txt"}},
fileset{dir: rootDirectory.Path(), includes: []string{"source/bar"}})
assert.NilError(t, err)
err = unzipFiles(dst, filepath.Join(rootDirectory.Path(), "destination"))
assert.NilError(t, err)
expected := fs.Expected(t,
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))),
fs.WithDir("destination",
fs.WithFile("dst.zip", "", fs.MatchAnyFileContent),
fs.WithFile("foo.txt", "foo"),
fs.WithDir("source",
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar")))))
assert.Assert(t, fs.Equal(rootDirectory.Path(), expected))
}
func TestZipTreeWithGlob(t *testing.T) {
rootDirectory := fs.NewDir(t, "root",
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))))
defer rootDirectory.Remove()
dst := filepath.Join(rootDirectory.Path(), "destination", "dst.zip")
err := compress(Zip{}, nil, -1, dst, fileset{dir: rootDirectory.Path(), includes: []string{"source/*"}})
assert.NilError(t, err)
err = unzipFiles(dst, filepath.Join(rootDirectory.Path(), "destination"))
assert.NilError(t, err)
expected := fs.Expected(t,
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))),
fs.WithDir("destination",
fs.WithFile("dst.zip", "", fs.MatchAnyFileContent),
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar")))))
assert.Assert(t, fs.Equal(rootDirectory.Path(), expected))
}
func TestZipTreeWithEmptyGlob(t *testing.T) {
rootDirectory := fs.NewDir(t, "root",
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))))
defer rootDirectory.Remove()
dst := filepath.Join(rootDirectory.Path(), "destination", "dst.zip")
src := "source/non-existing*"
err := compress(Zip{}, nil, -1, dst, fileset{dir: rootDirectory.Path(), includes: []string{src}})
assert.Error(t, err, fmt.Sprintf("file %q does not exist", filepath.Join(rootDirectory.Path(), src)))
}
func TestZipTreeWithExcludes(t *testing.T) {
rootDirectory := fs.NewDir(t, "root",
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))))
defer rootDirectory.Remove()
dst := filepath.Join(rootDirectory.Path(), "destination", "dst.zip")
err := compress(Zip{}, nil, -1, dst, fileset{dir: rootDirectory.Path(), includes: []string{"*"}, excludes: []string{"*/foo*"}})
assert.NilError(t, err)
err = unzipFiles(dst, filepath.Join(rootDirectory.Path(), "destination"))
assert.NilError(t, err)
expected := fs.Expected(t,
fs.WithDir("source",
fs.WithFile("foo.txt", "foo"),
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar"))),
fs.WithDir("destination",
fs.WithFile("dst.zip", "", fs.MatchAnyFileContent),
fs.WithDir("source",
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar")))))
assert.Assert(t, fs.Equal(rootDirectory.Path(), expected))
}