-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathfuncs_test.go
272 lines (219 loc) · 7.15 KB
/
funcs_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package native
import (
"encoding/json"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"testing"
jsonnet "github.com/google/go-jsonnet"
"github.com/stretchr/testify/assert"
)
// callNative calls a native function used by jsonnet VM.
func callNative(name string, data []interface{}) (res interface{}, err error, callerr error) {
for _, fun := range Funcs() {
if fun.Name == name {
// Call the function
ret, err := fun.Func(data)
return ret, err, nil
}
}
return nil, nil, fmt.Errorf("could not find native function %s", name)
}
// callVMNative calls a native function used by jsonnet VM that requires access to the VM resource
func callVMNative(name string, data []interface{}) (res interface{}, err error, callerr error) {
vm := jsonnet.MakeVM()
for _, fun := range VMFuncs(vm) {
if fun.Name == name {
// Call the function
ret, err := fun.Func(data)
return ret, err, nil
}
}
return nil, nil, fmt.Errorf("could not find VM native function %s", name)
}
func TestSha256(t *testing.T) {
ret, err, callerr := callNative("sha256", []interface{}{"foo"})
assert.Empty(t, callerr)
assert.Equal(t, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", ret)
assert.Empty(t, err)
}
func TestParseJSONEmptyDict(t *testing.T) {
ret, err, callerr := callNative("parseJson", []interface{}{"{}"})
assert.Empty(t, callerr)
assert.Equal(t, map[string]interface{}{}, ret)
assert.Empty(t, err)
}
func TestParseJSONkeyValuet(t *testing.T) {
ret, err, callerr := callNative("parseJson", []interface{}{"{\"a\": 47}"})
assert.Empty(t, callerr)
assert.Equal(t, map[string]interface{}{"a": 47.0}, ret)
assert.Empty(t, err)
}
func TestParseJSONInvalid(t *testing.T) {
ret, err, callerr := callNative("parseJson", []interface{}{""})
assert.Empty(t, callerr)
assert.Empty(t, ret)
assert.IsType(t, &json.SyntaxError{}, err)
}
func TestParseYAMLEmpty(t *testing.T) {
ret, err, callerr := callNative("parseYaml", []interface{}{""})
assert.Empty(t, callerr)
assert.Equal(t, []interface{}{}, ret)
assert.Empty(t, err)
}
func TestParseYAMLKeyValue(t *testing.T) {
ret, err, callerr := callNative("parseYaml", []interface{}{"a: 47"})
assert.Empty(t, callerr)
assert.Equal(t, []interface{}{map[string]interface{}{"a": 47.0}}, ret)
assert.Empty(t, err)
}
func TestParseYAMLInvalid(t *testing.T) {
ret, err, callerr := callNative("parseYaml", []interface{}{"'"})
assert.Empty(t, callerr)
assert.Empty(t, ret)
assert.NotEmpty(t, err)
}
func TestManifestJSONFromJSON(t *testing.T) {
ret, err, callerr := callNative("manifestJsonFromJson", []interface{}{"{}", float64(4)})
assert.Empty(t, callerr)
assert.Equal(t, "{}\n", ret)
assert.Empty(t, err)
}
func TestManifestJSONFromJSONReindent(t *testing.T) {
ret, err, callerr := callNative("manifestJsonFromJson", []interface{}{"{ \"a\": 47}", float64(4)})
assert.Empty(t, callerr)
assert.Equal(t, "{\n \"a\": 47\n}\n", ret)
assert.Empty(t, err)
}
func TestManifestJSONFromJSONInvalid(t *testing.T) {
ret, err, callerr := callNative("manifestJsonFromJson", []interface{}{"", float64(4)})
assert.Empty(t, callerr)
assert.Empty(t, ret)
assert.NotEmpty(t, err)
}
func TestManifestYAMLFromJSONEmpty(t *testing.T) {
ret, err, callerr := callNative("manifestYamlFromJson", []interface{}{"{}"})
assert.Empty(t, callerr)
assert.Equal(t, "{}\n", ret)
assert.Empty(t, err)
}
func TestManifestYAMLFromJSONKeyValue(t *testing.T) {
ret, err, callerr := callNative("manifestYamlFromJson", []interface{}{"{ \"a\": 47}"})
assert.Empty(t, callerr)
assert.Equal(t, "a: 47\n", ret)
assert.Empty(t, err)
}
func TestManifestYAMLFromJSONList(t *testing.T) {
ret, err, callerr := callNative("manifestYamlFromJson", []interface{}{`{ "list": ["a", "b", "c"]}`})
assert.Empty(t, callerr)
assert.Equal(t, `list:
- a
- b
- c
`, ret)
assert.Empty(t, err)
}
func TestManifestYAMLFromJSONInvalid(t *testing.T) {
ret, err, callerr := callNative("manifestYamlFromJson", []interface{}{""})
assert.Empty(t, callerr)
assert.Empty(t, ret)
assert.NotEmpty(t, err)
}
func TestEscapeStringRegex(t *testing.T) {
ret, err, callerr := callNative("escapeStringRegex", []interface{}{""})
assert.Empty(t, callerr)
assert.Equal(t, "", ret)
assert.Empty(t, err)
}
func TestEscapeStringRegexValue(t *testing.T) {
ret, err, callerr := callNative("escapeStringRegex", []interface{}{"([0-9]+).*\\s"})
assert.Empty(t, callerr)
assert.Equal(t, "\\(\\[0-9\\]\\+\\)\\.\\*\\\\s", ret)
assert.Empty(t, err)
}
func TestEscapeStringRegexInvalid(t *testing.T) {
ret, err, callerr := callNative("escapeStringRegex", []interface{}{"([0-9]+"})
assert.Empty(t, callerr)
assert.Equal(t, "\\(\\[0-9\\]\\+", ret)
assert.Empty(t, err)
}
func TestRegexMatch(t *testing.T) {
ret, err, callerr := callNative("regexMatch", []interface{}{"", "a"})
assert.Empty(t, callerr)
assert.Equal(t, true, ret)
assert.Empty(t, err)
}
func TestRegexMatchNoMatch(t *testing.T) {
ret, err, callerr := callNative("regexMatch", []interface{}{"a", "b"})
assert.Empty(t, callerr)
assert.Equal(t, false, ret)
assert.Empty(t, err)
}
func TestRegexMatchInvalidRegex(t *testing.T) {
ret, err, callerr := callNative("regexMatch", []interface{}{"[0-", "b"})
assert.Empty(t, callerr)
assert.Empty(t, ret)
assert.NotEmpty(t, err)
}
func TestRegexSubstNoChange(t *testing.T) {
ret, err, callerr := callNative("regexSubst", []interface{}{"a", "b", "c"})
assert.Empty(t, callerr)
assert.Equal(t, "b", ret)
assert.Empty(t, err)
}
func TestRegexSubstValid(t *testing.T) {
ret, err, callerr := callNative("regexSubst", []interface{}{"p[^m]*", "pm", "poe"})
assert.Empty(t, callerr)
assert.Equal(t, "poem", ret)
assert.Empty(t, err)
}
func TestRegexSubstInvalid(t *testing.T) {
ret, err, callerr := callNative("regexSubst", []interface{}{"p[^m*", "pm", "poe"})
assert.Empty(t, callerr)
assert.Empty(t, ret)
assert.NotEmpty(t, err)
}
func TestImportFiles(t *testing.T) {
cwd, err := os.Getwd()
assert.NoError(t, err)
tempDir := t.TempDir()
defer func() {
if err := os.Chdir(cwd); err != nil {
panic(err)
}
}()
err = os.Chdir(tempDir)
assert.NoError(t, err)
importDirName := "imports"
importDir := filepath.Join(tempDir, importDirName)
err = os.Mkdir(importDir, 0750)
assert.NoError(t, err)
importMap := map[string]string{
"test1.libsonnet": "{ test: 1 }",
"test2.libsonnet": "{ test: 2 }",
}
excludeMap := map[string]string{
"skip1.libsonnet": `{ test: error "should not be included" }`,
"skip2.libsonnet": `{ test: error "should not be included" }`,
}
expectedJson := `{"test1.libsonnet":{"test":1},"test2.libsonnet":{"test":2}}`
allMap := make(map[string]string)
maps.Copy(allMap, importMap)
maps.Copy(allMap, excludeMap)
for fName, content := range allMap {
fPath := filepath.Join(importDir, fName)
err = os.WriteFile(fPath, []byte(content), 0644)
assert.NoError(t, err)
}
opts := make(map[string]interface{})
opts["calledFrom"] = filepath.Join(tempDir, "main.jsonnet")
opts["exclude"] = slices.Collect(maps.Keys(excludeMap))
ret, err, callerr := callVMNative("importFiles", []interface{}{importDirName, opts})
assert.NoError(t, err)
assert.Nil(t, callerr)
retJson, err := json.Marshal(ret)
assert.NoError(t, err)
assert.Equal(t, expectedJson, string(retJson))
}