|
| 1 | +package nativelib |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestExtractLibrary(t *testing.T) { |
| 11 | + zipPath := createTestZip(t, "test.dll", []byte("fake dll content")) |
| 12 | + destDir := t.TempDir() |
| 13 | + |
| 14 | + path, err := ExtractLibrary(zipPath, destDir, "test.dll") |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("ExtractLibrary() error: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + if filepath.Base(path) != "test.dll" { |
| 20 | + t.Errorf("extracted path = %q, want test.dll basename", path) |
| 21 | + } |
| 22 | + |
| 23 | + data, err := os.ReadFile(path) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("read extracted file: %v", err) |
| 26 | + } |
| 27 | + if string(data) != "fake dll content" { |
| 28 | + t.Errorf("content = %q, want %q", string(data), "fake dll content") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestExtractLibraryNestedPath(t *testing.T) { |
| 33 | + zipPath := createTestZipNested(t, "lib/wgpu_native.dll", []byte("nested content")) |
| 34 | + destDir := t.TempDir() |
| 35 | + |
| 36 | + path, err := ExtractLibrary(zipPath, destDir, "wgpu_native.dll") |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("ExtractLibrary() error: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + data, err := os.ReadFile(path) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("read: %v", err) |
| 44 | + } |
| 45 | + if string(data) != "nested content" { |
| 46 | + t.Errorf("content = %q, want %q", string(data), "nested content") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestExtractLibraryNotFound(t *testing.T) { |
| 51 | + zipPath := createTestZip(t, "other.dll", []byte("data")) |
| 52 | + destDir := t.TempDir() |
| 53 | + |
| 54 | + _, err := ExtractLibrary(zipPath, destDir, "missing.dll") |
| 55 | + if err == nil { |
| 56 | + t.Fatal("expected error for missing file in archive") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestExtractLibraryInvalidZip(t *testing.T) { |
| 61 | + tmpFile := filepath.Join(t.TempDir(), "bad.zip") |
| 62 | + os.WriteFile(tmpFile, []byte("not a zip"), 0o644) |
| 63 | + |
| 64 | + _, err := ExtractLibrary(tmpFile, t.TempDir(), "test.dll") |
| 65 | + if err == nil { |
| 66 | + t.Fatal("expected error for invalid zip") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func createTestZip(t *testing.T, name string, content []byte) string { |
| 71 | + t.Helper() |
| 72 | + path := filepath.Join(t.TempDir(), "test.zip") |
| 73 | + f, err := os.Create(path) |
| 74 | + if err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + defer f.Close() |
| 78 | + |
| 79 | + w := zip.NewWriter(f) |
| 80 | + entry, err := w.Create(name) |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + entry.Write(content) |
| 85 | + w.Close() |
| 86 | + return path |
| 87 | +} |
| 88 | + |
| 89 | +func createTestZipNested(t *testing.T, name string, content []byte) string { |
| 90 | + t.Helper() |
| 91 | + path := filepath.Join(t.TempDir(), "test.zip") |
| 92 | + f, err := os.Create(path) |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + defer f.Close() |
| 97 | + |
| 98 | + w := zip.NewWriter(f) |
| 99 | + entry, err := w.Create(name) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + entry.Write(content) |
| 104 | + w.Close() |
| 105 | + return path |
| 106 | +} |
0 commit comments