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

rules: Add InputFromTextWithOptions for Go users #1278

Merged
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
11 changes: 11 additions & 0 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func InputFromText(fileName, text string) (Input, error) {
return NewInput(map[string]string{fileName: text}, map[string]*ast.Module{fileName: mod}), nil
}

// InputFromTextWithOptions creates a new Input from raw Rego text while
// respecting the provided options.
func InputFromTextWithOptions(fileName, text string, opts ast.ParserOptions) (Input, error) {
mod, err := ast.ParseModuleWithOpts(fileName, text, opts)
if err != nil {
return Input{}, fmt.Errorf("failed to parse module: %w", err)
}

return NewInput(map[string]string{fileName: text}, map[string]*ast.Module{fileName: mod}), nil
}

func AllGoRules(conf config.Config) []Rule {
return []Rule{
NewOpaFmtRule(conf),
Expand Down
40 changes: 40 additions & 0 deletions pkg/rules/rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rules

import (
"testing"

"github.com/open-policy-agent/opa/ast"
)

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

testCases := map[string]struct {
Module string
RegoVersion ast.RegoVersion
}{
"regov1": {
Module: `package test
p if { true }`,
RegoVersion: ast.RegoV1,
},
"regov0": {
Module: `package test
p { true }`,
RegoVersion: ast.RegoV0,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

_, err := InputFromTextWithOptions("p.rego", tc.Module, ast.ParserOptions{
RegoVersion: tc.RegoVersion,
})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
}
}
Loading