Basic support for running jsonnet command in tests (#167)

A step towards a unified test suite. In particular this
lets us use C++ version with Go tests. The problem remains
that errors (both static and runtime) may differ between implementations.
This will require special handling. C++ version seems to pass all
"positive" tests. Go version also has this problem (error formatter used in tests is different).

Also native functions etc. are not handled in any way at the moment.
This commit is contained in:
Stanisław Barzowski 2018-01-11 03:27:44 +01:00 committed by Dave Cunningham
parent 7c8f4d0b12
commit 34a24d8bcb

View File

@ -22,6 +22,7 @@ import (
"errors"
"flag"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"testing"
@ -31,6 +32,7 @@ import (
)
var update = flag.Bool("update", false, "update .golden files")
var jsonnetCmd = flag.String("cmd", "", "path to jsonnet command (if not specified or empty, internal implementation is used)")
// TODO(sbarzowski) figure out how to measure coverage on the external tests
@ -103,7 +105,7 @@ type jsonnetResult struct {
isError bool
}
func runJsonnet(i jsonnetInput) jsonnetResult {
func runInternalJsonnet(i jsonnetInput) jsonnetResult {
vm := MakeVM()
errFormatter := termErrorFormatter{pretty: true, maxStackTraceSize: 9}
@ -138,6 +140,43 @@ func runJsonnet(i jsonnetInput) jsonnetResult {
}
}
func runJsonnetCommand(i jsonnetInput) jsonnetResult {
// TODO(sbarzowski) Special handling of errors (which may differ between versions)
input := bytes.NewBuffer(i.input)
var output bytes.Buffer
isError := false
cmd := exec.Cmd{
Path: *jsonnetCmd,
Stdin: input,
Stdout: &output,
Stderr: &output,
Args: []string{"jsonnet", "-"},
}
err := cmd.Run()
if err != nil {
switch err := err.(type) {
case *exec.ExitError:
// It finished with non-zero exit code
isError = true
default:
// We weren't able to run it
panic(err)
}
}
return jsonnetResult{
output: output.String(),
isError: isError,
}
}
func runJsonnet(i jsonnetInput) jsonnetResult {
if jsonnetCmd != nil && *jsonnetCmd != "" {
return runJsonnetCommand(i)
} else {
return runInternalJsonnet(i)
}
}
func runTest(t *testing.T, test *mainTest) {
read := func(file string) []byte {