Skip to content

Commit 63067e8

Browse files
hcldec: New test for marks+refinements together
The interactions between value marks and unknown value refinements can be a little tricky, so this new addition to the "RefineWith" tests confirms that it does indeed handle marked values correctly when passing through the refinement spec.
1 parent 6ec7124 commit 63067e8

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

hcldec/spec_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/google/go-cmp/cmp"
1313
"github.com/zclconf/go-cty-debug/ctydebug"
1414
"github.com/zclconf/go-cty/cty"
15+
"github.com/zclconf/go-cty/cty/function"
1516

1617
"github.com/hashicorp/hcl/v2"
1718
"github.com/hashicorp/hcl/v2/hclsyntax"
@@ -218,6 +219,7 @@ func TestRefineValueSpec(t *testing.T) {
218219
foo = "hello"
219220
bar = unk
220221
dyn = dyn
222+
marked = mark(unk)
221223
`
222224

223225
f, diags := hclsyntax.ParseConfig([]byte(config), "", hcl.InitialPos)
@@ -256,16 +258,37 @@ dyn = dyn
256258
}
257259
}
258260
spec := &ObjectSpec{
259-
"foo": attrSpec("foo"),
260-
"bar": attrSpec("bar"),
261-
"dyn": attrSpec("dyn"),
261+
"foo": attrSpec("foo"),
262+
"bar": attrSpec("bar"),
263+
"dyn": attrSpec("dyn"),
264+
"marked": attrSpec("marked"),
262265
}
263266

264267
got, diags := Decode(f.Body, spec, &hcl.EvalContext{
265268
Variables: map[string]cty.Value{
266269
"unk": cty.UnknownVal(cty.String),
267270
"dyn": cty.DynamicVal,
268271
},
272+
Functions: map[string]function.Function{
273+
"mark": function.New(&function.Spec{
274+
Params: []function.Parameter{
275+
{
276+
Name: "v",
277+
Type: cty.DynamicPseudoType,
278+
AllowMarked: true,
279+
AllowNull: true,
280+
AllowUnknown: true,
281+
AllowDynamicType: true,
282+
},
283+
},
284+
Type: func(args []cty.Value) (cty.Type, error) {
285+
return args[0].Type(), nil
286+
},
287+
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
288+
return args[0].Mark("boop"), nil
289+
},
290+
}),
291+
},
269292
})
270293
if diags.HasErrors() {
271294
t.Fatal(diags.Error())
@@ -284,6 +307,10 @@ dyn = dyn
284307
// Correct behavior here requires that we convert the DynamicVal
285308
// to an unknown string first and then refine it.
286309
"dyn": cty.UnknownVal(cty.String).RefineNotNull(),
310+
311+
// This argument had a mark applied, which should be preserved
312+
// despite the refinement.
313+
"marked": cty.UnknownVal(cty.String).RefineNotNull().Mark("boop"),
287314
})
288315
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
289316
t.Errorf("wrong result\n%s", diff)

0 commit comments

Comments
 (0)