forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars_test.go
86 lines (83 loc) · 2.12 KB
/
vars_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 runn
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestEvaluateSchema(t *testing.T) {
td := t.TempDir()
brokenJson, err := os.Create(filepath.Join(td, "broken.json"))
if err != nil {
t.Fatal(err)
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tests := []struct {
value any
store map[string]any
want any
wantErr bool
}{
{1, nil, 1, false},
{[]string{"1"}, nil, []string{"1"}, false},
{"string", nil, "string", false},
{"json://testdata/vars.json", nil, map[string]any{"foo": "test", "bar": float64(1)}, false},
{"yaml://testdata/vars.yaml", nil, map[string]any{"foo": "test", "bar": uint64(1), "baz": float64(2.5)}, false},
{"yaml://testdata/vars.yml", nil, map[string]any{"foo": "test", "bar": uint64(1), "baz": float64(2.5)}, false},
{"json://not_exists.json", nil, "json://not_exists.json", true},
{"json://" + brokenJson.Name(), nil, "json://" + brokenJson.Name(), true},
{
"json://testdata/non_template.json",
map[string]any{"vars": map[string]any{"foo": "test", "bar": 1}},
map[string]any{"foo": "{{.vars.foo -}}", "bar": float64(1)},
false,
},
{
"json://testdata/template.json.template",
map[string]any{"vars": map[string]any{"foo": "test", "bar": 1}},
map[string]any{"foo": "test", "bar": float64(1)},
false,
},
{
"yaml://testdata/template.yml.template",
map[string]any{"vars": map[string]any{"foo": "test", "bar": 1}},
map[string]any{"foo": "test", "bar": uint64(1)},
false,
},
{
"json://testdata/newline.json",
map[string]any{},
map[string]any{"foo": "abc\ndef", "bar": "abc\n\ndef"},
false,
},
{
"json://testdata/invalid_ext.js",
map[string]any{},
"",
true,
},
}
for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()
got, err := evaluateSchema(tt.value, wd, tt.store)
if err != nil {
if !tt.wantErr {
t.Errorf("unexpected error: %s", err)
}
return
}
if tt.wantErr {
t.Error("want error")
}
if diff := cmp.Diff(got, tt.want, nil); diff != "" {
t.Errorf("%s", diff)
}
})
}
}