From 34a24d8bcb8844be381afed404753c60b979ed5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Barzowski?= Date: Thu, 11 Jan 2018 03:27:44 +0100 Subject: [PATCH] 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. --- main_test.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index d9ecaec..44542c9 100644 --- a/main_test.go +++ b/main_test.go @@ -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 {