Skip to content

Commit ded4f1a

Browse files
authored
Merge pull request #29805 from hashicorp/alisdair/fix-init-workspace-select-input-false
command/init: Fail if -input=false but required
2 parents 0ef4fe0 + ecb98e1 commit ded4f1a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

internal/command/meta_backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ func (m *Meta) selectWorkspace(b backend.Backend) error {
241241
return m.SetWorkspace(workspaces[0])
242242
}
243243

244+
if !m.input {
245+
return fmt.Errorf("Currently selected workspace %q does not exist", workspace)
246+
}
247+
244248
// Otherwise, ask the user to select a workspace from the list of existing workspaces.
245249
v, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{
246250
Id: "select-workspace",

internal/command/meta_backend_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"reflect"
88
"sort"
9+
"strings"
910
"testing"
1011

1112
"github.com/hashicorp/terraform/internal/addrs"
@@ -867,6 +868,28 @@ func TestMetaBackend_initSelectedWorkspaceDoesNotExistAutoSelect(t *testing.T) {
867868
}
868869
}
869870

871+
// Initializing a backend which supports workspaces and does *not* have
872+
// the currently selected workspace with input=false should fail.
873+
func TestMetaBackend_initSelectedWorkspaceDoesNotExistInputFalse(t *testing.T) {
874+
// Create a temporary working directory that is empty
875+
td := tempDir(t)
876+
testCopyDir(t, testFixturePath("init-backend-selected-workspace-doesnt-exist-multi"), td)
877+
defer os.RemoveAll(td)
878+
defer testChdir(t, td)()
879+
880+
// Setup the meta
881+
m := testMetaBackend(t, nil)
882+
m.input = false
883+
884+
// Get the backend
885+
_, diags := m.Backend(&BackendOpts{Init: true})
886+
887+
// Should fail immediately
888+
if got, want := diags.ErrWithWarnings().Error(), `Currently selected workspace "bar" does not exist`; !strings.Contains(got, want) {
889+
t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want)
890+
}
891+
}
892+
870893
// Changing a configured backend, copying state
871894
func TestMetaBackend_configuredChangeCopy(t *testing.T) {
872895
// Create a temporary working directory that is empty

0 commit comments

Comments
 (0)