-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_test.go
314 lines (264 loc) · 7 KB
/
files_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package mcsmanager
import (
"archive/tar"
"fmt"
"io"
"os"
"path/filepath"
"testing"
)
var files = []string{
"file1.txt",
"file2.txt",
filepath.Join("nested", "file3.txt"),
filepath.Join("nested", "file4.txt"),
}
// setupTestDir creates a simple file tree in the given directory.
func setupTestDir(path string) error {
if err := os.Mkdir(filepath.Join(path, "nested"), 0755); err != nil {
return err
}
// Create our test files
for _, file := range files {
path := filepath.Join(path, file)
err := os.WriteFile(path, []byte("test file contents"), 0644)
if err != nil {
return err
}
}
return nil
}
func TestArchiveDir(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
if err := setupTestDir(dir); err != nil {
t.Fatalf("error creating test dir: %s\n", err)
}
// Create our tar writer
archivePath := filepath.Join(dir, "archive.tar")
tarFile, err := os.Create(archivePath)
if err != nil {
t.Fatalf("error creating archive file: %s\n", err)
}
w := tar.NewWriter(tarFile)
// Archive the directory tree
err = Archive(dir, w, "archive.tar")
if err != nil {
t.Fatalf("error writing file tree to archive: %s\n", err)
}
w.Close()
tarFile.Close()
// Extract the archive files to inspect them
extractPath := filepath.Join(dir, "extracted")
if err = os.Mkdir(extractPath, 0755); err != nil {
t.Fatalf("error creating extraction dir: %s\n", err)
}
if err = extract(archivePath, extractPath); err != nil {
t.Fatalf("error extracting archive: %s\n", err)
}
if err = verifyFiles(dir, extractPath); err != nil {
t.Fatal(err)
}
}
// extract extracts the entire file tree inside a tar archive to the given path.
func extract(archive, dir string) error {
tarFile, err := os.Open(archive)
if err != nil {
return err
}
defer tarFile.Close()
extractPath, err := filepath.Abs(dir)
if err != nil {
return err
}
reader := tar.NewReader(tarFile)
// Extract the file tree in the archive
for {
header, err := reader.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
fi := header.FileInfo()
path := filepath.Join(extractPath, header.Name)
// If it's a directory, create it
if fi.Mode().IsDir() {
if err := os.MkdirAll(path, fi.Mode().Perm()); err != nil {
return err
}
continue
}
// Create new file from archived file
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fi.Mode().Perm())
if err != nil {
return err
}
// Copy the contents of the file
n, err := io.Copy(file, reader)
if err != nil {
return err
}
file.Close()
if n != fi.Size() {
return fmt.Errorf("error copying file '%s': wrote %d, but should have written %d", header.Name, n, fi.Size())
}
}
return nil
}
// verifyFiles opens the original and extracted files and checks to see that they match.
func verifyFiles(basePath, extractedPath string) error {
for _, fName := range files {
expectedPath := filepath.Join(basePath, fName)
actualPath := filepath.Join(extractedPath, fName)
expectedFi, err := os.Stat(expectedPath)
if err != nil {
return err
}
actualFi, err := os.Stat(actualPath)
if err != nil {
return err
}
// Test the attributes
if expectedFi.Size() != actualFi.Size() {
return fmt.Errorf("file size does not match for '%s': expected %d, got %d", fName, expectedFi.Size(), actualFi.Size())
}
if expectedFi.Mode().Perm() != actualFi.Mode().Perm() {
return fmt.Errorf("file perms do not match for '%s': expected %#o, got %#o", fName, expectedFi.Mode().Perm(), actualFi.Mode().Perm())
}
// Test the file contents
expectedContents, err := os.ReadFile(expectedPath)
if err != nil {
return err
}
actualContents, err := os.ReadFile(actualPath)
if err != nil {
return err
}
if string(expectedContents) != string(actualContents) {
return fmt.Errorf("file contents differ for file '%s': expected '%s', got '%s'", fName, string(expectedContents), string(actualContents))
}
}
return nil
}
func TestCountFiles(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
if err := setupTestDir(dir); err != nil {
t.Fatalf("error creating test dir: %s\n", err)
}
result, err := CountFiles(dir)
if err != nil {
t.Fatalf("error counting files: %s\n", err)
}
// Check if the result is correct
if result != len(files) {
t.Fatalf("counted the wrong number of files: expected %d, actual: %d", len(files), result)
}
}
func TestCountFilesWithExclusions(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
if err := setupTestDir(dir); err != nil {
t.Fatalf("error creating test dir: %s\n", err)
}
result, err := CountFiles(dir, "file2.txt")
if err != nil {
t.Fatalf("error counting files: %s\n", err)
}
// Check if the result is correct
if result != len(files)-1 {
t.Fatalf("counted the wrong number of files: expected %d, actual: %d", len(files)-1, result)
}
}
func TestPruneFiles(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
files := []string{
"file1.txt",
"file2.png",
}
// Create our test files
for _, file := range files {
path := filepath.Join(dir, file)
f, err := os.Create(path)
if err != nil {
t.Fatalf("error creating test file: %s\n", err)
}
f.Close()
}
result, err := Prune(dir, 2)
if err != nil {
t.Fatalf("error counting files: %s\n", err)
}
// Check if the result is correct
if result != 1 {
t.Fatalf("pruned wrong number of files: expected 1, pruned %d", result)
}
}
func TestPruneFilesWithExemptions(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
files := []string{
"file1.txt",
"file2.png",
}
// Create our test files
for _, file := range files {
path := filepath.Join(dir, file)
f, err := os.Create(path)
if err != nil {
t.Fatalf("error creating test file: %s\n", err)
}
f.Close()
}
result, err := Prune(dir, 2, ".txt")
if err != nil {
t.Fatalf("error counting files: %s\n", err)
}
// Check if the result is correct
if result != 0 {
t.Fatalf("pruned wrong number of files: expected 0, actual %d", result)
}
}
func TestPruneOldFiles(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
// Create our test files
path := filepath.Join(dir, "old.txt")
f, err := os.Create(path)
if err != nil {
t.Fatalf("error creating test file: %s\n", err)
}
f.Close()
// Prune the files
result, err := PruneOld(dir, 0)
if err != nil {
t.Fatalf("error counting files: %s\n", err)
}
// Check if the result is correct
if result != 1 {
t.Fatalf("pruned wrong number of files: expected 1, pruned %d", result)
}
}
func TestPruneOldFilesWithExemptions(t *testing.T) {
// Create temp dir to test in
dir := t.TempDir()
// Create our test files
path := filepath.Join(dir, "old.txt")
f, err := os.Create(path)
if err != nil {
t.Fatalf("error creating test file: %s\n", err)
}
f.Close()
// Prune the files
result, err := PruneOld(dir, 0, ".txt")
if err != nil {
t.Fatalf("error counting files: %s\n", err)
}
// Check if the result is correct
if result != 0 {
t.Fatalf("pruned wrong number of files: expected 0, pruned %d", result)
}
}