(t *testing.T)
| 280 | } |
| 281 | |
| 282 | func TestGoGetterDownload(t *testing.T) { |
| 283 | tests := []struct { |
| 284 | name string |
| 285 | src string |
| 286 | tmpDir string |
| 287 | setupFS func(fs afero.Fs, destDir string) error |
| 288 | mockDownloader func(*mockDownloader, string) |
| 289 | expectError bool |
| 290 | errorMsg string |
| 291 | }{ |
| 292 | { |
| 293 | name: "successful download with policy.yaml file", |
| 294 | src: "https://github.com/example/policy", |
| 295 | tmpDir: "/tmp/test", |
| 296 | setupFS: func(fs afero.Fs, destDir string) error { |
| 297 | // Create the downloaded directory structure |
| 298 | if err := fs.MkdirAll(destDir, 0o755); err != nil { |
| 299 | return err |
| 300 | } |
| 301 | // Create a policy.yaml file that choosePolicyFile can find |
| 302 | return afero.WriteFile(fs, path.Join(destDir, "policy.yaml"), []byte("test config"), 0o644) |
| 303 | }, |
| 304 | mockDownloader: func(m *mockDownloader, destDir string) { |
| 305 | // Mock successful download |
| 306 | m.On("Download", mock.Anything, destDir, "https://github.com/example/policy", false). |
| 307 | Return(&fileMetadata.FSMetadata{Path: destDir}, nil) |
| 308 | }, |
| 309 | expectError: false, |
| 310 | }, |
| 311 | { |
| 312 | name: "successful download with .ec/policy.json file", |
| 313 | src: "https://github.com/example/policy2", |
| 314 | tmpDir: "/tmp/test2", |
| 315 | setupFS: func(fs afero.Fs, destDir string) error { |
| 316 | if err := fs.MkdirAll(destDir, 0o755); err != nil { |
| 317 | return err |
| 318 | } |
| 319 | // Create .ec/policy.json file (higher priority) |
| 320 | return afero.WriteFile(fs, path.Join(destDir, ".ec/policy.json"), []byte(`{"policy": "config"}`), 0o644) |
| 321 | }, |
| 322 | mockDownloader: func(m *mockDownloader, destDir string) { |
| 323 | m.On("Download", mock.Anything, destDir, "https://github.com/example/policy2", false). |
| 324 | Return(&fileMetadata.FSMetadata{Path: destDir}, nil) |
| 325 | }, |
| 326 | expectError: false, |
| 327 | }, |
| 328 | { |
| 329 | name: "download failure", |
| 330 | src: "https://github.com/example/nonexistent", |
| 331 | tmpDir: "/tmp/test3", |
| 332 | setupFS: func(fs afero.Fs, destDir string) error { |
| 333 | // No filesystem setup needed for download failure |
| 334 | return nil |
| 335 | }, |
| 336 | mockDownloader: func(m *mockDownloader, destDir string) { |
| 337 | m.On("Download", mock.Anything, destDir, "https://github.com/example/nonexistent", false). |
| 338 | Return(nil, errors.New("failed to download: repository not found")) |
| 339 | }, |
nothing calls this directly
no test coverage detected