-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress_test.go
More file actions
184 lines (152 loc) · 5.23 KB
/
compress_test.go
File metadata and controls
184 lines (152 loc) · 5.23 KB
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
package pathlib
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/suite"
)
type CompressSuite struct {
suite.Suite
tempDir string
}
func TestCompressSuite(t *testing.T) {
suite.Run(t, new(CompressSuite))
}
func (s *CompressSuite) SetupTest() {
s.tempDir = s.T().TempDir()
}
func (s *CompressSuite) TearDownSuite() {
}
func (s *CompressSuite) createTestFiles(dirPath *FsPath) []struct {
name string
content string
} {
testFiles := []struct {
name string
content string
}{
{"file1.txt", "Content of file 1"},
{"file2.txt", "Content of file 2"},
{"subdir/file3.txt", "Content of file 3 in subdirectory"},
{"subdir1/subdir2/file4.txt", "Content of file 4 in sub/subdirectory"},
}
for i, tf := range testFiles {
filePath := dirPath.Join(tf.name)
s.Require().NoError(filePath.MkParentDir())
s.Require().NoError(filePath.WriteText(tf.content))
relPath, err := filePath.RelativeTo(dirPath.absPath)
s.Require().NoError(err, "Failed to get relative path")
testFiles[i].name = relPath
}
return testFiles
}
func (s *CompressSuite) TestCompression() {
testCases := []struct {
name string
compressFunc func(*FsPath, string) (*FsPath, int, error)
extractFunc func(*FsPath, string, ...CompressOption) error
fileExtension string
}{
{
name: "Tar.gz",
compressFunc: func(dirPath *FsPath, fileName string) (*FsPath, int, error) {
return dirPath.TarGzDir(fileName)
},
extractFunc: func(archivePath *FsPath, destDir string, opts ...CompressOption) error {
return archivePath.Untar(destDir, opts...)
},
fileExtension: ".tar.gz",
},
{
name: "Zip",
compressFunc: func(dirPath *FsPath, fileName string) (*FsPath, int, error) {
return dirPath.ZipDir(fileName)
},
extractFunc: func(archivePath *FsPath, destDir string, opts ...CompressOption) error {
return archivePath.Unzip(destDir, opts...)
},
fileExtension: ".zip",
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
dirPath := Path(s.tempDir)
testFiles := s.createTestFiles(dirPath)
wantedLen := len(testFiles)
s.Run("Successful compression", func() {
compressedPath, totalFiles, err := tc.compressFunc(dirPath, "test_archive"+tc.fileExtension)
s.Require().NoError(err)
s.NotNil(compressedPath)
s.Equal(wantedLen, totalFiles)
s.True(compressedPath.Exists())
s.verifyCompressedContent(compressedPath, testFiles, tc.extractFunc)
})
s.Run("Overwrite existing compressed file", func() {
_, _, err := tc.compressFunc(dirPath, "overwrite_test"+tc.fileExtension)
s.Require().NoError(err)
compressedPath, totalFiles, err := tc.compressFunc(dirPath, "overwrite_test"+tc.fileExtension)
s.Require().NoError(err)
s.NotNil(compressedPath)
s.Equal(wantedLen, totalFiles)
s.True(compressedPath.Exists())
})
s.Run("Non-directory path", func() {
filePath := dirPath.Join("file1.txt")
_, _, err := tc.compressFunc(filePath, "should_fail"+tc.fileExtension)
s.Require().Error(err)
s.ErrorIs(err, ErrNotDirectory)
})
s.Run("Empty directory", func() {
emptyDir := dirPath.Join("empty_dir")
s.Require().NoError(emptyDir.Mkdir(0o755, true))
compressedPath, totalFiles, err := tc.compressFunc(emptyDir, "empty"+tc.fileExtension)
s.Require().NoError(err)
s.NotNil(compressedPath)
s.Equal(0, totalFiles)
s.True(compressedPath.Exists())
_ = compressedPath.Unlink(true)
})
s.Run("Extract with custom max size", func() {
compressedPath, _, err := tc.compressFunc(dirPath, "max_size_test"+tc.fileExtension)
s.Require().NoError(err)
extractDir := s.T().TempDir()
smallMaxSize := int64(10) // Only allow 10 bytes
err = tc.extractFunc(compressedPath, extractDir, WithMaxSize(smallMaxSize))
s.Require().Error(err)
s.ErrorIs(err, ErrFileTooLarge)
})
})
}
}
func (s *CompressSuite) verifyCompressedContent(compressedPath *FsPath, expectedFiles []struct{ name, content string }, extractFunc func(*FsPath, string, ...CompressOption) error) {
s.T().Helper()
extractPath := Path(s.T().TempDir())
err := extractFunc(compressedPath, extractPath.absPath)
s.Require().NoError(err, "Failed to extract archive")
archiveName := strings.TrimSuffix(strings.TrimSuffix(compressedPath.Name, ".tar.gz"), ".zip")
extractedContentPath := extractPath.Join(archiveName)
for _, ef := range expectedFiles {
extractedFilePath := extractedContentPath.Join(ef.name)
s.True(extractedFilePath.Exists(), "File not found in extracted content: "+ef.name)
content, err := extractedFilePath.ReadText()
s.Require().NoError(err, "Failed to read extracted file: "+ef.name)
s.Equal(ef.content, content, "Content mismatch for file: "+ef.name)
}
var foundFiles []string
err = extractedContentPath.Walk(func(relPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
foundFiles = append(foundFiles, relPath)
}
return nil
})
s.Require().NoError(err, "Failed to walk extracted directory")
s.Equal(len(expectedFiles), len(foundFiles), "Number of extracted files doesn't match expected")
// Print found files for debugging
if len(foundFiles) != len(expectedFiles) {
s.T().Logf("Found files: %v", foundFiles)
s.T().Logf("Expected files: %v", expectedFiles)
}
}