Skip to content

Commit 23563d0

Browse files
authored
Merge pull request #1505 from amangale/patch-4
Use HasSuffix to check for suffix
2 parents d8f5a38 + 7ff77a2 commit 23563d0

File tree

2 files changed

+1
-39
lines changed

2 files changed

+1
-39
lines changed

modules/terraform/workspace.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package terraform
22

33
import (
4-
"fmt"
5-
"regexp"
64
"strings"
75

86
"github.com/gruntwork-io/terratest/modules/testing"
@@ -44,24 +42,13 @@ func WorkspaceSelectOrNewE(t testing.TestingT, options *Options, name string) (s
4442
func isExistingWorkspace(out string, name string) bool {
4543
workspaces := strings.Split(out, "\n")
4644
for _, ws := range workspaces {
47-
if nameMatchesWorkspace(name, ws) {
45+
if strings.HasSuffix(ws, name) {
4846
return true
4947
}
5048
}
5149
return false
5250
}
5351

54-
func nameMatchesWorkspace(name string, workspace string) bool {
55-
// Regex for matching workspace should match for strings with optional leading asterisk "*"
56-
// following optional white spaces following the workspace name.
57-
// E.g. for the given name "terratest", following strings will match:
58-
//
59-
// "* terratest"
60-
// " terratest"
61-
match, _ := regexp.MatchString(fmt.Sprintf("^\\*?\\s*%s$", name), workspace)
62-
return match
63-
}
64-
6552
// WorkspaceDelete removes the specified terraform workspace with the given options.
6653
// It returns the name of the current workspace AFTER deletion, and the returned error (that can be nil).
6754
// If the workspace to delete is the current one, then it tries to switch to the "default" workspace.

modules/terraform/workspace_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,6 @@ func TestIsExistingWorkspace(t *testing.T) {
108108
}
109109
}
110110

111-
func TestNameMatchesWorkspace(t *testing.T) {
112-
t.Parallel()
113-
114-
testCases := []struct {
115-
name string
116-
workspace string
117-
expected bool
118-
}{
119-
{"default", " default", true},
120-
{"default", "* default", true},
121-
{"default", "", false},
122-
{"foo", " foobar", false},
123-
{"foo", "* foobar", false},
124-
{"foobar", " foo", false},
125-
{"foobar", "* foo", false},
126-
{"foo", " foo", true},
127-
{"foo", "* foo", true},
128-
}
129-
130-
for _, testCase := range testCases {
131-
actual := nameMatchesWorkspace(testCase.name, testCase.workspace)
132-
assert.Equal(t, testCase.expected, actual, "Name: %q, Workspace: %q", testCase.name, testCase.workspace)
133-
}
134-
}
135-
136111
func TestWorkspaceDeleteE(t *testing.T) {
137112
t.Parallel()
138113

0 commit comments

Comments
 (0)