Skip to content

Commit

Permalink
Basic support for running jsonnet command in tests (bazel-contrib#167)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sbarzowski authored and sparkprime committed Jan 11, 2018
1 parent 7c8f4d0 commit 34a24d8
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"flag"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand All @@ -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

Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 34a24d8

Please sign in to comment.