summaryrefslogtreecommitdiff
path: root/hcldec
diff options
context:
space:
mode:
authorMartin Atkins <mart@degeneration.co.uk>2023-10-06 10:40:55 -0700
committerMartin Atkins <mart@degeneration.co.uk>2023-10-06 11:14:24 -0700
commit63067e819cb6e8044d34b0a32d198a4941f34e42 (patch)
treea91483c17e744f38b4614aea3dc17522349c2b5d /hcldec
parent6ec71247d9d00d0e22cc0d33636844380bc11463 (diff)
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.
Diffstat (limited to 'hcldec')
-rw-r--r--hcldec/spec_test.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/hcldec/spec_test.go b/hcldec/spec_test.go
index 25c5d6c..1b0594d 100644
--- a/hcldec/spec_test.go
+++ b/hcldec/spec_test.go
@@ -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"
@@ -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)
@@ -256,9 +258,10 @@ 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{
@@ -266,6 +269,26 @@ dyn = dyn
"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())
@@ -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)