rego: Fix printing duplication when parse errors length is 1

Signed-off-by: teselil <tzlil@datree.com>
This commit is contained in:
teselil
2023-04-18 01:51:17 +03:00
committed by Ashutosh Narkar
parent 04351d288b
commit 845652e115
2 changed files with 31 additions and 1 deletions
+8 -1
View File
@@ -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
}
+23
View File
@@ -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