From 3fea97138353d2c6312acac724835040cdcd3e3a Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Thu, 19 Dec 2024 12:18:06 +0200 Subject: [PATCH] Add more tests for the analyze command --- plugin/studio/package_analyze_command.go | 3 +++ plugin/studio/studio_plugin_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/plugin/studio/package_analyze_command.go b/plugin/studio/package_analyze_command.go index 66dbdfa..fd1ff70 100644 --- a/plugin/studio/package_analyze_command.go +++ b/plugin/studio/package_analyze_command.go @@ -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) } diff --git a/plugin/studio/studio_plugin_test.go b/plugin/studio/studio_plugin_test.go index 3bb3f14..6cad143 100644 --- a/plugin/studio/studio_plugin_test.go +++ b/plugin/studio/studio_plugin_test.go @@ -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 {