Skip to content

Commit

Permalink
ensure deterministic resp.RequiresReplace order
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Aug 20, 2021
1 parent 3bacf1b commit 583f552
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tfsdk/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"sync"

"github.com/hashicorp/terraform-plugin-framework/internal/diagnostics"
Expand Down Expand Up @@ -736,6 +737,11 @@ func (s *server) PlanResourceChange(ctx context.Context, req *tfprotov6.PlanReso
resp.PlannedState = &plannedState
resp.RequiresReplace = append(resp.RequiresReplace, modifyPlanResp.RequiresReplace...)

// sort the RequiresReplace slice so response is deterministic
sort.Slice(resp.RequiresReplace, func(i, j int) bool {
return resp.RequiresReplace[i].String() < resp.RequiresReplace[j].String()
})

return resp, nil
}

Expand Down
6 changes: 2 additions & 4 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ func TestServerPlanResourceChange(t *testing.T) {
"interface": tftypes.NewValue(tftypes.String, "scsi"),
}),
}),
expectedRequiresReplace: []*tftypes.AttributePath{tftypes.NewAttributePath().WithAttributeName("size"), tftypes.NewAttributePath().WithAttributeName("scratch_disk").WithAttributeName("interface")},
expectedRequiresReplace: []*tftypes.AttributePath{tftypes.NewAttributePath().WithAttributeName("scratch_disk").WithAttributeName("interface"), tftypes.NewAttributePath().WithAttributeName("size")},
},
"three_attrplanmodifies_requiresreplaceif_false": {
priorState: tftypes.NewValue(testServeResourceTypeThreeType, map[string]tftypes.Value{
Expand Down Expand Up @@ -2272,9 +2272,7 @@ func TestServerPlanResourceChange(t *testing.T) {
t.Errorf("Expected planned private to be %q, got %q", tc.expectedPlannedPrivate, got.PlannedPrivate)
return
}
if diff := cmp.Diff(got.RequiresReplace, tc.expectedRequiresReplace, cmpopts.EquateEmpty(), cmpopts.SortSlices(func(x, y *tftypes.AttributePath) bool {
return x.String() < y.String()
})); diff != "" {
if diff := cmp.Diff(got.RequiresReplace, tc.expectedRequiresReplace, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("Unexpected diff in requires replace (+wanted, -got): %s", diff)
return
}
Expand Down

0 comments on commit 583f552

Please sign in to comment.