-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_test.go
186 lines (157 loc) · 4.08 KB
/
archive_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
package archiver
import (
"crypto/sha256"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUntarWithNonTarFile(t *testing.T) {
file, err := os.Open("test-fixtures/tree2/f1.txt")
assert.Nil(t, err)
tempDir, err := ioutil.TempDir("", "TestUntarWithNonTarFile-dir-")
assert.Nil(t, err)
err = Untar(tempDir, file)
assert.NotNil(t, err)
}
func TestTarUntar(t *testing.T) {
tests := []struct {
path string
options []TarOption
numEntries int
ignoredFiles map[string]bool
}{
{
path: "test-fixtures/tree1",
numEntries: 3,
},
{
path: "test-fixtures/tree2",
numEntries: 2,
},
{
path: "test-fixtures/tree3",
numEntries: 0,
},
{
path: "test-fixtures/tree4",
options: []TarOption{HonorGitIgnore()},
numEntries: 4,
ignoredFiles: map[string]bool{
"test-fixtures/tree4/d1/f11.txt": true,
"test-fixtures/tree4/.gitignore": true,
},
},
{
path: "test-fixtures/tree5",
options: []TarOption{ArchiveGitRepo()},
numEntries: 2,
ignoredFiles: map[string]bool{
"test-fixtures/tree5/f1.txt": true,
"test-fixtures/tree5/.git/.holder": true,
"test-fixtures/tree5/.git": true,
"test-fixtures/tree5/.gitignore": true,
},
},
}
for _, test := range tests {
runTestArchiveUnArchive(t, test.path, test.options, test.numEntries, test.ignoredFiles)
}
}
func runTestArchiveUnArchive(t *testing.T, path string, options []TarOption, numEntries int, ignoredFiles map[string]bool) {
file, err := ioutil.TempFile("", "TestArchiveUnArchive-file-")
assert.Nil(t, err)
err = Tar(path, file, options...)
assert.Nil(t, err)
err = file.Close()
assert.Nil(t, err)
tempDir, err := ioutil.TempDir("", "TestArchiveUnArchive-dir-")
assert.Nil(t, err)
file, err = os.Open(file.Name())
assert.Nil(t, err)
defer file.Close()
err = Untar(tempDir, file)
assert.Nil(t, err)
assertFoldersEqual(t, path, tempDir, numEntries, ignoredFiles)
}
func assertFoldersEqual(t *testing.T, dir1 string, dir2 string, numEntries int, ignoredFiles map[string]bool) {
dir1Map, err := walkDir(dir1, ignoredFiles)
assert.Nil(t, err)
dir2Map, err := walkDir(dir2, map[string]bool{})
assert.Nil(t, err)
assert.Equal(t, len(dir1Map), len(dir2Map))
assert.Equal(t, numEntries, len(dir1Map))
for path, fi1 := range dir1Map {
relPath, err := filepath.Rel(dir1, path)
assert.Nil(t, err)
path2 := filepath.Join(dir2, relPath)
fi2, ok := dir2Map[path2]
assert.True(t, ok)
assertFileInfosEqual(t, fi1, fi2)
if !fi1.IsDir() {
absPath1, err := filepath.Abs(filepath.Join(dir1, relPath))
assert.Nil(t, err)
absPath2, err := filepath.Abs(filepath.Join(dir2, relPath))
assert.Nil(t, err)
hash1, err := hashFileContent(absPath1)
assert.Nil(t, err)
hash2, err := hashFileContent(absPath2)
assert.Nil(t, err)
assert.Equal(t, hash1, hash2)
}
}
}
func assertFileInfosEqual(t *testing.T, fi1 os.FileInfo, fi2 os.FileInfo) {
assert.Equal(t, fi1.Mode(), fi2.Mode())
assert.Equal(t, fi1.Name(), fi2.Name())
}
func hashFileContent(path string) (string, error) {
hasher := sha256.New()
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
_, err = io.Copy(hasher, f)
if err != nil {
return "", err
}
return string(hasher.Sum(nil)), nil
}
func walkDir(path string, ignoredFiles map[string]bool) (map[string]os.FileInfo, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
m := map[string]os.FileInfo{}
err = filepath.Walk(path, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.Mode().IsRegular() && !fi.Mode().IsDir() {
return nil
}
absFile, err := filepath.Abs(file)
if err != nil {
return err
}
relPath, err := filepath.Rel(absPath, absFile)
if err != nil {
return err
} else if relPath == "." {
return nil
}
absPath := filepath.Clean(filepath.Join(path, relPath))
m[absPath] = fi
return nil
})
for key := range ignoredFiles {
_, ok := m[key]
if ok {
delete(m, key)
}
}
return m, err
}