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

Fix location for multivalue rules with generated bodies #7129

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
2 changes: 2 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ func (p *Parser) parseRules() []*Rule {
case usesContains:
rule.Body = NewBody(NewExpr(BooleanTerm(true).SetLocation(rule.Location)).SetLocation(rule.Location))
rule.generatedBody = true
rule.Location = rule.Head.Location

return []*Rule{&rule}

default:
Expand Down
23 changes: 23 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4112,6 +4112,29 @@ func TestWildcards(t *testing.T) {
})
}

// https://github.com/open-policy-agent/opa/issues/7128
func TestParseMultiValueRuleGeneratedBodyLocationText(t *testing.T) {
t.Parallel()

mod := `package test

import rego.v1

foo contains "bar"
`

parsed, err := ParseModule("test.rego", mod)
if err != nil {
t.Fatal(err)
}

text := string(parsed.Rules[0].Location.Text)

if text != `foo contains "bar"` {
t.Errorf("Expected rule location text to be %q but got %q", `foo contains "bar"`, text)
}
}

func TestRuleFromBodyJSONOptions(t *testing.T) {
tests := []string{
`pi = 3.14159`,
Expand Down