-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
jsonnet_test.go
86 lines (80 loc) · 1.59 KB
/
jsonnet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package lambroll_test
import (
"encoding/json"
"testing"
"github.com/fujiwara/lambroll"
"github.com/google/go-cmp/cmp"
"github.com/google/go-jsonnet"
)
var testSrcJsonnet = `
local env = std.native("env");
local must_env = std.native("must_env");
{
foo: env("FOO", "default"),
bar: must_env("BAR"),
}
`
var testCaseJsonnetNativeFuncs = []struct {
name string
env map[string]string
expected map[string]string
errExpected bool
}{
{
name: "env FOO not set",
env: map[string]string{
"BAR": "bar",
},
expected: map[string]string{
"foo": "default",
"bar": "bar",
},
},
{
name: "env FOO set",
env: map[string]string{
"FOO": "foo",
"BAR": "bar",
},
expected: map[string]string{
"foo": "foo",
"bar": "bar",
},
},
{
name: "must_env BAR not set",
env: map[string]string{
"FOO": "foo",
},
errExpected: true,
},
}
func TestJsonnetNativeFuncs(t *testing.T) {
vm := jsonnet.MakeVM()
for _, f := range lambroll.DefaultJsonnetNativeFuncs() {
vm.NativeFunction(f)
}
for _, c := range testCaseJsonnetNativeFuncs {
t.Run(c.name, func(t *testing.T) {
for k, v := range c.env {
t.Setenv(k, v)
}
out, err := vm.EvaluateAnonymousSnippet("test.jsonnet", testSrcJsonnet)
if c.errExpected {
if err == nil {
t.Fatal("expected error")
}
return
} else if err != nil {
t.Fatal(err)
}
var got map[string]string
if err := json.Unmarshal([]byte(out), &got); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(c.expected, got); diff != "" {
t.Errorf("(-expected, +got)\n%s", diff)
}
})
}
}