diff --git a/internal/command/workdir/backend_state.go b/internal/command/workdir/backend_state.go index 2829291f45..6494353c52 100644 --- a/internal/command/workdir/backend_state.go +++ b/internal/command/workdir/backend_state.go @@ -143,9 +143,10 @@ func (f *BackendStateFile) DeepCopy() *BackendStateFile { return nil } ret := &BackendStateFile{ - Version: f.Version, - TFVersion: f.TFVersion, - Backend: f.Backend.DeepCopy(), + Version: f.Version, + TFVersion: f.TFVersion, + Backend: f.Backend.DeepCopy(), + StateStore: f.StateStore.DeepCopy(), } if f.Remote != nil { // This shouldn't ever be present in an object held by a caller since diff --git a/internal/command/workdir/backend_state_test.go b/internal/command/workdir/backend_state_test.go index adeeef9c05..f1953b8116 100644 --- a/internal/command/workdir/backend_state_test.go +++ b/internal/command/workdir/backend_state_test.go @@ -5,6 +5,7 @@ package workdir import ( "encoding/json" + "reflect" "strings" "testing" @@ -230,3 +231,46 @@ func TestEncodeBackendStateFile(t *testing.T) { } } + +func TestBackendStateFile_DeepCopy(t *testing.T) { + + tests := map[string]struct { + file *BackendStateFile + }{ + "Deep copy preserves state_store data": { + file: &BackendStateFile{ + StateStore: &StateStoreConfigState{ + Type: "foo_bar", + Provider: getTestProviderState(t, "1.2.3", "A", "B", "C"), + ConfigRaw: json.RawMessage([]byte(`{"foo":"bar"}`)), + Hash: 123, + }, + }, + }, + "Deep copy preserves backend data": { + file: &BackendStateFile{ + Backend: &BackendConfigState{ + Type: "foobar", + ConfigRaw: json.RawMessage([]byte(`{"foo":"bar"}`)), + Hash: 123, + }, + }, + }, + "Deep copy preserves version and Terraform version data": { + file: &BackendStateFile{ + Version: 3, + TFVersion: "9.9.9", + }, + }, + } + + for tn, tc := range tests { + t.Run(tn, func(t *testing.T) { + copy := tc.file.DeepCopy() + + if !reflect.DeepEqual(copy, tc.file) { + t.Fatalf("unexpected difference in backend state data:\n got %#v, want %#v", copy, tc.file) + } + }) + } +}