From 2f94fc7d1b230619af374e83441cd0b917e6a05b Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Wed, 2 Oct 2024 14:02:47 +0200 Subject: [PATCH] Fix nits (#1161) * build/do.rq: unconditionally re-run e2e tests * pkg/linter: use slices.Contains instead of util.Contains Signed-off-by: Stephan Renatus --- build/do.rq | 6 +++--- e2e/cli_test.go | 4 ++-- internal/util/util.go | 11 ----------- pkg/linter/linter.go | 16 ++++++++-------- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/build/do.rq b/build/do.rq index ca45df7d..a6b635a6 100755 --- a/build/do.rq +++ b/build/do.rq @@ -162,7 +162,8 @@ test if { } e2e if { - run("go test -tags e2e ./e2e") + # NOTE: e2e tests depend on the executable, so go should not cache their result (count=1 enforces that) + run("go test -tags e2e ./e2e -count=1") run("go test -tags integration ./internal/capabilities") } @@ -267,8 +268,7 @@ fetch_eopa_caps if { # only nonzero size files with JSON extensions. The size check is to # avoid long-tail edge cases where we crashed after opening the file # for writing but before committing any content. - eopa_caps_tree := {p: - f | + eopa_caps_tree := {p: f | f := rq.tree(eopa_caps_dir, {})[p] f.size != 0 f.ext == "json" diff --git a/e2e/cli_test.go b/e2e/cli_test.go index 200ad41c..ca277ff9 100644 --- a/e2e/cli_test.go +++ b/e2e/cli_test.go @@ -13,6 +13,7 @@ import ( "os/exec" "path/filepath" "runtime" + "slices" "strings" "testing" @@ -22,7 +23,6 @@ import ( "github.com/open-policy-agent/opa/tester" "github.com/styrainc/regal/internal/testutil" - "github.com/styrainc/regal/internal/util" "github.com/styrainc/regal/pkg/config" "github.com/styrainc/regal/pkg/report" ) @@ -367,7 +367,7 @@ func TestLintRuleNamingConventionFromCustomCategory(t *testing.T) { } for _, violation := range rep.Violations { - if !util.Contains(expectedViolations, violation.Description) { + if !slices.Contains(expectedViolations, violation.Description) { t.Errorf("unexpected violation: %s", violation.Description) } } diff --git a/internal/util/util.go b/internal/util/util.go index 8b400e16..64007943 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -20,17 +20,6 @@ func Keys[K comparable, V any](m map[K]V) []K { return ks } -// Contains checks if slice contains element. -func Contains[T comparable](s []T, e T) bool { - for _, v := range s { - if v == e { - return true - } - } - - return false -} - // NullToEmpty returns empty slice if provided slice is nil. func NullToEmpty[T any](a []T) []T { if a == nil { diff --git a/pkg/linter/linter.go b/pkg/linter/linter.go index 58f5b503..48876635 100644 --- a/pkg/linter/linter.go +++ b/pkg/linter/linter.go @@ -326,7 +326,7 @@ func (l Linter) Lint(ctx context.Context) (report.Report, error) { rulesSkippedCounter := 0 for _, notice := range regoReport.Notices { - if !util.Contains(finalReport.Notices, notice) { + if !slices.Contains(finalReport.Notices, notice) { finalReport.Notices = append(finalReport.Notices, notice) if notice.Severity != "none" { @@ -587,7 +587,7 @@ func (l Linter) paramsToRulesConfig() map[string]any { params["ignore_files"] = l.ignoreFiles } - return map[string]interface{}{ + return map[string]any{ "eval": map[string]any{ "params": params, }, @@ -944,7 +944,7 @@ func (l Linter) enabledGoRules() ([]rules.Rule, error) { if l.disableAll { for _, rule := range rules.AllGoRules(config.Config{}) { - if util.Contains(l.enableCategory, rule.Category()) || util.Contains(l.enable, rule.Name()) { + if slices.Contains(l.enableCategory, rule.Category()) || slices.Contains(l.enable, rule.Name()) { enabledGoRules = append(enabledGoRules, rule) } } @@ -954,7 +954,7 @@ func (l Linter) enabledGoRules() ([]rules.Rule, error) { if l.enableAll { for _, rule := range rules.AllGoRules(config.Config{}) { - if !util.Contains(l.disableCategory, rule.Category()) && !util.Contains(l.disable, rule.Name()) { + if !slices.Contains(l.disableCategory, rule.Category()) && !slices.Contains(l.disable, rule.Name()) { enabledGoRules = append(enabledGoRules, rule) } } @@ -969,23 +969,23 @@ func (l Linter) enabledGoRules() ([]rules.Rule, error) { for _, rule := range rules.AllGoRules(*conf) { // disabling specific rule has the highest precedence - if util.Contains(l.disable, rule.Name()) { + if slices.Contains(l.disable, rule.Name()) { continue } // likewise for enabling specific rule - if util.Contains(l.enable, rule.Name()) { + if slices.Contains(l.enable, rule.Name()) { enabledGoRules = append(enabledGoRules, rule) continue } // next highest precedence is disabling / enabling a category - if util.Contains(l.disableCategory, rule.Category()) { + if slices.Contains(l.disableCategory, rule.Category()) { continue } - if util.Contains(l.enableCategory, rule.Category()) { + if slices.Contains(l.enableCategory, rule.Category()) { enabledGoRules = append(enabledGoRules, rule) continue