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.
This commit is contained in:
Alisdair McDiarmid 2020-06-18 10:03:30 -04:00
parent b239570abb
commit 8252920e9a
2 changed files with 34 additions and 7 deletions

View File

@ -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)

View File

@ -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 {