From f9a6f18462e291c48e323cf00bc356d108fb37ad Mon Sep 17 00:00:00 2001 From: Stepan Stipl Date: Fri, 27 Aug 2021 14:01:00 +0100 Subject: [PATCH 1/3] fix: Continue if judge can't parse version of result Not all results are expected to have valid versions, such as additional resources. fixes #204 --- pkg/judge/rego.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/judge/rego.go b/pkg/judge/rego.go index b93da0e5..fa7476b5 100644 --- a/pkg/judge/rego.go +++ b/pkg/judge/rego.go @@ -55,7 +55,7 @@ func (j *RegoJudge) Eval(input []map[string]interface{}) ([]Result, error) { since, err := goversion.NewVersion(m["Since"].(string)) if err != nil { - return nil, err + log.Debug().Msgf("Failed to parse version: %s", err) } results = append(results, Result{ From 9f650fc6ce78a81369ed98fdc409c1737f20cc60 Mon Sep 17 00:00:00 2001 From: Stepan Stipl Date: Fri, 27 Aug 2021 15:07:43 +0100 Subject: [PATCH 2/3] fix: Keep results with unknown version Not all results are expected to have valid versions, such as additional resources. fixes #204 --- pkg/printer/filter.go | 2 +- pkg/printer/filter_test.go | 26 ++++++++++++++++++++++++-- pkg/printer/json_test.go | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/printer/filter.go b/pkg/printer/filter.go index 49dfd0d2..aa858438 100644 --- a/pkg/printer/filter.go +++ b/pkg/printer/filter.go @@ -11,7 +11,7 @@ func FilterNonRelevantResults(results []judge.Result, tv *goversion.Version) ([] filtered := []judge.Result{} for i := range results { - if results[i].Since.LessThanOrEqual(tv) { + if results[i].Since == nil || results[i].Since.LessThanOrEqual(tv) { filtered = append(filtered, results[i]) } } diff --git a/pkg/printer/filter_test.go b/pkg/printer/filter_test.go index 77a12b7a..53a42e60 100644 --- a/pkg/printer/filter_test.go +++ b/pkg/printer/filter_test.go @@ -30,12 +30,21 @@ var testInput []judge.Result = []judge.Result{ ReplaceWith: "testReplaceWith2", Since: testVersion2, }, + { + Name: "testName3", + Kind: "testKind3", + Namespace: "testNamespace3", + ApiVersion: "v1", + RuleSet: "testRuleset3", + ReplaceWith: "testReplaceWith3", + Since: nil, + }, } func TestFilterNonRelevantResults(t *testing.T) { filterVersion, _ := goversion.NewVersion("2.0.0") - results, err := FilterNonRelevantResults(testInput, filterVersion) + results, err := FilterNonRelevantResults(testInput[0:2], filterVersion) if err != nil { t.Fatalf("failed to filter results: %s", err) } @@ -60,7 +69,20 @@ func TestFilterNonRelevantResultsEmpty(t *testing.T) { } } -func TestFilterNonRelevantResultsNilVersion(t *testing.T) { +func TestFilterNonRelevantResultsWithNilVersion(t *testing.T) { + filterVersion, _ := goversion.NewVersion("2.0.0") + + results, err := FilterNonRelevantResults(testInput[2:3], filterVersion) + if err != nil { + t.Fatalf("failed to filter results: %s", err) + } + + if len(results) != 1 { + t.Errorf("expected 1 results after filter, got %d intead", len(results)) + } +} + +func TestFilterNonRelevantResultsNilTargetVersion(t *testing.T) { var filterVersion *goversion.Version results, err := FilterNonRelevantResults(testInput, filterVersion) diff --git a/pkg/printer/json_test.go b/pkg/printer/json_test.go index 88e4f934..77ab2332 100644 --- a/pkg/printer/json_test.go +++ b/pkg/printer/json_test.go @@ -20,7 +20,7 @@ func TestJsonPopulateOutput(t *testing.T) { if err != nil { t.Fatal(err) } - if len(j) != 2 { + if len(j) != len(testInput) { t.Error("wrong number of results") } } From b79c73b89b944005bd100c21a9f3116525a1ea01 Mon Sep 17 00:00:00 2001 From: Stepan Stipl Date: Fri, 27 Aug 2021 16:17:33 +0100 Subject: [PATCH 3/3] chore: Fix failing pre-commit golang fmt --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f95223ce..d97a4f21 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,6 +18,6 @@ repos: hooks: - id: checkmake - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks - rev: master + rev: v2.1.0 hooks: - id: pretty-format-golang