|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestZipDirectory(t *testing.T) { |
| 11 | + tmpDir, err := os.MkdirTemp("", "zip-test-*") |
| 12 | + if err != nil { |
| 13 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 14 | + } |
| 15 | + defer os.RemoveAll(tmpDir) |
| 16 | + |
| 17 | + files := map[string]string{ |
| 18 | + "manifest.json": `{"name": "test", "version": "1.0"}`, |
| 19 | + "background.js": "console.log('background');", |
| 20 | + "content.js": "console.log('content');", |
| 21 | + "icons/icon.png": "fake-png-data", |
| 22 | + "node_modules/dep/foo.js": "should be excluded", |
| 23 | + "test.test.js": "should be excluded", |
| 24 | + } |
| 25 | + |
| 26 | + for path, content := range files { |
| 27 | + fullPath := filepath.Join(tmpDir, path) |
| 28 | + dir := filepath.Dir(fullPath) |
| 29 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 30 | + t.Fatalf("Failed to create directory %s: %v", dir, err) |
| 31 | + } |
| 32 | + if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil { |
| 33 | + t.Fatalf("Failed to write file %s: %v", fullPath, err) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + tmpZip, err := os.CreateTemp("", "test-zip-*.zip") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Failed to create temp zip: %v", err) |
| 40 | + } |
| 41 | + tmpZip.Close() |
| 42 | + defer os.Remove(tmpZip.Name()) |
| 43 | + |
| 44 | + // Test with exclusions |
| 45 | + t.Run("with exclusions", func(t *testing.T) { |
| 46 | + opts := &ZipOptions{ |
| 47 | + ExcludeDirectories: []string{"node_modules"}, |
| 48 | + ExcludeFilenamePatterns: []string{"*.test.js"}, |
| 49 | + } |
| 50 | + if err := ZipDirectory(tmpDir, tmpZip.Name(), opts); err != nil { |
| 51 | + t.Fatalf("ZipDirectory failed: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + // Verify the zip contents |
| 55 | + r, err := zip.OpenReader(tmpZip.Name()) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Failed to open zip: %v", err) |
| 58 | + } |
| 59 | + defer r.Close() |
| 60 | + |
| 61 | + expectedFiles := map[string]bool{ |
| 62 | + "manifest.json": false, |
| 63 | + "background.js": false, |
| 64 | + "content.js": false, |
| 65 | + "icons/": false, |
| 66 | + "icons/icon.png": false, |
| 67 | + } |
| 68 | + |
| 69 | + for _, f := range r.File { |
| 70 | + if f.FileInfo().IsDir() { |
| 71 | + expectedFiles[f.Name] = true |
| 72 | + } else { |
| 73 | + if _, ok := expectedFiles[f.Name]; ok { |
| 74 | + expectedFiles[f.Name] = true |
| 75 | + } else { |
| 76 | + t.Errorf("Unexpected file found in zip: %s", f.Name) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + for name, found := range expectedFiles { |
| 82 | + if !found && name != "icons/" { |
| 83 | + t.Errorf("Expected file not found in zip: %s", name) |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + // Test without exclusions (nil opts) |
| 89 | + t.Run("without exclusions", func(t *testing.T) { |
| 90 | + tmpZip2, err := os.CreateTemp("", "test-zip-no-exclude-*.zip") |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("Failed to create temp zip: %v", err) |
| 93 | + } |
| 94 | + tmpZip2.Close() |
| 95 | + defer os.Remove(tmpZip2.Name()) |
| 96 | + |
| 97 | + if err := ZipDirectory(tmpDir, tmpZip2.Name(), nil); err != nil { |
| 98 | + t.Fatalf("ZipDirectory failed: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + // Verify all files are included (no exclusions) |
| 102 | + r, err := zip.OpenReader(tmpZip2.Name()) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to open zip: %v", err) |
| 105 | + } |
| 106 | + defer r.Close() |
| 107 | + |
| 108 | + fileCount := 0 |
| 109 | + for _, f := range r.File { |
| 110 | + if !f.FileInfo().IsDir() { |
| 111 | + fileCount++ |
| 112 | + } |
| 113 | + } |
| 114 | + if fileCount <= 4 { |
| 115 | + t.Errorf("Expected more than 4 files when exclusions are disabled, got %d", fileCount) |
| 116 | + } |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func TestUnzip(t *testing.T) { |
| 121 | + tmpZip, err := os.CreateTemp("", "test-unzip-*.zip") |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("Failed to create temp zip: %v", err) |
| 124 | + } |
| 125 | + tmpZip.Close() |
| 126 | + defer os.Remove(tmpZip.Name()) |
| 127 | + |
| 128 | + zw, err := os.Create(tmpZip.Name()) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("Failed to open zip for writing: %v", err) |
| 131 | + } |
| 132 | + zipWriter := zip.NewWriter(zw) |
| 133 | + |
| 134 | + testFiles := map[string]string{ |
| 135 | + "file1.txt": "content of file 1", |
| 136 | + "subdir/file2.txt": "content of file 2", |
| 137 | + } |
| 138 | + |
| 139 | + if _, err := zipWriter.Create("subdir/"); err != nil { |
| 140 | + t.Fatalf("Failed to create dir entry: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + for name, content := range testFiles { |
| 144 | + w, err := zipWriter.Create(name) |
| 145 | + if err != nil { |
| 146 | + t.Fatalf("Failed to create zip entry %s: %v", name, err) |
| 147 | + } |
| 148 | + if _, err := w.Write([]byte(content)); err != nil { |
| 149 | + t.Fatalf("Failed to write zip entry %s: %v", name, err) |
| 150 | + } |
| 151 | + } |
| 152 | + zipWriter.Close() |
| 153 | + zw.Close() |
| 154 | + |
| 155 | + // Unzip to temp directory |
| 156 | + destDir, err := os.MkdirTemp("", "test-unzip-dest-*") |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("Failed to create dest dir: %v", err) |
| 159 | + } |
| 160 | + defer os.RemoveAll(destDir) |
| 161 | + |
| 162 | + if err := Unzip(tmpZip.Name(), destDir); err != nil { |
| 163 | + t.Fatalf("Unzip failed: %v", err) |
| 164 | + } |
| 165 | + |
| 166 | + // Verify extracted files |
| 167 | + for name, expectedContent := range testFiles { |
| 168 | + path := filepath.Join(destDir, name) |
| 169 | + content, err := os.ReadFile(path) |
| 170 | + if err != nil { |
| 171 | + t.Errorf("Failed to read extracted file %s: %v", name, err) |
| 172 | + continue |
| 173 | + } |
| 174 | + if string(content) != expectedContent { |
| 175 | + t.Errorf("File %s: expected %q, got %q", name, expectedContent, string(content)) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments