mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
3be1d08b87
golint is deprecated. The author of the code no longer supports the codebase. golangci-lint is faster than golint, and is in use by other opa repositories (e.g. Gatekeeper). This commit changes tools.go to reference golangci (so it ends up in vendor) and modifies check-lint to use golangci instead. Breaking API Changes: - plugins/rest/rest.go: Fix typo "AllowInsureTLS" -> "AllowInsecureTLS" - storage/errors.go: Removed unused IndexingNotSupportedErr Signed-off-by: Will Beason <willbeason@google.com>
178 lines
3.3 KiB
Go
178 lines
3.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/util/test"
|
|
)
|
|
|
|
const formatted = `package test
|
|
|
|
p {
|
|
a == 1
|
|
true
|
|
1 + 3
|
|
}
|
|
`
|
|
|
|
func TestFmtFormatFile(t *testing.T) {
|
|
params := fmtCommandParams{}
|
|
var stdout bytes.Buffer
|
|
|
|
unformatted := `
|
|
package test
|
|
|
|
p { a == 1; true
|
|
1 + 3
|
|
}
|
|
|
|
|
|
`
|
|
|
|
files := map[string]string{
|
|
"policy.rego": unformatted,
|
|
}
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
policyFile := filepath.Join(path, "policy.rego")
|
|
info, err := os.Stat(policyFile)
|
|
err = formatFile(¶ms, &stdout, policyFile, info, err)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
}
|
|
|
|
actual := stdout.String()
|
|
if actual != formatted {
|
|
t.Fatalf("Expected:%s\n\nGot:\n%s\n\n", formatted, actual)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFmtFormatFileNoChanges(t *testing.T) {
|
|
params := fmtCommandParams{}
|
|
var stdout bytes.Buffer
|
|
|
|
files := map[string]string{
|
|
"policy.rego": formatted,
|
|
}
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
policyFile := filepath.Join(path, "policy.rego")
|
|
info, err := os.Stat(policyFile)
|
|
err = formatFile(¶ms, &stdout, policyFile, info, err)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
}
|
|
|
|
actual := stdout.String()
|
|
if actual != formatted {
|
|
t.Fatalf("Expected:%s\n\nGot:\n%s\n\n", formatted, actual)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFmtFormatFileDiff(t *testing.T) {
|
|
params := fmtCommandParams{
|
|
diff: true,
|
|
}
|
|
var stdout bytes.Buffer
|
|
|
|
files := map[string]string{
|
|
"policy.rego": formatted,
|
|
}
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
policyFile := filepath.Join(path, "policy.rego")
|
|
info, err := os.Stat(policyFile)
|
|
err = formatFile(¶ms, &stdout, policyFile, info, err)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
}
|
|
|
|
actual := stdout.String()
|
|
|
|
if len(actual) > 0 {
|
|
t.Fatalf("Expected no output, got:\n%s\n\n", actual)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFmtFormatFileList(t *testing.T) {
|
|
params := fmtCommandParams{
|
|
list: true,
|
|
}
|
|
var stdout bytes.Buffer
|
|
|
|
files := map[string]string{
|
|
"policy.rego": formatted,
|
|
}
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
policyFile := filepath.Join(path, "policy.rego")
|
|
info, err := os.Stat(policyFile)
|
|
err = formatFile(¶ms, &stdout, policyFile, info, err)
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
}
|
|
|
|
actual := strings.TrimSpace(stdout.String())
|
|
|
|
if len(actual) > 0 {
|
|
t.Fatalf("Expected no output, got:\n%s\n\n", actual)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFmtFailFileNoChanges(t *testing.T) {
|
|
params := fmtCommandParams{
|
|
fail: true,
|
|
}
|
|
|
|
files := map[string]string{
|
|
"policy.rego": formatted,
|
|
}
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
policyFile := filepath.Join(path, "policy.rego")
|
|
info, err := os.Stat(policyFile)
|
|
err = formatFile(¶ms, ioutil.Discard, policyFile, info, err)
|
|
if err != nil {
|
|
t.Fatalf("Expected error but did not receive one")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFmtFailFileChanges(t *testing.T) {
|
|
params := fmtCommandParams{
|
|
fail: true,
|
|
}
|
|
|
|
unformatted := `
|
|
package test
|
|
|
|
p { a == 1; true
|
|
1 + 3
|
|
}
|
|
|
|
|
|
`
|
|
|
|
files := map[string]string{
|
|
"policy.rego": unformatted,
|
|
}
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
policyFile := filepath.Join(path, "policy.rego")
|
|
info, err := os.Stat(policyFile)
|
|
err = formatFile(¶ms, ioutil.Discard, policyFile, info, err)
|
|
if err == nil {
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
}
|
|
})
|
|
}
|