Skip to content

Commit 680342a

Browse files
authored
Handle missing reqs section gracefully (#53)
* fix: handle missing reqs section gracefully and update tests for expected behavior * fix: improve output message for missing reqs section and enhance tests for various scenarios * fix: ensure output format is reset to default after test execution
1 parent f2c3abd commit 680342a

File tree

2 files changed

+99
-28
lines changed

2 files changed

+99
-28
lines changed

cli/src/cmd/app/commands/core.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,16 @@ func executeReqs() error {
156156
return err
157157
}
158158

159+
// If no reqs section exists, skip checks gracefully
159160
if len(azureYaml.Reqs) == 0 {
160-
return fmt.Errorf("no reqs defined in azure.yaml - run 'azd app reqs --generate' to add them")
161+
if output.IsJSON() {
162+
return output.PrintJSON(ReqsResult{
163+
Satisfied: true,
164+
Reqs: []ReqResult{},
165+
})
166+
}
167+
output.Info("No reqs defined in azure.yaml - skipping checks. Run 'azd app reqs --generate' to add requirement checks.")
168+
return nil
161169
}
162170

163171
// Initialize cache manager

cli/src/cmd/app/commands/reqs_test.go

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"os"
55
"path/filepath"
66
"runtime"
7-
"strings"
87
"testing"
98

9+
"github.com/jongio/azd-app/cli/src/internal/output"
1010
"gopkg.in/yaml.v3"
1111
)
1212

@@ -393,36 +393,99 @@ func TestRunPrereqsWithInvalidYAML(t *testing.T) {
393393
}
394394

395395
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+
},
400437
}
401-
defer func() {
402-
if chdirErr := os.Chdir(originalDir); chdirErr != nil {
403-
t.Logf("Warning: failed to restore directory: %v", chdirErr)
404-
}
405-
}()
406438

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+
}()
412451

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+
}
419457

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+
})
426489
}
427490
}
428491

0 commit comments

Comments
 (0)