Skip to content

Commit 3617411

Browse files
hcldec: A test case for attributes set to cty.DynamicVal with refinements
This test case is here to anticipate a _possible_ bug that isn't actually buggy in the current implementation: if an attribute spec is given a non-dynamic type constraint and then refined based on that type constraint then the hcldec implementation must perform the type conversion first and only then attempt to add the refinements. Another possible variation here would be for the attribute spec to have a dynamic type constraint (cty.DynamicPseudoType) and then try to refine its result. That case isn't tested here because that's always an implementation error in the calling application: RefineValueSpec must be used only in ways that are valid for the full range of types that the nested spec could produce, and there are no refinements that are valid for the full range of cty.DynamicPseudoType. That situation can and will panic at runtime, alerting the application developer that they've used hcldec incorrectly. There is no way for end-user input to cause this panic if the calling application is written correctly. This doesn't actually change the system behavior. It's a regression test to catch possible regressions under future maintenance.
1 parent ed6d4bf commit 3617411

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

hcldec/spec_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ func TestRefineValueSpec(t *testing.T) {
217217
config := `
218218
foo = "hello"
219219
bar = unk
220+
dyn = dyn
220221
`
221222

222223
f, diags := hclsyntax.ParseConfig([]byte(config), "", hcl.InitialPos)
@@ -257,11 +258,13 @@ bar = unk
257258
spec := &ObjectSpec{
258259
"foo": attrSpec("foo"),
259260
"bar": attrSpec("bar"),
261+
"dyn": attrSpec("dyn"),
260262
}
261263

262264
got, diags := Decode(f.Body, spec, &hcl.EvalContext{
263265
Variables: map[string]cty.Value{
264266
"unk": cty.UnknownVal(cty.String),
267+
"dyn": cty.DynamicVal,
265268
},
266269
})
267270
if diags.HasErrors() {
@@ -276,6 +279,11 @@ bar = unk
276279

277280
// The final value of bar is unknown but refined as non-null.
278281
"bar": cty.UnknownVal(cty.String).RefineNotNull(),
282+
283+
// The final value of dyn is unknown but refined as non-null.
284+
// Correct behavior here requires that we convert the DynamicVal
285+
// to an unknown string first and then refine it.
286+
"dyn": cty.UnknownVal(cty.String).RefineNotNull(),
279287
})
280288
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
281289
t.Errorf("wrong result\n%s", diff)

0 commit comments

Comments
 (0)