forked from hashicorp/hcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tryfunc_test.go
264 lines (250 loc) · 6.66 KB
/
tryfunc_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tryfunc
import (
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
func TestTryFunc(t *testing.T) {
tests := map[string]struct {
expr string
vars map[string]cty.Value
want cty.Value
wantErr string
}{
"one argument succeeds": {
`try(1)`,
nil,
cty.NumberIntVal(1),
``,
},
"one marked argument succeeds": {
`try(sensitive)`,
map[string]cty.Value{
"sensitive": cty.StringVal("secret").Mark("porpoise"),
},
cty.StringVal("secret").Mark("porpoise"),
``,
},
"two arguments, first succeeds": {
`try(1, 2)`,
nil,
cty.NumberIntVal(1),
``,
},
"two arguments, first fails": {
`try(nope, 2)`,
nil,
cty.NumberIntVal(2),
``,
},
"two arguments, first depends on unknowns": {
`try(unknown, 2)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.Number),
},
cty.DynamicVal, // can't proceed until first argument is known
``,
},
"two arguments, first succeeds and second depends on unknowns": {
`try(1, unknown)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.Number),
},
cty.NumberIntVal(1), // we know 1st succeeds, so it doesn't matter that 2nd is unknown
``,
},
"two arguments, first depends on unknowns deeply": {
`try(has_unknowns, 2)`,
map[string]cty.Value{
"has_unknowns": cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}),
},
cty.DynamicVal, // can't proceed until first argument is wholly known
``,
},
"two arguments, first traverses through an unkown": {
`try(unknown.baz, 2)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.Map(cty.String)),
},
cty.DynamicVal, // can't proceed until first argument is wholly known
``,
},
"two arguments, both marked, first succeeds": {
`try(sensitive, other)`,
map[string]cty.Value{
"sensitive": cty.StringVal("secret").Mark("porpoise"),
"other": cty.StringVal("that").Mark("a"),
},
cty.StringVal("secret").Mark("porpoise"),
``,
},
"two arguments, both marked, second succeeds": {
`try(sensitive, other)`,
map[string]cty.Value{
"other": cty.StringVal("that").Mark("a"),
},
cty.StringVal("that").Mark("a"),
``,
},
"two arguments, result is element of marked list ": {
`try(sensitive[0], other)`,
map[string]cty.Value{
"sensitive": cty.ListVal([]cty.Value{
cty.StringVal("list"),
cty.StringVal("of "),
cty.StringVal("secrets"),
}).Mark("secret"),
"other": cty.StringVal("not"),
},
cty.StringVal("list").Mark("secret"),
``,
},
"nested known expression from unknown": {
// this expression contains an unknown, but will always return in
// "bar"
`try({u: false ? unknown : "bar"}, other)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.String),
"other": cty.MapVal(map[string]cty.Value{
"v": cty.StringVal("oops"),
}),
},
cty.ObjectVal(map[string]cty.Value{
"u": cty.StringVal("bar"),
}),
``,
},
"nested index op on unknown": {
// unknown and other have identical types, but we must return a
// dynamic value since v could change within the final result value
// after the first argument becomes known.
`try({u: unknown["foo"], v: "orig"}, other)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.Map(cty.String)),
"other": cty.MapVal(map[string]cty.Value{
"u": cty.StringVal("oops"),
"v": cty.StringVal("oops"),
}),
},
cty.DynamicVal,
``,
},
"three arguments, all fail": {
`try(this, that, this_thing_in_particular)`,
nil,
cty.NumberIntVal(2),
// The grammar of this stringification of the message is unfortunate,
// but caller can type-assert our result to get the original
// diagnostics directly in order to produce a better result.
`test.hcl:1,1-5: Error in function call; Call to function "try" failed: no expression succeeded:
- Variables not allowed (at test.hcl:1,5-9)
Variables may not be used here.
- Variables not allowed (at test.hcl:1,11-15)
Variables may not be used here.
- Variables not allowed (at test.hcl:1,17-41)
Variables may not be used here.
At least one expression must produce a successful result.`,
},
"no arguments": {
`try()`,
nil,
cty.NilVal,
`test.hcl:1,1-5: Error in function call; Call to function "try" failed: at least one argument is required.`,
},
}
for k, test := range tests {
t.Run(k, func(t *testing.T) {
expr, diags := hclsyntax.ParseExpression([]byte(test.expr), "test.hcl", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Fatalf("unexpected problems: %s", diags.Error())
}
ctx := &hcl.EvalContext{
Variables: test.vars,
Functions: map[string]function.Function{
"try": TryFunc,
},
}
got, err := expr.Value(ctx)
if err != nil {
if test.wantErr != "" {
if got, want := err.Error(), test.wantErr; got != want {
t.Errorf("wrong error\ngot: %s\nwant: %s", got, want)
}
} else {
t.Errorf("unexpected error\ngot: %s\nwant: <nil>", err)
}
return
}
if test.wantErr != "" {
t.Errorf("wrong error\ngot: <nil>\nwant: %s", test.wantErr)
}
if !test.want.RawEquals(got) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
}
})
}
}
func TestCanFunc(t *testing.T) {
tests := map[string]struct {
expr string
vars map[string]cty.Value
want cty.Value
}{
"succeeds": {
`can(1)`,
nil,
cty.True,
},
"fails": {
`can(nope)`,
nil,
cty.False,
},
"simple unknown": {
`can(unknown)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.Number),
},
cty.UnknownVal(cty.Bool),
},
"traversal through unknown": {
`can(unknown.foo)`,
map[string]cty.Value{
"unknown": cty.UnknownVal(cty.Map(cty.Number)),
},
cty.UnknownVal(cty.Bool),
},
"deep unknown": {
`can(has_unknown)`,
map[string]cty.Value{
"has_unknown": cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}),
},
cty.UnknownVal(cty.Bool),
},
}
for k, test := range tests {
t.Run(k, func(t *testing.T) {
expr, diags := hclsyntax.ParseExpression([]byte(test.expr), "test.hcl", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Fatalf("unexpected problems: %s", diags.Error())
}
ctx := &hcl.EvalContext{
Variables: test.vars,
Functions: map[string]function.Function{
"can": CanFunc,
},
}
got, err := expr.Value(ctx)
if err != nil {
t.Errorf("unexpected error\ngot: %s\nwant: <nil>", err)
}
if !test.want.RawEquals(got) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
}
})
}
}