From 9961c40cc8cd23a98b481ba0d0f9aa9b4ad1ac52 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Fri, 25 Oct 2024 08:46:34 +0200 Subject: [PATCH] Make `opa check` respect `--ignore` when `--bundle` flag is set Fixes #7136 Signed-off-by: Anders Eknert --- cmd/check.go | 8 ++++++++ cmd/check_test.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cmd/check.go b/cmd/check.go index 5fbb4e4f4e..db874812bd 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -7,6 +7,7 @@ package cmd import ( "fmt" "io" + "io/fs" "os" "github.com/spf13/cobra" @@ -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 @@ -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 { diff --git a/cmd/check_test.go b/cmd/check_test.go index fae90043ac..9914c43025 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -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