From 96d2265ddbed9a56a97643ea6f2994828f34ed9c Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Thu, 18 Jun 2020 10:10:44 -0400 Subject: [PATCH] command: Add test for invalid selected workspace If somehow an invalid workspace has been selected, the Meta.Workspace method should not return an error, to ensure that we don't break any existing workflows with invalid workspace names. --- command/meta_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/command/meta_test.go b/command/meta_test.go index 7f92da02c..84f2a4840 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -11,6 +11,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform/backend" + "github.com/hashicorp/terraform/backend/local" "github.com/hashicorp/terraform/terraform" ) @@ -238,6 +239,39 @@ func TestMeta_Workspace_override(t *testing.T) { } } +func TestMeta_Workspace_invalidSelected(t *testing.T) { + td := tempDir(t) + os.MkdirAll(td, 0755) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // this is an invalid workspace name + workspace := "test workspace" + + // create the workspace directories + if err := os.MkdirAll(filepath.Join(local.DefaultWorkspaceDir, workspace), 0755); err != nil { + t.Fatal(err) + } + + // create the workspace file to select it + if err := os.MkdirAll(DefaultDataDir, 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(DefaultDataDir, local.DefaultWorkspaceFile), []byte(workspace), 0644); err != nil { + t.Fatal(err) + } + + m := new(Meta) + + ws, err := m.Workspace() + if ws != workspace { + t.Errorf("Unexpected workspace\n got: %s\nwant: %s\n", ws, workspace) + } + if err != nil { + t.Errorf("Unexpected error: %s", err) + } +} + func TestMeta_process(t *testing.T) { test = false defer func() { test = true }()