Add test case for native function returning error

A native function returning an error should be reported as a runtime
error, with a stack trace.
This commit is contained in:
Cam Hutchison 2017-12-17 20:43:07 +11:00 committed by Dave Cunningham
parent de6b2b8a55
commit c7a5b68f1c
3 changed files with 21 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package jsonnet
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
@ -81,6 +82,14 @@ var jsonToString = &NativeFunction{
}, },
} }
var nativeError = &NativeFunction{
Name: "nativeError",
Params: ast.Identifiers{},
Func: func(x []interface{}) (interface{}, error) {
return nil, errors.New("Native function error")
},
}
type jsonnetInput struct { type jsonnetInput struct {
name string name string
input []byte input []byte
@ -106,6 +115,7 @@ func runJsonnet(i jsonnetInput) jsonnetResult {
} }
vm.NativeFunction(jsonToString) vm.NativeFunction(jsonToString)
vm.NativeFunction(nativeError)
var output string var output string

10
testdata/native_error.golden vendored Normal file
View File

@ -0,0 +1,10 @@
RUNTIME ERROR: Native function error
-------------------------------------------------
testdata/native_error:1:1-28 $
std.native("nativeError")()
-------------------------------------------------
During evaluation

1
testdata/native_error.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.native("nativeError")()