Skip to content

Commit a9498e8

Browse files
committed
add zip_extensions test
1 parent 0166306 commit a9498e8

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

pkg/util/zip_extensions_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package util
2+
3+
import (
4+
"archive/zip"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
func TestZipExtensionDirectory(t *testing.T) {
11+
// Create a temporary directory structure for testing
12+
tmpDir, err := os.MkdirTemp("", "zip-extension-test-*")
13+
if err != nil {
14+
t.Fatalf("Failed to create temp dir: %v", err)
15+
}
16+
defer os.RemoveAll(tmpDir)
17+
18+
// Create test file structure
19+
files := map[string]string{
20+
"manifest.json": `{"name": "test", "version": "1.0"}`,
21+
"background.js": "console.log('background');",
22+
"content.js": "console.log('content');",
23+
"icons/icon.png": "fake-png-data",
24+
"node_modules/dep/foo.js": "should be excluded",
25+
"test.test.js": "should be excluded",
26+
}
27+
28+
for path, content := range files {
29+
fullPath := filepath.Join(tmpDir, path)
30+
dir := filepath.Dir(fullPath)
31+
if err := os.MkdirAll(dir, 0755); err != nil {
32+
t.Fatalf("Failed to create directory %s: %v", dir, err)
33+
}
34+
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
35+
t.Fatalf("Failed to write file %s: %v", fullPath, err)
36+
}
37+
}
38+
39+
// Create output zip file
40+
tmpZip, err := os.CreateTemp("", "test-extension-*.zip")
41+
if err != nil {
42+
t.Fatalf("Failed to create temp zip: %v", err)
43+
}
44+
tmpZip.Close()
45+
defer os.Remove(tmpZip.Name())
46+
47+
// Test with default exclusions
48+
t.Run("with default exclusions", func(t *testing.T) {
49+
_, err := ZipExtensionDirectory(tmpDir, tmpZip.Name(), &ExtensionZipOptions{
50+
ExcludeDefaults: false,
51+
})
52+
if err != nil {
53+
t.Fatalf("ZipExtensionDirectory failed: %v", err)
54+
}
55+
56+
// Verify the zip contents
57+
r, err := zip.OpenReader(tmpZip.Name())
58+
if err != nil {
59+
t.Fatalf("Failed to open zip: %v", err)
60+
}
61+
defer r.Close()
62+
63+
expectedFiles := map[string]bool{
64+
"manifest.json": false,
65+
"background.js": false,
66+
"content.js": false,
67+
"icons/": false,
68+
"icons/icon.png": false,
69+
}
70+
71+
for _, f := range r.File {
72+
if f.FileInfo().IsDir() {
73+
expectedFiles[f.Name] = true
74+
} else {
75+
if _, ok := expectedFiles[f.Name]; ok {
76+
expectedFiles[f.Name] = true
77+
} else {
78+
// Check for files that should be excluded
79+
if contains([]string{"node_modules", ".git", "test.test.js", "package-lock.json", "__tests__"}, f.Name) {
80+
t.Errorf("Excluded file found in zip: %s", f.Name)
81+
}
82+
}
83+
}
84+
}
85+
86+
for name, found := range expectedFiles {
87+
if !found && name != "icons/" {
88+
t.Errorf("Expected file not found in zip: %s", name)
89+
}
90+
}
91+
})
92+
93+
// Test without exclusions
94+
t.Run("without default exclusions", func(t *testing.T) {
95+
tmpZip2, err := os.CreateTemp("", "test-extension-no-exclude-*.zip")
96+
if err != nil {
97+
t.Fatalf("Failed to create temp zip: %v", err)
98+
}
99+
tmpZip2.Close()
100+
defer os.Remove(tmpZip2.Name())
101+
102+
stats, err := ZipExtensionDirectory(tmpDir, tmpZip2.Name(), &ExtensionZipOptions{
103+
ExcludeDefaults: true, // Disable default exclusions
104+
})
105+
if err != nil {
106+
t.Fatalf("ZipExtensionDirectory failed: %v", err)
107+
}
108+
109+
// Should include all files when exclusions are disabled
110+
if stats.FilesIncluded <= 4 {
111+
t.Errorf("Expected more than 4 files when exclusions are disabled, got %d", stats.FilesIncluded)
112+
}
113+
})
114+
}
115+
116+
func TestDefaultExtensionExclusions(t *testing.T) {
117+
// Verify the exclusion lists are not empty
118+
if len(DefaultExtensionExclusions.ExcludeDirectory) == 0 {
119+
t.Error("ExcludeDirectory should not be empty")
120+
}
121+
if len(DefaultExtensionExclusions.ExcludeFilenamePatterns) == 0 {
122+
t.Error("ExcludeFilenamePatterns should not be empty")
123+
}
124+
125+
// Verify specific important exclusions are present
126+
expectedDirs := []string{"node_modules", ".git", "__tests__", "coverage"}
127+
for _, path := range expectedDirs {
128+
if !contains(DefaultExtensionExclusions.ExcludeDirectory, path) {
129+
t.Errorf("Expected directory exclusion not found: %s", path)
130+
}
131+
}
132+
133+
expectedPatterns := []string{"*.test.js", "*.test.ts", "*.spec.js", "*.spec.ts", "*.log", "*.swp"}
134+
for _, pattern := range expectedPatterns {
135+
if !contains(DefaultExtensionExclusions.ExcludeFilenamePatterns, pattern) {
136+
t.Errorf("Expected pattern exclusion not found: %s", pattern)
137+
}
138+
}
139+
}
140+
141+
// Helper function to check if a slice contains a string
142+
func contains(slice []string, str string) bool {
143+
for _, s := range slice {
144+
if s == str {
145+
return true
146+
}
147+
}
148+
return false
149+
}

0 commit comments

Comments
 (0)