Skip to content

Commit

Permalink
Fix nits (#1161)
Browse files Browse the repository at this point in the history
* build/do.rq: unconditionally re-run e2e tests
* pkg/linter: use slices.Contains instead of util.Contains

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus authored Oct 2, 2024
1 parent a55d2f4 commit 2f94fc7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
6 changes: 3 additions & 3 deletions build/do.rq
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"

Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
Expand Down
11 changes: 0 additions & 11 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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
Expand Down

0 comments on commit 2f94fc7

Please sign in to comment.