Skip to content

Commit

Permalink
Merge pull request #209 from doitintl/fix-additional-filter
Browse files Browse the repository at this point in the history
Fix additional filter
  • Loading branch information
stepanstipl authored Aug 27, 2021
2 parents 3669c5e + b79c73b commit 430d1ad
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion pkg/judge/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Expand Down
26 changes: 24 additions & 2 deletions pkg/printer/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 430d1ad

Please sign in to comment.