-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtar_test.go
189 lines (153 loc) · 4.71 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
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
package xtractr_test
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/dsnet/compress/bzip2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ulikunitz/xz"
"golift.io/xtractr"
)
type compressor interface {
Compress(t *testing.T, sourceDir string, destFileBase string) error
}
type (
tarCompressor struct{}
tarZCompressor struct{}
tarBzipCompressor struct{}
tarXZCompressor struct{}
tarGzipCompressor struct{}
)
func TestTar(t *testing.T) {
t.Parallel()
tests := []struct {
name string
compressor compressor
extension string
}{
{"tar", &tarCompressor{}, "tar"},
{"tarZ", &tarZCompressor{}, "tar.z"},
{"tarBzip", &tarBzipCompressor{}, "tar.bz2"},
{"tarXZ", &tarXZCompressor{}, "tar.xz"},
{"tarGzip", &tarGzipCompressor{}, "tar.gz"},
}
testFilesInfo := createTestFiles(t)
require.NotNil(t, testFilesInfo)
for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
t.Parallel()
archiveBase := filepath.Join(testFilesInfo.dstFilesDir, "archive")
err := test.compressor.Compress(t, testFilesInfo.srcFilesDir, archiveBase)
require.NoError(t, err)
extractDir := filepath.Join(testFilesInfo.dstFilesDir, test.name)
err = os.Mkdir(extractDir, 0o700)
require.NoError(t, err)
size, files, archives, err := xtractr.ExtractFile(&xtractr.XFile{
FilePath: archiveBase + "." + test.extension,
OutputDir: filepath.Clean(extractDir),
FileMode: 0o600,
DirMode: 0o700,
})
require.NoError(t, err)
assert.Equal(t, int64(testFilesInfo.dataSize), size)
assert.Len(t, files, testFilesInfo.fileCount)
assert.Len(t, archives, testFilesInfo.archiveCount)
})
}
}
func writeTar(sourceDir string, destWriter io.Writer) error {
tarWriter := tar.NewWriter(destWriter)
outErr := filepath.Walk(sourceDir, func(path string, info os.FileInfo, _ error) error {
if info.Mode().IsDir() {
return nil
}
relativePath := path[len(sourceDir):]
if len(relativePath) == 0 {
return nil
}
fileReader, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer fileReader.Close()
h, err := tar.FileInfoHeader(info, relativePath)
if err != nil {
return fmt.Errorf("failed to create tar header: %w", err)
}
h.Name = relativePath
if err = tarWriter.WriteHeader(h); err != nil {
return fmt.Errorf("failed to write tar header: %w", err)
}
if _, err := io.Copy(tarWriter, fileReader); err != nil {
return fmt.Errorf("failed to copy file (%s) into tar header: %w", fileReader.Name(), err)
}
return nil
})
if outErr == nil {
return nil
}
return fmt.Errorf("failed to walk source directory: %w", outErr)
}
func (c *tarCompressor) Compress(t *testing.T, sourceDir string, destBase string) error {
t.Helper()
tarFile, err := os.Create(destBase + ".tar")
defer safeCloser(t, tarFile)
require.NoError(t, err)
return writeTar(sourceDir, tarFile)
}
func (c *tarZCompressor) Compress(t *testing.T, _ string, destBase string) error {
t.Helper()
// No native Go library for .tar.Z and compress Unix utility is not available on
// Windows. So, we use a pre-compressed file from test_data.
tarZFilename := destBase + ".tar.z"
tarZDestFile, err := os.Create(tarZFilename)
require.NoError(t, err)
tarZTestFile, err := os.Open("test_data/archive.tar.Z")
require.NoError(t, err)
written, err := io.Copy(tarZDestFile, tarZTestFile)
assert.Greater(t, written, int64(0))
require.NoError(t, err)
return nil
}
func (c *tarBzipCompressor) Compress(t *testing.T, sourceDir string, destBase string) error {
t.Helper()
tarBZ2Filename := destBase + ".tar.bz2"
tarBZ2File, err := os.Create(tarBZ2Filename)
require.NoError(t, err)
bzip2Writer, err := bzip2.NewWriter(tarBZ2File, &bzip2.WriterConfig{Level: bzip2.BestSpeed})
defer safeCloser(t, bzip2Writer)
require.NoError(t, err)
err = writeTar(sourceDir, bzip2Writer)
require.NoError(t, err)
return nil
}
func (c *tarXZCompressor) Compress(t *testing.T, sourceDir string, destBase string) error {
t.Helper()
tarXZFilename := destBase + ".tar.xz"
tarXZFile, err := os.Create(tarXZFilename)
require.NoError(t, err)
xzWriter, err := xz.NewWriter(tarXZFile)
defer safeCloser(t, xzWriter)
require.NoError(t, err)
err = writeTar(sourceDir, xzWriter)
require.NoError(t, err)
return nil
}
func (c *tarGzipCompressor) Compress(t *testing.T, sourceDir string, destBase string) error {
t.Helper()
tarGZFilename := destBase + ".tar.gz"
tarGZFile, err := os.Create(tarGZFilename)
require.NoError(t, err)
gzipWriter := gzip.NewWriter(tarGZFile)
defer gzipWriter.Close()
require.NoError(t, err)
err = writeTar(sourceDir, gzipWriter)
require.NoError(t, err)
return nil
}