mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
afb30d3f9d
Brace yourselves! For there are many touched files here. No changes in semantics however. Spent a long time trying out the various optional rules gocritic provides, and settled for a few of them. There are more I really like, but that would take many hours to address across the codebase. Perhaps others find gocritic too pedantic? If so, we can merge the fixes without enabling the rule. Signed-off-by: Anders Eknert <anders@styra.com>
98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
// Copyright 2024 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ast
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCompile_DefaultRegoVersion(t *testing.T) {
|
|
tests := []struct {
|
|
note string
|
|
modules map[string]*Module
|
|
expErrs []string
|
|
}{
|
|
{
|
|
note: "no module rego-version, no v1 violations",
|
|
modules: map[string]*Module{
|
|
"test": {
|
|
Package: MustParsePackage(`package test`),
|
|
Imports: MustParseImports(`import data.foo
|
|
import data.bar`),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
note: "no module rego-version, v1 violations", // default is v0, no errors expected
|
|
modules: map[string]*Module{
|
|
"test": {
|
|
Package: MustParsePackage(`package test`),
|
|
Imports: MustParseImports(`import data.foo
|
|
import data.bar as foo`),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
note: "v0 module, v1 violations",
|
|
modules: map[string]*Module{
|
|
"test": MustParseModuleWithOpts(`package test
|
|
import data.foo
|
|
import data.bar as foo`,
|
|
ParserOptions{RegoVersion: RegoV0}),
|
|
},
|
|
},
|
|
{
|
|
note: "v1 module, v1 violations",
|
|
modules: map[string]*Module{
|
|
"test": MustParseModuleWithOpts(`package test
|
|
import data.foo
|
|
import data.bar as foo`,
|
|
ParserOptions{RegoVersion: RegoV1}),
|
|
},
|
|
expErrs: []string{
|
|
"3:7: rego_compile_error: import must not shadow import data.foo",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.note, func(t *testing.T) {
|
|
compiler := NewCompiler()
|
|
|
|
compiler.Compile(tc.modules)
|
|
|
|
if len(tc.expErrs) > 0 {
|
|
assertErrors(t, compiler.Errors, tc.expErrs)
|
|
} else if len(compiler.Errors) > 0 {
|
|
t.Fatalf("Unexpected errors: %v", compiler.Errors)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertErrors(t *testing.T, actual Errors, expected []string) {
|
|
t.Helper()
|
|
if len(expected) != len(actual) {
|
|
t.Fatalf("Expected %d errors, got %d:\n\n%s\n", len(expected), len(actual), actual.Error())
|
|
}
|
|
incorrectErrs := false
|
|
for _, e := range expected {
|
|
found := false
|
|
for _, actual := range actual {
|
|
if strings.Contains(actual.Error(), e) {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
incorrectErrs = true
|
|
}
|
|
}
|
|
if incorrectErrs {
|
|
t.Fatalf("Expected errors:\n\n%s\n\nGot:\n\n%s\n", expected, actual.Error())
|
|
}
|
|
}
|