|
1 | 1 | package ondemand |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
4 | 9 |
|
5 | 10 | func TestZipFolder(t *testing.T) { |
6 | | - // TODO: Revisit this test - currently failing in CI |
7 | | - // Test may fail if "zip" folder doesn't exist or is not accessible in CI environment. |
8 | | - // Needs proper test data setup or mocking of filesystem. |
9 | | - t.Skip("Skipping until zip folder test can be properly set up") |
| 11 | + // Create a temporary directory for the test |
| 12 | + tempDir, err := os.MkdirTemp("", "ziptest") |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 15 | + } |
| 16 | + defer os.RemoveAll(tempDir) |
| 17 | + |
| 18 | + t.Logf("DEBUG: Created temp dir: %s", tempDir) |
| 19 | + |
| 20 | + // Create a test folder inside temp dir |
| 21 | + testFolder := filepath.Join(tempDir, "testfolder") |
| 22 | + err = os.Mkdir(testFolder, 0755) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Failed to create test folder: %v", err) |
| 25 | + } |
| 26 | + t.Logf("DEBUG: Created test folder: %s", testFolder) |
| 27 | + |
| 28 | + // Create some test files |
| 29 | + testFiles := []struct { |
| 30 | + name string |
| 31 | + content string |
| 32 | + }{ |
| 33 | + {"file1.txt", "Hello, World!"}, |
| 34 | + {"file2.txt", "Test content"}, |
| 35 | + {"file3.log", "Log data here"}, |
| 36 | + } |
| 37 | + |
| 38 | + for _, tf := range testFiles { |
| 39 | + filePath := filepath.Join(testFolder, tf.name) |
| 40 | + err = os.WriteFile(filePath, []byte(tf.content), 0644) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Failed to create test file %s: %v", tf.name, err) |
| 43 | + } |
| 44 | + t.Logf("DEBUG: Created test file: %s", filePath) |
| 45 | + } |
| 46 | + |
| 47 | + // Change to temp dir so the zip is created there |
| 48 | + originalDir, err := os.Getwd() |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("Failed to get current dir: %v", err) |
| 51 | + } |
| 52 | + defer os.Chdir(originalDir) |
10 | 53 |
|
11 | | - _, err := ZipFolder("zip") |
| 54 | + err = os.Chdir(tempDir) |
12 | 55 | if err != nil { |
13 | | - t.Fatal(err) |
| 56 | + t.Fatalf("Failed to change to temp dir: %v", err) |
14 | 57 | } |
| 58 | + t.Logf("DEBUG: Changed to dir: %s", tempDir) |
| 59 | + |
| 60 | + // Call ZipFolder with the relative path |
| 61 | + zipName, err := ZipFolder("testfolder") |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("ZipFolder failed: %v", err) |
| 64 | + } |
| 65 | + t.Logf("DEBUG: Created zip file: %s", zipName) |
| 66 | + |
| 67 | + // Verify the zip file was created |
| 68 | + zipPath := filepath.Join(tempDir, zipName) |
| 69 | + if _, err := os.Stat(zipPath); os.IsNotExist(err) { |
| 70 | + t.Fatalf("Zip file was not created at %s", zipPath) |
| 71 | + } |
| 72 | + t.Logf("DEBUG: Verified zip exists at: %s", zipPath) |
| 73 | + |
| 74 | + // Open and verify the zip contents |
| 75 | + reader, err := zip.OpenReader(zipPath) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Failed to open zip file: %v", err) |
| 78 | + } |
| 79 | + defer reader.Close() |
| 80 | + |
| 81 | + t.Logf("DEBUG: Zip contains %d files", len(reader.File)) |
| 82 | + for _, f := range reader.File { |
| 83 | + t.Logf("DEBUG: Zip entry: %s (size: %d)", f.Name, f.UncompressedSize64) |
| 84 | + } |
| 85 | + |
| 86 | + // Verify all test files are in the zip |
| 87 | + if len(reader.File) != len(testFiles) { |
| 88 | + t.Errorf("Expected %d files in zip, got %d", len(testFiles), len(reader.File)) |
| 89 | + } |
| 90 | + |
| 91 | + // Cleanup the zip file |
| 92 | + os.Remove(zipPath) |
15 | 93 | } |
0 commit comments