opentofu/terraform/eval_diff_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

119 lines
3.0 KiB
Go

package terraform
import (
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)
func TestProcessIgnoreChangesIndividual(t *testing.T) {
tests := map[string]struct {
Old, New cty.Value
Ignore []string
Want cty.Value
}{
"string": {
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("a value"),
"b": cty.StringVal("b value"),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("new a value"),
"b": cty.StringVal("new b value"),
}),
[]string{"a"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("a value"),
"b": cty.StringVal("new b value"),
}),
},
"changed type": {
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("a value"),
"b": cty.StringVal("b value"),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.NumberIntVal(1),
"b": cty.StringVal("new b value"),
}),
[]string{"a"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("a value"),
"b": cty.StringVal("new b value"),
}),
},
"list": {
cty.ObjectVal(map[string]cty.Value{
"a": cty.ListVal([]cty.Value{
cty.StringVal("a0 value"),
cty.StringVal("a1 value"),
}),
"b": cty.StringVal("b value"),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.ListVal([]cty.Value{
cty.StringVal("new a0 value"),
cty.StringVal("new a1 value"),
}),
"b": cty.StringVal("new b value"),
}),
[]string{"a"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.ListVal([]cty.Value{
cty.StringVal("a0 value"),
cty.StringVal("a1 value"),
}),
"b": cty.StringVal("new b value"),
}),
},
"object attribute": {
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("a.foo value"),
"bar": cty.StringVal("a.bar value"),
}),
"b": cty.StringVal("b value"),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("new a.foo value"),
"bar": cty.StringVal("new a.bar value"),
}),
"b": cty.StringVal("new b value"),
}),
[]string{"a.bar"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("new a.foo value"),
"bar": cty.StringVal("a.bar value"),
}),
"b": cty.StringVal("new b value"),
}),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ignore := make([]hcl.Traversal, len(test.Ignore))
for i, ignoreStr := range test.Ignore {
trav, diags := hclsyntax.ParseTraversalAbs([]byte(ignoreStr), "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Fatalf("failed to parse %q: %s", ignoreStr, diags.Error())
}
ignore[i] = trav
}
ret, diags := processIgnoreChangesIndividual(test.Old, test.New, ignore)
if diags.HasErrors() {
t.Fatal(diags.Err())
}
if got, want := ret, test.Want; !want.RawEquals(got) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
}
}