Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hclsyntax, hcldec: Just some extra tests for marks and refinements interacting together #631

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,26 @@ upper(
}).Mark("sensitive"),
0,
},
{ // splat with sensitive collection that's unknown
`maps.*.enabled`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"maps": cty.UnknownVal(cty.List(cty.Map(cty.Bool))).Mark("sensitive"),
},
},
cty.UnknownVal(cty.List(cty.Bool)).RefineNotNull().Mark("sensitive"),
0,
},
{ // splat with sensitive collection that's unknown and not null
`maps.*.enabled`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"maps": cty.UnknownVal(cty.List(cty.Map(cty.Bool))).RefineNotNull().Mark("sensitive"),
},
},
cty.UnknownVal(cty.List(cty.Bool)).RefineNotNull().Mark("sensitive"),
0,
},
{ // splat with collection with sensitive elements
`maps.*.x`,
&hcl.EvalContext{
Expand Down