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 {