Skip to content

Commit

Permalink
Make opa check respect --ignore when --bundle flag is set (#7137)
Browse files Browse the repository at this point in the history
Fixes #7136

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored Oct 25, 2024
1 parent 8e44b98 commit 1b797d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"fmt"
"io"
"io/fs"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,6 +88,7 @@ func checkModules(params checkParams, args []string) error {
WithSkipBundleVerification(true).
WithProcessAnnotation(true).
WithCapabilities(capabilities).
WithFilter(filterFromPaths(params.ignore)).
AsBundle(path)
if err != nil {
return err
Expand Down Expand Up @@ -130,6 +132,12 @@ func checkModules(params checkParams, args []string) error {
return nil
}

func filterFromPaths(paths []string) loader.Filter {
return func(abspath string, info fs.FileInfo, depth int) bool {
return loaderFilter{Ignore: paths}.Apply(abspath, info, depth)
}
}

func outputErrors(format string, err error) {
var out io.Writer
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ func TestCheckIgnoresNonRegoFiles(t *testing.T) {
})
}

func TestCheckIgnoreBundleMode(t *testing.T) {
t.Parallel()

files := map[string]string{
"ignore.rego": `invalid rego`,
"include.rego": `package valid`,
}

test.WithTempFS(files, func(root string) {
params := newCheckParams()

params.ignore = []string{"ignore.rego"}
params.bundleMode = true

err := checkModules(params, []string{root})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}

func TestCheckFailsOnInvalidRego(t *testing.T) {
files := map[string]string{
"test.rego": `package test
Expand Down

0 comments on commit 1b797d9

Please sign in to comment.