Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix additional filter #209

Merged
merged 3 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}