|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package common |
| 8 | + |
| 9 | +import ( |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func TestLoadExtras(t *testing.T) { |
| 16 | + // Create a temporary directory for test files |
| 17 | + tempDir := t.TempDir() |
| 18 | + |
| 19 | + // Test case 1: Successfully load multiple files |
| 20 | + t.Run("success_multiple_files", func(t *testing.T) { |
| 21 | + // Create test files |
| 22 | + file1Path := filepath.Join(tempDir, "test1.json") |
| 23 | + file1Content := []byte(`{"key": "value1"}`) |
| 24 | + if err := os.WriteFile(file1Path, file1Content, 0644); err != nil { |
| 25 | + t.Fatalf("failed to create test file: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + file2Path := filepath.Join(tempDir, "test2.json") |
| 29 | + file2Content := []byte(`{"key": "value2"}`) |
| 30 | + if err := os.WriteFile(file2Path, file2Content, 0644); err != nil { |
| 31 | + t.Fatalf("failed to create test file: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + extraFiles := []string{ |
| 35 | + "foo=" + file1Path, |
| 36 | + "bar=" + file2Path, |
| 37 | + } |
| 38 | + |
| 39 | + result, err := LoadExtras(extraFiles) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("expected no error, got: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + if len(result) != 2 { |
| 45 | + t.Errorf("expected 2 entries, got %d", len(result)) |
| 46 | + } |
| 47 | + |
| 48 | + if string(result["foo"]) != string(file1Content) { |
| 49 | + t.Errorf("expected %q for foo, got %q", string(file1Content), string(result["foo"])) |
| 50 | + } |
| 51 | + |
| 52 | + if string(result["bar"]) != string(file2Content) { |
| 53 | + t.Errorf("expected %q for bar, got %q", string(file2Content), string(result["bar"])) |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + // Test case 2: Empty input slice |
| 58 | + t.Run("empty_input", func(t *testing.T) { |
| 59 | + extraFiles := []string{} |
| 60 | + |
| 61 | + result, err := LoadExtras(extraFiles) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("expected no error, got: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + if len(result) != 0 { |
| 67 | + t.Errorf("expected empty map, got %d entries", len(result)) |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + // Test case 3: File does not exist |
| 72 | + t.Run("file_not_found", func(t *testing.T) { |
| 73 | + extraFiles := []string{ |
| 74 | + "missing=" + filepath.Join(tempDir, "nonexistent.json"), |
| 75 | + } |
| 76 | + |
| 77 | + result, err := LoadExtras(extraFiles) |
| 78 | + if err == nil { |
| 79 | + t.Fatal("expected error for missing file, got nil") |
| 80 | + } |
| 81 | + |
| 82 | + if result != nil { |
| 83 | + t.Errorf("expected nil result on error, got: %v", result) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + // Test case 4: Invalid format - no colon |
| 88 | + t.Run("invalid_format_no_colon", func(t *testing.T) { |
| 89 | + extraFiles := []string{"foobar"} |
| 90 | + |
| 91 | + result, err := LoadExtras(extraFiles) |
| 92 | + if err == nil { |
| 93 | + t.Fatal("expected error for invalid format, got nil") |
| 94 | + } |
| 95 | + |
| 96 | + if result != nil { |
| 97 | + t.Errorf("expected nil result on error, got: %v", result) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + // Test case 5: Invalid format - empty key |
| 102 | + t.Run("invalid_format_empty_key", func(t *testing.T) { |
| 103 | + extraFiles := []string{"=" + filepath.Join(tempDir, "test.json")} |
| 104 | + |
| 105 | + result, err := LoadExtras(extraFiles) |
| 106 | + if err == nil { |
| 107 | + t.Fatal("expected error for empty key, got nil") |
| 108 | + } |
| 109 | + |
| 110 | + if result != nil { |
| 111 | + t.Errorf("expected nil result on error, got: %v", result) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + // Test case 6: Invalid format - empty filepath |
| 116 | + t.Run("invalid_format_empty_filepath", func(t *testing.T) { |
| 117 | + extraFiles := []string{"key="} |
| 118 | + |
| 119 | + result, err := LoadExtras(extraFiles) |
| 120 | + if err == nil { |
| 121 | + t.Fatal("expected error for empty filepath, got nil") |
| 122 | + } |
| 123 | + |
| 124 | + if result != nil { |
| 125 | + t.Errorf("expected nil result on error, got: %v", result) |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + // Test case 7: Filepath with colons (e.g., Windows paths or URLs) |
| 130 | + t.Run("filepath_with_colons", func(t *testing.T) { |
| 131 | + filePath := filepath.Join(tempDir, "test.json") |
| 132 | + fileContent := []byte("content") |
| 133 | + if err := os.WriteFile(filePath, fileContent, 0644); err != nil { |
| 134 | + t.Fatalf("failed to create test file: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + // Simulate a key with filepath that might have colons |
| 138 | + extraFiles := []string{ |
| 139 | + "mykey=" + filePath, |
| 140 | + } |
| 141 | + |
| 142 | + result, err := LoadExtras(extraFiles) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("expected no error, got: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + if string(result["mykey"]) != string(fileContent) { |
| 148 | + t.Errorf("expected %q, got %q", string(fileContent), string(result["mykey"])) |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + // Test case 8: Binary file content |
| 153 | + t.Run("binary_content", func(t *testing.T) { |
| 154 | + filePath := filepath.Join(tempDir, "binary.dat") |
| 155 | + binaryContent := []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD} |
| 156 | + if err := os.WriteFile(filePath, binaryContent, 0644); err != nil { |
| 157 | + t.Fatalf("failed to create test file: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + extraFiles := []string{"binary=" + filePath} |
| 161 | + |
| 162 | + result, err := LoadExtras(extraFiles) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("expected no error, got: %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + if len(result["binary"]) != len(binaryContent) { |
| 168 | + t.Errorf("expected length %d, got %d", len(binaryContent), len(result["binary"])) |
| 169 | + } |
| 170 | + |
| 171 | + for i, b := range binaryContent { |
| 172 | + if result["binary"][i] != b { |
| 173 | + t.Errorf("byte mismatch at index %d: expected %x, got %x", i, b, result["binary"][i]) |
| 174 | + } |
| 175 | + } |
| 176 | + }) |
| 177 | +} |
0 commit comments