opentofu/lang/blocktoattr/variables_test.go
Martin Atkins 39e609d5fd vendor: switch to HCL 2.0 in the HCL repository
Previously we were using the experimental HCL 2 repository, but now we'll
shift over to the v2 import path within the main HCL repository as part of
actually releasing HCL 2.0 as stable.

This is a mechanical search/replace to the new import paths. It also
switches to the v2.0.0 release of HCL, which includes some new code that
Terraform didn't previously have but should not change any behavior that
matters for Terraform's purposes.

For the moment the experimental HCL2 repository is still an indirect
dependency via terraform-config-inspect, so it remains in our go.sum and
vendor directories for the moment. Because terraform-config-inspect uses
a much smaller subset of the HCL2 functionality, this does still manage
to prune the vendor directory a little. A subsequent release of
terraform-config-inspect should allow us to completely remove that old
repository in a future commit.
2019-10-02 15:10:21 -07:00

201 lines
3.9 KiB
Go

package blocktoattr
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
hcljson "github.com/hashicorp/hcl/v2/json"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/zclconf/go-cty/cty"
)
func TestExpandedVariables(t *testing.T) {
fooSchema := &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.List(cty.Object(map[string]cty.Type{
"bar": cty.String,
})),
Optional: true,
},
"bar": {
Type: cty.Map(cty.String),
Optional: true,
},
},
}
tests := map[string]struct {
src string
json bool
schema *configschema.Block
want []hcl.Traversal
}{
"empty": {
src: ``,
schema: &configschema.Block{},
want: nil,
},
"attribute syntax": {
src: `
foo = [
{
bar = baz
},
]
`,
schema: fooSchema,
want: []hcl.Traversal{
{
hcl.TraverseRoot{
Name: "baz",
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 4, Column: 11, Byte: 23},
End: hcl.Pos{Line: 4, Column: 14, Byte: 26},
},
},
},
},
},
"block syntax": {
src: `
foo {
bar = baz
}
`,
schema: fooSchema,
want: []hcl.Traversal{
{
hcl.TraverseRoot{
Name: "baz",
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 3, Column: 9, Byte: 15},
End: hcl.Pos{Line: 3, Column: 12, Byte: 18},
},
},
},
},
},
"block syntax with nested blocks": {
src: `
foo {
bar {
boop = baz
}
}
`,
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.List(cty.Object(map[string]cty.Type{
"bar": cty.List(cty.Object(map[string]cty.Type{
"boop": cty.String,
})),
})),
Optional: true,
},
},
},
want: []hcl.Traversal{
{
hcl.TraverseRoot{
Name: "baz",
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 4, Column: 12, Byte: 26},
End: hcl.Pos{Line: 4, Column: 15, Byte: 29},
},
},
},
},
},
"dynamic block syntax": {
src: `
dynamic "foo" {
for_each = beep
content {
bar = baz
}
}
`,
schema: fooSchema,
want: []hcl.Traversal{
{
hcl.TraverseRoot{
Name: "beep",
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 3, Column: 14, Byte: 30},
End: hcl.Pos{Line: 3, Column: 18, Byte: 34},
},
},
},
{
hcl.TraverseRoot{
Name: "baz",
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 5, Column: 11, Byte: 57},
End: hcl.Pos{Line: 5, Column: 14, Byte: 60},
},
},
},
},
},
"misplaced dynamic block": {
src: `
dynamic "bar" {
for_each = beep
content {
key = val
}
}
`,
schema: fooSchema,
want: []hcl.Traversal{
{
hcl.TraverseRoot{
Name: "beep",
SrcRange: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 3, Column: 14, Byte: 30},
End: hcl.Pos{Line: 3, Column: 18, Byte: 34},
},
},
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var f *hcl.File
var diags hcl.Diagnostics
if test.json {
f, diags = hcljson.Parse([]byte(test.src), "test.tf.json")
} else {
f, diags = hclsyntax.ParseConfig([]byte(test.src), "test.tf", hcl.Pos{Line: 1, Column: 1})
}
if diags.HasErrors() {
for _, diag := range diags {
t.Errorf("unexpected diagnostic: %s", diag)
}
t.FailNow()
}
got := ExpandedVariables(f.Body, test.schema)
co := cmpopts.IgnoreUnexported(hcl.TraverseRoot{})
if !cmp.Equal(got, test.want, co) {
t.Errorf("wrong result\n%s", cmp.Diff(test.want, got, co))
}
})
}
}