-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_test.go
229 lines (185 loc) · 6.13 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"archive/tar"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
"github.com/mholt/archiver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_Archive(t *testing.T) {
sizes := []uint64{
1024 * 512, // 500KB
1024 * 1024 * 1, // 1MB
1024 * 1024 * 2, // 2MB
1024 * 1024 * 4, // 4MB
1024 * 1024 * 10, // 10MB
}
for _, maxSize := range sizes {
maxSize := maxSize
t.Run(fmt.Sprintf("it archives a directory up to the specified maximum size - %d", maxSize), func(t *testing.T) {
stdOut, _ := newMockWriters()
archiveFilepath, unarchivedPath := getSingleTestPaths(t, fmt.Sprint(maxSize))
err := Archive(Args{
SourceDir: sourceDir,
ArchiveFile: archiveFilepath,
MaxSize: maxSize,
}, stdOut)
assert.NoError(t, err)
err = archiver.Unarchive(archiveFilepath, unarchivedPath)
assert.NoError(t, err)
var numSourceFiles uint64
assertDir(t, sourceDir, func(t *testing.T, path string, fileInfo os.FileInfo) {
numSourceFiles += 1
})
var totalUntarredSize uint64
var numArchivedFiles uint64
assertDir(t, unarchivedPath, func(t *testing.T, path string, fileInfo os.FileInfo) {
fixtureFilePath := strings.TrimPrefix(path, unarchivedPath)
fixtureFileInfo, err := os.Stat(fixtureFilePath)
assert.NoError(t, err)
assert.Equal(t, fixtureFileInfo.Name(), fileInfo.Name())
assert.Equal(t, fixtureFileInfo.Size(), fileInfo.Size())
assert.Equal(t, fixtureFileInfo.Mode(), fileInfo.Mode())
totalUntarredSize += uint64(fileInfo.Size())
numArchivedFiles += 1
})
assert.True(t, totalUntarredSize <= maxSize)
assert.True(t, numArchivedFiles <= numSourceFiles)
})
}
t.Run("it archives all the files if max size is zero", func(t *testing.T) {
stdOut, _ := newMockWriters()
archiveFilepath, unarchivedPath := getSingleTestPaths(t, "all")
err := Archive(Args{
SourceDir: sourceDir,
ArchiveFile: archiveFilepath,
MaxSize: 0,
}, stdOut)
assert.NoError(t, err)
err = archiver.Unarchive(archiveFilepath, unarchivedPath)
assert.NoError(t, err)
var numExpectedFiles uint64
assertDir(t, sourceDir, func(t *testing.T, path string, fileInfo os.FileInfo) {
numExpectedFiles += 1
})
var numArchivedFiles uint64
assertDir(t, unarchivedPath, func(t *testing.T, path string, fileInfo os.FileInfo) {
fixtureFilePath := strings.TrimPrefix(path, unarchivedPath)
fixtureFileInfo, err := os.Stat(fixtureFilePath)
assert.NoError(t, err)
assert.Equal(t, fixtureFileInfo.Name(), fileInfo.Name())
assert.Equal(t, fixtureFileInfo.Size(), fileInfo.Size())
assert.Equal(t, fixtureFileInfo.Mode(), fileInfo.Mode())
numArchivedFiles += 1
})
assert.Equal(t, numExpectedFiles, numArchivedFiles)
})
t.Run("it preserves the files modified time", func(t *testing.T) {
stdOut, _ := newMockWriters()
archiveFilepath, _ := getSingleTestPaths(t, "mtime")
// set the time of the source files back one week to ease the testing of mtime
oneWeekAgo := time.Now().AddDate(0, 0, -7)
setTimes(t, sourceDir, oneWeekAgo, oneWeekAgo)
err := Archive(Args{
SourceDir: sourceDir,
ArchiveFile: archiveFilepath,
MaxSize: 0,
}, stdOut)
assert.NoError(t, err)
err = archiver.Walk(archiveFilepath, func(f archiver.File) error {
if header, ok := f.Header.(*tar.Header); ok {
filename := header.Name
filename = restoreAbsPath(filename)
fixtureFileInfo, err := os.Stat(filename)
assert.NoError(t, err)
fixtureModTime := fixtureFileInfo.ModTime().Truncate(time.Second)
assert.WithinDuration(t, fixtureModTime, f.ModTime(), 2*time.Second)
assert.WithinDuration(t, fixtureModTime, header.ModTime, 2*time.Second)
} else {
assert.Fail(t, "Wrong header format")
}
return nil
})
assert.NoError(t, err)
})
t.Run("it returns an error is the source dir does not exists", func(t *testing.T) {
stdOut, _ := newMockWriters()
archiveFilepath, _ := getSingleTestPaths(t, "mtime")
err := Archive(Args{
SourceDir: "non_existent",
ArchiveFile: archiveFilepath,
MaxSize: 0,
}, stdOut)
assert.Error(t, err)
})
t.Run("it returns an error if it can't initialise the archive file", func(t *testing.T) {
stdOut, _ := newMockWriters()
archiveFilepath, _ := getSingleTestPaths(t, "dash/prefix")
err := Archive(Args{
SourceDir: sourceDir,
ArchiveFile: archiveFilepath,
MaxSize: 0,
}, stdOut)
assert.Error(t, err)
})
t.Run("it prints the archived files if verbose", func(t *testing.T) {
stdOut, _ := newMockWriters()
archiveFilepath, unarchivedPath := getSingleTestPaths(t, "verbose")
err := Archive(Args{
SourceDir: sourceDir,
ArchiveFile: archiveFilepath,
MaxSize: 1000000,
Verbose: true,
}, stdOut)
assert.NoError(t, err)
err = archiver.Unarchive(archiveFilepath, unarchivedPath)
assert.NoError(t, err)
var numArchivedFiles int
assertDir(t, unarchivedPath, func(t *testing.T, path string, fileInfo os.FileInfo) {
numArchivedFiles += 1
})
assert.Equal(t, numArchivedFiles, len(stdOut.Lines()))
})
}
func getSingleTestPaths(t *testing.T, prefix string) (string, string) {
archiveFilepath := path.Join(testDir, fmt.Sprintf("%s-archive.tar.gz", prefix))
t.Cleanup(func() {
if err := os.RemoveAll(archiveFilepath); err != nil {
panic(err)
}
})
unarchivedPath := path.Join(testDir, fmt.Sprintf("%s-unarchived", prefix))
t.Cleanup(func() {
if err := os.RemoveAll(unarchivedPath); err != nil {
panic(err)
}
})
return archiveFilepath, unarchivedPath
}
type fileTester func(t *testing.T, path string, fileInfo os.FileInfo)
func assertDir(t *testing.T, dirPath string, tester fileTester) {
err := filepath.Walk(dirPath, func(path string, info fs.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
tester(t, path, info)
return nil
})
assert.NoError(t, err)
}
func setTimes(t *testing.T, dirPath string, atime time.Time, mtime time.Time) {
err := filepath.WalkDir(dirPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
return os.Chtimes(path, atime, mtime)
})
require.NoError(t, err)
}