Skip to content

Commit

Permalink
hcldec: New test for marks+refinements together
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed Oct 6, 2023
1 parent 6ec7124 commit 63067e8
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions hcldec/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand Down Expand Up @@ -218,6 +219,7 @@ func TestRefineValueSpec(t *testing.T) {
foo = "hello"
bar = unk
dyn = dyn
marked = mark(unk)
`

f, diags := hclsyntax.ParseConfig([]byte(config), "", hcl.InitialPos)
Expand Down Expand Up @@ -256,16 +258,37 @@ dyn = dyn
}
}
spec := &ObjectSpec{
"foo": attrSpec("foo"),
"bar": attrSpec("bar"),
"dyn": attrSpec("dyn"),
"foo": attrSpec("foo"),
"bar": attrSpec("bar"),
"dyn": attrSpec("dyn"),
"marked": attrSpec("marked"),
}

got, diags := Decode(f.Body, spec, &hcl.EvalContext{
Variables: map[string]cty.Value{
"unk": cty.UnknownVal(cty.String),
"dyn": cty.DynamicVal,
},
Functions: map[string]function.Function{
"mark": function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "v",
Type: cty.DynamicPseudoType,
AllowMarked: true,
AllowNull: true,
AllowUnknown: true,
AllowDynamicType: true,
},
},
Type: func(args []cty.Value) (cty.Type, error) {
return args[0].Type(), nil
},
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return args[0].Mark("boop"), nil
},
}),
},
})
if diags.HasErrors() {
t.Fatal(diags.Error())
Expand All @@ -284,6 +307,10 @@ dyn = dyn
// Correct behavior here requires that we convert the DynamicVal
// to an unknown string first and then refine it.
"dyn": cty.UnknownVal(cty.String).RefineNotNull(),

// This argument had a mark applied, which should be preserved
// despite the refinement.
"marked": cty.UnknownVal(cty.String).RefineNotNull().Mark("boop"),
})
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
t.Errorf("wrong result\n%s", diff)
Expand Down

0 comments on commit 63067e8

Please sign in to comment.