From 19a74df14dcfa257bb42921a3274a9e3ebdf2b41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20=C5=A0ar=C4=8Devi=C4=87?= Date: Thu, 28 Jan 2021 14:14:47 +0100 Subject: [PATCH] Log when parsing errors --- pkg/cli/evaluate.go | 4 ++++ pkg/pipelines/extract_when_list.go | 34 +++++++++++++++++++++++++++++- pkg/when/whencli/list_inputs.go | 3 --- test/e2e/change_in_invalid_when.rb | 4 ++-- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/pkg/cli/evaluate.go b/pkg/cli/evaluate.go index 6db0455..e8d1fe3 100644 --- a/pkg/cli/evaluate.go +++ b/pkg/cli/evaluate.go @@ -66,6 +66,10 @@ func check(err error) { os.Exit(1) } + if _, ok := err.(*logs.ErrorInvalidWhenExpression); ok { + os.Exit(1) + } + panic(err) } diff --git a/pkg/pipelines/extract_when_list.go b/pkg/pipelines/extract_when_list.go index 21aa011..5d0f7ed 100644 --- a/pkg/pipelines/extract_when_list.go +++ b/pkg/pipelines/extract_when_list.go @@ -3,6 +3,7 @@ package pipelines import ( "strconv" + logs "github.com/semaphoreci/spc/pkg/logs" when "github.com/semaphoreci/spc/pkg/when" whencli "github.com/semaphoreci/spc/pkg/when/whencli" ) @@ -29,9 +30,16 @@ func (e *whenExtractor) Parse() ([]when.WhenExpression, error) { expressions = append(expressions, e.Expression) } + res := []when.WhenExpression{} + requirments, err := whencli.ListInputs(expressions) if err != nil { - return []when.WhenExpression{}, err + return res, err + } + + err = e.verifyParsed(requirments) + if err != nil { + return res, err } for index := range e.list { @@ -41,6 +49,30 @@ func (e *whenExtractor) Parse() ([]when.WhenExpression, error) { return e.list, nil } +func (e *whenExtractor) verifyParsed(requirments []whencli.ListInputsResult) error { + var err error + + for index, r := range requirments { + if r.Error != "" { + loc := logs.Location{ + Path: e.list[index].Path, + File: e.pipeline.yamlPath, + } + + logError := logs.ErrorInvalidWhenExpression{ + Message: r.Error, + Location: loc, + } + + logs.Log(logError) + + err = &logError + } + } + + return err +} + func (e *whenExtractor) ExtractAutoCancel() { e.tryExtractingFromPath([]string{"auto_cancel", "queued", "when"}) e.tryExtractingFromPath([]string{"auto_cancel", "running", "when"}) diff --git a/pkg/when/whencli/list_inputs.go b/pkg/when/whencli/list_inputs.go index 5339a64..ba05bfb 100644 --- a/pkg/when/whencli/list_inputs.go +++ b/pkg/when/whencli/list_inputs.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "os" "os/exec" @@ -51,8 +50,6 @@ func prepareResults(expressions []string, results *gabs.Container) ([]ListInputs Inputs: el.Search("inputs"), Error: el.Search("error").Data().(string), }) - log.Println(index) - log.Println(el) } return result, nil diff --git a/test/e2e/change_in_invalid_when.rb b/test/e2e/change_in_invalid_when.rb index a9724ce..05a37e6 100644 --- a/test/e2e/change_in_invalid_when.rb +++ b/test/e2e/change_in_invalid_when.rb @@ -54,7 +54,7 @@ assert_eq(errors[0], { "type" => "ErrorInvalidWhenExpression", - "message" => "Invalid when expression: branch = 'master' and ahahahaha and change_in('/lib')", + "message" => "Invalid expression on the left of 'and' operator.", "location" => { "file" => ".semaphore/semaphore.yml", "path" => ["blocks", "0", "skip", "when"] @@ -63,7 +63,7 @@ assert_eq(errors[1], { "type" => "ErrorInvalidWhenExpression", - "message" => "Invalid when expression: branch =", + "message" => "Invalid or incomplete expression at the end of the line.", "location" => { "file" => ".semaphore/semaphore.yml", "path" => ["blocks", "1", "skip", "when"]