terraform/internal/command/arguments/workspace_new_test.go
Sarah French 6554bda227
Some checks failed
build / Determine intended Terraform version (push) Has been cancelled
build / Determine Go toolchain version (push) Has been cancelled
Quick Checks / Unit Tests (push) Has been cancelled
Quick Checks / Race Tests (push) Has been cancelled
Quick Checks / End-to-end Tests (push) Has been cancelled
Quick Checks / Code Consistency Checks (push) Has been cancelled
build / Generate release metadata (push) Has been cancelled
build / Build for freebsd_386 (push) Has been cancelled
build / Build for linux_386 (push) Has been cancelled
build / Build for openbsd_386 (push) Has been cancelled
build / Build for windows_386 (push) Has been cancelled
build / Build for darwin_amd64 (push) Has been cancelled
build / Build for freebsd_amd64 (push) Has been cancelled
build / Build for linux_amd64 (push) Has been cancelled
build / Build for openbsd_amd64 (push) Has been cancelled
build / Build for solaris_amd64 (push) Has been cancelled
build / Build for windows_amd64 (push) Has been cancelled
build / Build for freebsd_arm (push) Has been cancelled
build / Build for linux_arm (push) Has been cancelled
build / Build for darwin_arm64 (push) Has been cancelled
build / Build for linux_arm64 (push) Has been cancelled
build / Build for windows_arm64 (push) Has been cancelled
build / Build Docker image for linux_386 (push) Has been cancelled
build / Build Docker image for linux_amd64 (push) Has been cancelled
build / Build Docker image for linux_arm (push) Has been cancelled
build / Build Docker image for linux_arm64 (push) Has been cancelled
build / Build e2etest for linux_386 (push) Has been cancelled
build / Build e2etest for windows_386 (push) Has been cancelled
build / Build e2etest for darwin_amd64 (push) Has been cancelled
build / Build e2etest for linux_amd64 (push) Has been cancelled
build / Build e2etest for windows_amd64 (push) Has been cancelled
build / Build e2etest for linux_arm (push) Has been cancelled
build / Build e2etest for darwin_arm64 (push) Has been cancelled
build / Build e2etest for linux_arm64 (push) Has been cancelled
build / Run e2e test for linux_386 (push) Has been cancelled
build / Run e2e test for windows_386 (push) Has been cancelled
build / Run e2e test for darwin_amd64 (push) Has been cancelled
build / Run e2e test for linux_amd64 (push) Has been cancelled
build / Run e2e test for windows_amd64 (push) Has been cancelled
build / Run e2e test for linux_arm (push) Has been cancelled
build / Run e2e test for linux_arm64 (push) Has been cancelled
build / Run terraform-exec test for linux amd64 (push) Has been cancelled
refactor: Update workspace select and new subcommands to use the arguments package for parsing arguments and flags (#38430)
* refactor: Move method `ValidWorkspaceName` and related const into arguments package, update references

This function is primarily used to validate arguments, but it's used once (in (m *Meta) Workspace) in a different context, so I've left the original function signature in the command package.

* feat: Add `workspace new`-related code to arguments package

* refactor: Update `workspace new` to use the arguments package when parsing arguments

Note that this changes the format of errors returned when arg parsing fails - this is now wrapped in a diagnostic so now starts with `\nError: ` and has an extra trailing newline.

* feat: Add `workspace select`-related code to arguments package

* refactor: Update `workspace select` to use the arguments package when parsing arguments

Note that this changes the format of errors returned when arg parsing fails - this is now wrapped in a diagnostic so now starts with `\nError: ` and has an extra trailing newline.
2026-05-01 16:39:54 +01:00

149 lines
3.3 KiB
Go

// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
package arguments
import (
"testing"
"time"
"github.com/hashicorp/terraform/internal/tfdiags"
)
func TestParseWorkspaceNew_valid(t *testing.T) {
testCases := map[string]struct {
args []string
want *WorkspaceNew
}{
"name specified & default flags": {
[]string{"my-new-workspace"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "my-new-workspace",
Lock: true,
LockTimeout: 0,
StatePath: "",
},
},
"locking turned off via -lock": {
[]string{"-lock=false", "my-new-workspace"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "my-new-workspace",
Lock: false,
LockTimeout: 0,
StatePath: "",
},
},
"lock timeout specified via -lock-timeout": {
[]string{"-lock-timeout=30s", "my-new-workspace"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "my-new-workspace",
Lock: true,
LockTimeout: 30 * time.Second,
StatePath: "",
},
},
"state path specified via -state": {
[]string{"-state=path/to/state", "my-new-workspace"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "my-new-workspace",
Lock: true,
LockTimeout: 0,
StatePath: "path/to/state",
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseWorkspaceNew(tc.args)
if len(diags) > 0 {
t.Fatalf("unexpected diags: %v", diags)
}
if *got != *tc.want {
t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
}
})
}
}
func TestParseWorkspaceNew_invalid(t *testing.T) {
testCases := map[string]struct {
args []string
want *WorkspaceNew
wantDiags tfdiags.Diagnostics
}{
"unknown flag": {
[]string{"-boop", "my-new-workspace"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "my-new-workspace",
Lock: true,
},
tfdiags.Diagnostics{
tfdiags.Sourceless(
tfdiags.Error,
"Failed to parse command-line flags",
"flag provided but not defined: -boop",
),
},
},
"too many arguments": {
[]string{"my-new-workspace", "bar"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "", // Isn't set if there are extra arguments supplied
Lock: true,
},
tfdiags.Diagnostics{
tfdiags.Sourceless(
tfdiags.Error,
"Expected a single argument: NAME.",
"", // No detail
),
},
},
"invalid workspace name": {
[]string{"§@!invalid-name!@§"},
&WorkspaceNew{
Workspace: Workspace{
ViewType: ViewHuman,
},
Name: "§@!invalid-name!@§",
Lock: true,
},
tfdiags.Diagnostics{
tfdiags.Sourceless(
tfdiags.Error,
"\nThe workspace name \"§@!invalid-name!@§\" is not allowed. The name must contain only URL safe\ncharacters, contain no path separators, and not be an empty string.\n",
"", // No detail
),
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, gotDiags := ParseWorkspaceNew(tc.args)
if *got != *tc.want {
t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
}
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
})
}
}