ignore unexported state field in comparison

and unify the options used to compare changes in tests
pull/35558/head
James Bardin 2 years ago
parent 912738ea37
commit 0b8fbfe323

@ -1627,7 +1627,7 @@ func TestApply_backup(t *testing.T) {
actual := backupState.RootModule().Resources["test_instance.foo"]
expected := originalState.RootModule().Resources["test_instance.foo"]
if !cmp.Equal(actual, expected, cmpopts.EquateEmpty()) {
if !cmp.Equal(actual, expected, cmpopts.EquateEmpty(), cmpopts.IgnoreUnexported(states.ResourceInstanceObjectSrc{})) {
t.Fatalf(
"wrong aws_instance.foo state\n%s",
cmp.Diff(expected, actual, cmp.Transformer("bytesAsString", func(b []byte) string {

@ -14,6 +14,7 @@ import (
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
@ -38,6 +39,13 @@ import (
"github.com/hashicorp/terraform/version"
)
var changesCmpOpts = cmp.Options{
ctydebug.CmpOptions,
cmpCollectionsSet,
cmpopts.IgnoreUnexported(addrs.InputVariable{}),
cmpopts.IgnoreUnexported(states.ResourceInstanceObjectSrc{}),
}
func TestApplyWithRemovedResource(t *testing.T) {
fakePlanTimestamp, err := time.Parse(time.RFC3339, "1994-09-05T08:50:00Z")
if err != nil {
@ -219,7 +227,7 @@ func TestApplyWithRemovedResource(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -423,7 +431,7 @@ func TestApplyWithMovedResource(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -594,7 +602,7 @@ func TestApplyWithSensitivePropagation(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -762,7 +770,7 @@ func TestApplyWithCheckableObjects(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
@ -888,7 +896,7 @@ func TestApplyWithCheckableObjects(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1025,7 +1033,7 @@ func TestApplyWithForcePlanTimestamp(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1252,7 +1260,7 @@ func TestApplyWithFailedComponent(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
@ -1362,7 +1370,7 @@ func TestApplyWithFailedProviderLinkedComponent(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
@ -1841,7 +1849,7 @@ func TestApplyWithStateManipulation(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(tc.changes, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(tc.changes, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
@ -1990,7 +1998,7 @@ func TestApplyWithChangedInputValues(t *testing.T) {
return appliedChangeSortKey(applyChanges[i]) < appliedChangeSortKey(applyChanges[j])
})
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2146,7 +2154,7 @@ func TestApplyAutomaticInputConversion(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2279,7 +2287,7 @@ func TestApplyEphemeralInput(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2422,7 +2430,7 @@ func TestApplyMissingEphemeralInput(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2548,7 +2556,7 @@ func TestApplyEphemeralInputWithDefault(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, applyChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, applyChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}

@ -14,7 +14,6 @@ import (
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
@ -697,7 +696,7 @@ func TestPlanWithComplexVariableDefaults(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, changes, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, changes, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
@ -859,11 +858,7 @@ func TestPlanWithSingleResource(t *testing.T) {
},
}
cmpOptions := cmp.Options{
ctydebug.CmpOptions,
collections.CmpOptions,
}
if diff := cmp.Diff(wantChanges, gotChanges, cmpOptions); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -927,7 +922,7 @@ func TestPlanWithEphemeralInputVariables(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
})
@ -985,7 +980,7 @@ func TestPlanWithEphemeralInputVariables(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
})
@ -1044,7 +1039,7 @@ func TestPlanVariableOutputRoundtripNested(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1117,7 +1112,7 @@ func TestPlanSensitiveOutput(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1190,7 +1185,7 @@ func TestPlanSensitiveOutputNested(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1292,7 +1287,7 @@ func TestPlanSensitiveOutputAsInput(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1652,7 +1647,7 @@ func TestPlanWithSensitivePropagation(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -1813,7 +1808,7 @@ func TestPlanWithSensitivePropagationNested(t *testing.T) {
return plannedChangeSortKey(gotChanges[i]) < plannedChangeSortKey(gotChanges[j])
})
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2096,14 +2091,7 @@ func TestPlanWithCheckableObjects(t *testing.T) {
},
}
cmpOptions := cmp.Options{
ctydebug.CmpOptions,
collections.CmpOptions,
cmp.Options{
cmpopts.IgnoreUnexported(addrs.InputVariable{}),
},
}
if diff := cmp.Diff(wantChanges, gotChanges, cmpOptions); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2253,7 +2241,7 @@ func TestPlanWithDeferredResource(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2490,7 +2478,7 @@ func TestPlanWithDeferredComponentForEach(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2744,7 +2732,7 @@ func TestPlanWithDeferredComponentReferences(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -2890,7 +2878,7 @@ func TestPlanWithDeferredEmbeddedStackForEach(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -3038,7 +3026,7 @@ func TestPlanWithDeferredEmbeddedStackAndComponentForEach(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -3313,7 +3301,7 @@ func TestPlanWithDeferredProviderForEach(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -3385,7 +3373,7 @@ func TestPlanInvalidProvidersFailGracefully(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, changes, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, changes, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -3800,7 +3788,7 @@ func TestPlanWithStateManipulation(t *testing.T) {
return plannedChangeSortKey(changes[i]) < plannedChangeSortKey(changes[j])
})
if diff := cmp.Diff(tc.changes, changes, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(tc.changes, changes, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
@ -3946,11 +3934,7 @@ func TestPlan_plantimestamp_force_timestamp(t *testing.T) {
&stackplan.PlannedChangePlannedTimestamp{PlannedTimestamp: fakePlanTimestamp},
}
cmpOptions := cmp.Options{
ctydebug.CmpOptions,
collections.CmpOptions,
}
if diff := cmp.Diff(wantChanges, gotChanges, cmpOptions); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}
@ -4259,7 +4243,7 @@ func TestPlan_DependsOnUpdatesRequirements(t *testing.T) {
},
}
if diff := cmp.Diff(wantChanges, gotChanges, ctydebug.CmpOptions, cmpCollectionsSet); diff != "" {
if diff := cmp.Diff(wantChanges, gotChanges, changesCmpOpts); diff != "" {
t.Errorf("wrong changes\n%s", diff)
}
}

@ -7,7 +7,6 @@ import (
"encoding/json"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty/cty"
ctymsgpack "github.com/zclconf/go-cty/cty/msgpack"
@ -245,7 +244,7 @@ func TestAppliedChangeAsProto(t *testing.T) {
if err != nil {
t.Fatal(err)
}
spew.Dump(got)
if diff := cmp.Diff(test.Want, got, protocmp.Transform()); diff != "" {
t.Errorf("wrong result\n%s", diff)
}

@ -7,6 +7,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/stacks/stackaddrs"
"github.com/hashicorp/terraform/internal/stacks/stackstate/statekeys"
@ -87,7 +88,7 @@ func TestLoader_basic(t *testing.T) {
Status: states.ObjectReady,
}
if diff := cmp.Diff(got, want); diff != "" {
if diff := cmp.Diff(got, want, cmpopts.IgnoreUnexported(states.ResourceInstanceObjectSrc{})); diff != "" {
t.Errorf("unexpected resource instance object\ndiff: %s", diff)
}
}

Loading…
Cancel
Save