Skip to content

Commit

Permalink
Add more tests for the analyze command
Browse files Browse the repository at this point in the history
  • Loading branch information
thschmitt committed Dec 19, 2024
1 parent 29225d4 commit 3fea971
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions plugin/studio/package_analyze_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func (c PackageAnalyzeCommand) randomJsonResultFileName() string {

func (c PackageAnalyzeCommand) readAnalyzeResult(path string) ([]packageAnalyzeViolation, error) {
file, err := os.Open(path)
if err != nil && errors.Is(err, os.ErrNotExist) {
return []packageAnalyzeViolation{}, nil
}
if err != nil {
return []packageAnalyzeViolation{}, fmt.Errorf("Error reading %s file: %v", filepath.Base(path), err)
}
Expand Down
23 changes: 23 additions & 0 deletions plugin/studio/studio_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,29 @@ func TestAnalyzeSuccessfully(t *testing.T) {
}
}

func TestFailedAnalyzeReturnsFailureStatus(t *testing.T) {
exec := utils.NewExecCustomProcess(1, "Analyze output", "There was an error", func(name string, args []string) {})
context := test.NewContextBuilder().
WithDefinition("studio", studioDefinition).
WithCommandPlugin(PackageAnalyzeCommand{exec}).
Build()

source := studioProjectDirectory()
result := test.RunCli([]string{"studio", "package", "analyze", "--source", source}, context)

stdout := map[string]interface{}{}
err := json.Unmarshal([]byte(result.StdOut), &stdout)
if err != nil {
t.Errorf("Failed to deserialize analyze command result: %v", err)
}
if stdout["status"] != "Failed" {
t.Errorf("Expected status to be Failed, but got: %v", result.StdOut)
}
if stdout["error"] != "There was an error" {
t.Errorf("Expected error to be set, but got: %v", result.StdOut)
}
}

func findViolation(violations []interface{}, errorCode string) map[string]interface{} {
var violation map[string]interface{}
for _, v := range violations {
Expand Down

0 comments on commit 3fea971

Please sign in to comment.