|
4 | 4 | "os" |
5 | 5 | "path/filepath" |
6 | 6 | "runtime" |
7 | | - "strings" |
8 | 7 | "testing" |
9 | 8 |
|
| 9 | + "github.com/jongio/azd-app/cli/src/internal/output" |
10 | 10 | "gopkg.in/yaml.v3" |
11 | 11 | ) |
12 | 12 |
|
@@ -393,36 +393,99 @@ func TestRunPrereqsWithInvalidYAML(t *testing.T) { |
393 | 393 | } |
394 | 394 |
|
395 | 395 | func TestRunPrereqsWithNoPrereqs(t *testing.T) { |
396 | | - // Save current directory |
397 | | - originalDir, err := os.Getwd() |
398 | | - if err != nil { |
399 | | - t.Fatal(err) |
| 396 | + tests := []struct { |
| 397 | + name string |
| 398 | + yamlContent string |
| 399 | + useJSONOutput bool |
| 400 | + expectedError bool |
| 401 | + validateOutput func(t *testing.T) |
| 402 | + }{ |
| 403 | + { |
| 404 | + name: "empty reqs array - default output", |
| 405 | + yamlContent: `name: test |
| 406 | +reqs: [] |
| 407 | +`, |
| 408 | + useJSONOutput: false, |
| 409 | + expectedError: false, |
| 410 | + }, |
| 411 | + { |
| 412 | + name: "no reqs section - default output", |
| 413 | + yamlContent: `name: test |
| 414 | +services: |
| 415 | + - name: web |
| 416 | +`, |
| 417 | + useJSONOutput: false, |
| 418 | + expectedError: false, |
| 419 | + }, |
| 420 | + { |
| 421 | + name: "empty reqs array - JSON output", |
| 422 | + yamlContent: `name: test |
| 423 | +reqs: [] |
| 424 | +`, |
| 425 | + useJSONOutput: true, |
| 426 | + expectedError: false, |
| 427 | + }, |
| 428 | + { |
| 429 | + name: "no reqs section - JSON output", |
| 430 | + yamlContent: `name: test |
| 431 | +services: |
| 432 | + - name: api |
| 433 | +`, |
| 434 | + useJSONOutput: true, |
| 435 | + expectedError: false, |
| 436 | + }, |
400 | 437 | } |
401 | | - defer func() { |
402 | | - if chdirErr := os.Chdir(originalDir); chdirErr != nil { |
403 | | - t.Logf("Warning: failed to restore directory: %v", chdirErr) |
404 | | - } |
405 | | - }() |
406 | 438 |
|
407 | | - // Create temporary directory with empty reqs |
408 | | - tempDir := t.TempDir() |
409 | | - if err := os.Chdir(tempDir); err != nil { |
410 | | - t.Fatal(err) |
411 | | - } |
| 439 | + for _, tt := range tests { |
| 440 | + t.Run(tt.name, func(t *testing.T) { |
| 441 | + // Save current directory |
| 442 | + originalDir, err := os.Getwd() |
| 443 | + if err != nil { |
| 444 | + t.Fatal(err) |
| 445 | + } |
| 446 | + defer func() { |
| 447 | + if chdirErr := os.Chdir(originalDir); chdirErr != nil { |
| 448 | + t.Logf("Warning: failed to restore directory: %v", chdirErr) |
| 449 | + } |
| 450 | + }() |
412 | 451 |
|
413 | | - emptyYAML := `name: test |
414 | | -reqs: [] |
415 | | -` |
416 | | - if err := os.WriteFile("azure.yaml", []byte(emptyYAML), 0600); err != nil { |
417 | | - t.Fatal(err) |
418 | | - } |
| 452 | + // Create temporary directory |
| 453 | + tempDir := t.TempDir() |
| 454 | + if err := os.Chdir(tempDir); err != nil { |
| 455 | + t.Fatal(err) |
| 456 | + } |
419 | 457 |
|
420 | | - err = runReqs() |
421 | | - if err == nil { |
422 | | - t.Error("Expected error with empty reqs (backwards compatibility removed), got nil") |
423 | | - } |
424 | | - if !strings.Contains(err.Error(), "no reqs defined in azure.yaml") { |
425 | | - t.Errorf("Expected error about no reqs defined, got: %v", err) |
| 458 | + // Write azure.yaml |
| 459 | + if err := os.WriteFile("azure.yaml", []byte(tt.yamlContent), 0600); err != nil { |
| 460 | + t.Fatal(err) |
| 461 | + } |
| 462 | + |
| 463 | + // Set output format |
| 464 | + if tt.useJSONOutput { |
| 465 | + if err := output.SetFormat("json"); err != nil { |
| 466 | + t.Fatal(err) |
| 467 | + } |
| 468 | + defer func() { |
| 469 | + _ = output.SetFormat("default") |
| 470 | + }() |
| 471 | + } |
| 472 | + |
| 473 | + // Run the command |
| 474 | + err = runReqs() |
| 475 | + |
| 476 | + // Verify error expectation |
| 477 | + if tt.expectedError && err == nil { |
| 478 | + t.Error("Expected error but got nil") |
| 479 | + } |
| 480 | + if !tt.expectedError && err != nil { |
| 481 | + t.Errorf("Expected no error but got: %v", err) |
| 482 | + } |
| 483 | + |
| 484 | + // Additional validation if provided |
| 485 | + if tt.validateOutput != nil { |
| 486 | + tt.validateOutput(t) |
| 487 | + } |
| 488 | + }) |
426 | 489 | } |
427 | 490 | } |
428 | 491 |
|
|
0 commit comments