From 8252920e9a3c197c378c548b2c2c8701b7153d42 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Thu, 18 Jun 2020 10:03:30 -0400 Subject: [PATCH] command: Allow workspace delete with invalid name We are validating the workspace name for all workspace commands. Due to a bug with the TF_WORKSPACE environment variable, it has been possible to accidentally create a workspace with an invalid name. This commit removes the valid workspace name check for workspace delete to allow users to clean up any invalid workspaces. --- command/workspace_command_test.go | 33 +++++++++++++++++++++++++++++++ command/workspace_delete.go | 8 +------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/command/workspace_command_test.go b/command/workspace_command_test.go index 8a0544737..d97f4722c 100644 --- a/command/workspace_command_test.go +++ b/command/workspace_command_test.go @@ -335,6 +335,39 @@ func TestWorkspace_delete(t *testing.T) { t.Fatalf("wrong workspace: %q", current) } } + +func TestWorkspace_deleteInvalid(t *testing.T) { + td := tempDir(t) + os.MkdirAll(td, 0755) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // choose an invalid workspace name + workspace := "test workspace" + path := filepath.Join(local.DefaultWorkspaceDir, workspace) + + // create the workspace directories + if err := os.MkdirAll(path, 0755); err != nil { + t.Fatal(err) + } + + ui := new(cli.MockUi) + delCmd := &WorkspaceDeleteCommand{ + Meta: Meta{Ui: ui}, + } + + // delete the workspace + if code := delCmd.Run([]string{workspace}); code != 0 { + t.Fatalf("error deleting workspace: %s", ui.ErrorWriter) + } + + if _, err := os.Stat(path); err == nil { + t.Fatalf("should have deleted workspace, but %s still exists", path) + } else if !os.IsNotExist(err) { + t.Fatalf("unexpected error for workspace path: %s", err) + } +} + func TestWorkspace_deleteWithState(t *testing.T) { td := tempDir(t) os.MkdirAll(td, 0755) diff --git a/command/workspace_delete.go b/command/workspace_delete.go index d22de8e69..ebb7c5eed 100644 --- a/command/workspace_delete.go +++ b/command/workspace_delete.go @@ -40,13 +40,6 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int { return cli.RunResultHelp } - workspace := args[0] - - if !validWorkspaceName(workspace) { - c.Ui.Error(fmt.Sprintf(envInvalidName, workspace)) - return 1 - } - configPath, err := ModulePath(args[1:]) if err != nil { c.Ui.Error(err.Error()) @@ -78,6 +71,7 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int { return 1 } + workspace := args[0] exists := false for _, ws := range workspaces { if workspace == ws {