-
Notifications
You must be signed in to change notification settings - Fork 1
/
tar_test.go
138 lines (122 loc) · 4.38 KB
/
tar_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
package building
import (
"fmt"
"path/filepath"
"testing"
"gotest.tools/assert"
"gotest.tools/fs"
)
func TestTarTree(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.tar")
err := compress(Tar{}, 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 = untarFiles(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.tar", "", 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 TestTarTreeWithGlob(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.tar")
err := compress(Tar{}, nil, -1, dst, fileset{dir: rootDirectory.Path(), includes: []string{"source/*"}})
assert.NilError(t, err)
err = untarFiles(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.tar", "", 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 TestTarTreeWithEmptyGlob(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.tar")
src := "source/non-existing*"
err := compress(Tar{}, 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 TestGzipTarTree(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.tar.gz")
err := compress(Tar{}, nil, -1, dst,
fileset{dir: rootDirectory.Path(), includes: []string{"source/foo.txt", "source/bar"}})
assert.NilError(t, err)
err = untarFiles(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.tar.gz", "", 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 TestTarTreeWithExcludes(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.tar")
err := compress(Tar{}, nil, -1, dst, fileset{dir: rootDirectory.Path(), includes: []string{"*"}, excludes: []string{"*/foo*"}})
assert.NilError(t, err)
err = untarFiles(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.tar", "", fs.MatchAnyFileContent),
fs.WithDir("source",
fs.WithDir("bar",
fs.WithFile("bar.txt", "bar")))))
assert.Assert(t, fs.Equal(rootDirectory.Path(), expected))
}