mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
4f323e0597
There was an issue a little while back where we had a panic in the command but there is zero test coverage exercising it... so we never noticed. This at least will get us some bare-bones test coverage to ensure it isn't crashing on some basic use-cases. Signed-off-by: Patrick East <east.patrick@gmail.com>
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/util/test"
|
|
)
|
|
|
|
func TestParseExit0(t *testing.T) {
|
|
|
|
files := map[string]string{
|
|
"x.rego": `package x
|
|
|
|
p = 1
|
|
`,
|
|
}
|
|
errc, _, stderr := testParse(t, files)
|
|
if errc != 0 {
|
|
t.Fatalf("Expected exit code 0, got %v", errc)
|
|
}
|
|
if len(stderr) > 0 {
|
|
t.Fatalf("Expected no stderr output, got:\n%s\n", string(stderr))
|
|
}
|
|
}
|
|
|
|
func TestParseExit1(t *testing.T) {
|
|
|
|
files := map[string]string{
|
|
"x.rego": `???`,
|
|
}
|
|
errc, _, stderr := testParse(t, files)
|
|
if errc != 1 {
|
|
t.Fatalf("Expected exit code 1, got %v", errc)
|
|
}
|
|
if len(stderr) == 0 {
|
|
t.Fatalf("Expected output in stderr")
|
|
}
|
|
}
|
|
|
|
// Runs parse and returns the exit code, stdout, and stderr contents
|
|
func testParse(t *testing.T, files map[string]string) (int, []byte, []byte) {
|
|
t.Helper()
|
|
|
|
stdout := new(bytes.Buffer)
|
|
stderr := new(bytes.Buffer)
|
|
var errc int
|
|
|
|
test.WithTempFS(files, func(path string) {
|
|
var args []string
|
|
for file := range files {
|
|
args = append(args, filepath.Join(path, file))
|
|
}
|
|
errc = parse(args, stdout, stderr)
|
|
})
|
|
|
|
return errc, stdout.Bytes(), stderr.Bytes()
|
|
}
|