|
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 | + // Create a test folder inside temp dir |
| 19 | + testFolder := filepath.Join(tempDir, "testfolder") |
| 20 | + if err := os.Mkdir(testFolder, 0755); err != nil { |
| 21 | + t.Fatalf("Failed to create test folder: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + // Create test files |
| 25 | + testFiles := []struct { |
| 26 | + name string |
| 27 | + content string |
| 28 | + }{ |
| 29 | + {"file1.txt", "Hello, World!"}, |
| 30 | + {"file2.txt", "Test content"}, |
| 31 | + {"file3.log", "Log data here"}, |
| 32 | + } |
| 33 | + |
| 34 | + for _, tf := range testFiles { |
| 35 | + filePath := filepath.Join(testFolder, tf.name) |
| 36 | + if err := os.WriteFile(filePath, []byte(tf.content), 0644); err != nil { |
| 37 | + t.Fatalf("Failed to create test file %s: %v", tf.name, err) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Change to temp dir so the zip is created there |
| 42 | + originalDir, err := os.Getwd() |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Failed to get current dir: %v", err) |
| 45 | + } |
| 46 | + defer os.Chdir(originalDir) |
| 47 | + |
| 48 | + if err := os.Chdir(tempDir); err != nil { |
| 49 | + t.Fatalf("Failed to change to temp dir: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Call ZipFolder with the relative path |
| 53 | + zipName, err := ZipFolder("testfolder") |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("ZipFolder failed: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + // Verify the zip file was created |
| 59 | + zipPath := filepath.Join(tempDir, zipName) |
| 60 | + if _, err := os.Stat(zipPath); os.IsNotExist(err) { |
| 61 | + t.Fatalf("Zip file was not created at %s", zipPath) |
| 62 | + } |
10 | 63 |
|
11 | | - _, err := ZipFolder("zip") |
| 64 | + // Open and verify the zip contents |
| 65 | + reader, err := zip.OpenReader(zipPath) |
12 | 66 | if err != nil { |
13 | | - t.Fatal(err) |
| 67 | + t.Fatalf("Failed to open zip file: %v", err) |
| 68 | + } |
| 69 | + defer reader.Close() |
| 70 | + |
| 71 | + if len(reader.File) != len(testFiles) { |
| 72 | + t.Errorf("Expected %d files in zip, got %d", len(testFiles), len(reader.File)) |
14 | 73 | } |
15 | 74 | } |
0 commit comments