(t *testing.T)
| 295 | } |
| 296 | |
| 297 | func TestEnsurePoutineConfig(t *testing.T) { |
| 298 | // Create a temporary directory |
| 299 | tmpDir := testutil.TempDir(t, "test-*") |
| 300 | |
| 301 | t.Run("creates config file when it doesn't exist", func(t *testing.T) { |
| 302 | err := ensurePoutineConfig(tmpDir) |
| 303 | if err != nil { |
| 304 | t.Fatalf("ensurePoutineConfig failed: %v", err) |
| 305 | } |
| 306 | |
| 307 | // Check that the config file was created |
| 308 | configPath := filepath.Join(tmpDir, ".poutine.yml") |
| 309 | if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 310 | t.Errorf("Config file was not created at %s", configPath) |
| 311 | } |
| 312 | |
| 313 | // Read and verify the content |
| 314 | content, err := os.ReadFile(configPath) |
| 315 | if err != nil { |
| 316 | t.Fatalf("Failed to read config file: %v", err) |
| 317 | } |
| 318 | |
| 319 | expectedStrings := []string{ |
| 320 | "# Configure poutine security scanner", |
| 321 | "rulesConfig:", |
| 322 | "pr_runs_on_self_hosted:", |
| 323 | "allowed_runners:", |
| 324 | "- ubuntu-slim", |
| 325 | } |
| 326 | |
| 327 | for _, expected := range expectedStrings { |
| 328 | if !strings.Contains(string(content), expected) { |
| 329 | t.Errorf("Config file does not contain expected string %q. Content:\n%s", expected, string(content)) |
| 330 | } |
| 331 | } |
| 332 | }) |
| 333 | |
| 334 | t.Run("does not overwrite existing config file", func(t *testing.T) { |
| 335 | // Create a different temporary directory |
| 336 | tmpDir2 := testutil.TempDir(t, "test-*") |
| 337 | configPath := filepath.Join(tmpDir2, ".poutine.yml") |
| 338 | |
| 339 | // Create a custom config file |
| 340 | customContent := "# My custom poutine config\ncustom: value\n" |
| 341 | err := os.WriteFile(configPath, []byte(customContent), 0644) |
| 342 | if err != nil { |
| 343 | t.Fatalf("Failed to create custom config file: %v", err) |
| 344 | } |
| 345 | |
| 346 | // Call ensurePoutineConfig |
| 347 | err = ensurePoutineConfig(tmpDir2) |
| 348 | if err != nil { |
| 349 | t.Fatalf("ensurePoutineConfig failed: %v", err) |
| 350 | } |
| 351 | |
| 352 | // Read the file and verify it wasn't changed |
| 353 | content, err := os.ReadFile(configPath) |
| 354 | if err != nil { |
nothing calls this directly
no test coverage detected