mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
rego: Fix printing duplication when parse errors length is 1
Signed-off-by: teselil <tzlil@datree.com>
This commit is contained in:
+8
-1
@@ -1731,7 +1731,14 @@ func (r *Rego) parseModules(ctx context.Context, txn storage.Transaction, m metr
|
||||
for _, module := range r.modules {
|
||||
p, err := module.Parse()
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
switch errorWithType := err.(type) {
|
||||
case ast.Errors:
|
||||
for _, e := range errorWithType {
|
||||
errs = append(errs, e)
|
||||
}
|
||||
default:
|
||||
errs = append(errs, errorWithType)
|
||||
}
|
||||
}
|
||||
r.parsedModules[module.filename] = p
|
||||
}
|
||||
|
||||
@@ -937,6 +937,29 @@ func TestPrepareAndEvalOriginal(t *testing.T) {
|
||||
assertEval(t, r, "[[2]]")
|
||||
}
|
||||
|
||||
func TestPrepareAndEvalOnlyOneErrorOccurredPrintOnce(t *testing.T) {
|
||||
module := `
|
||||
package test
|
||||
package test
|
||||
x = input.y
|
||||
`
|
||||
|
||||
r := New(
|
||||
Query("data.test.x"),
|
||||
Module("", module),
|
||||
Package("foo"),
|
||||
Input(map[string]int{"y": 2}),
|
||||
)
|
||||
|
||||
_, err := r.PrepareForEval(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("Expected error but got nil")
|
||||
}
|
||||
if strings.Count(err.Error(), "1 error occurred") > 1 {
|
||||
t.Fatalf("Expected to print '1 error occurred' only once")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareAndEvalNewPrintHook(t *testing.T) {
|
||||
module := `
|
||||
package test
|
||||
|
||||
Reference in New Issue
Block a user