Skip to content

Commit 17fab17

Browse files
authored
Merge pull request #4702 from kersten/chore/cli-test-init-vars
🌱 (chore): refactor unit tests to isolate mutable state with BeforeEach for pkg/cli
2 parents b9b5d5c + bc25c8d commit 17fab17

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

pkg/cli/options_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ import (
3838
var _ = Describe("Discover external plugins", func() {
3939
Context("with valid plugins root path", func() {
4040
var (
41-
homePath string = os.Getenv("HOME")
42-
customPath string = "/tmp/myplugins"
41+
homePath string
42+
customPath string
4343
// store user's original EXTERNAL_PLUGINS_PATH
4444
originalPluginPath string
4545
xdghome string
4646
// store user's original XDG_CONFIG_HOME
4747
originalXdghome string
4848
)
4949

50+
BeforeEach(func() {
51+
homePath = os.Getenv("HOME")
52+
customPath = "/tmp/myplugins"
53+
})
54+
5055
When("XDG_CONFIG_HOME is not set and using the $HOME environment variable", func() {
5156
// store and unset the XDG_CONFIG_HOME
5257
BeforeEach(func() {
@@ -504,14 +509,24 @@ var _ = Describe("CLI options", func() {
504509
c *CLI
505510
err error
506511

512+
projectVersion config.Version
513+
514+
p plugin.Plugin
515+
np1 plugin.Plugin
516+
np2 mockPlugin
517+
np3 plugin.Plugin
518+
np4 plugin.Plugin
519+
)
520+
521+
BeforeEach(func() {
507522
projectVersion = config.Version{Number: 1}
508523

509-
p = newMockPlugin(pluginName, pluginVersion, projectVersion)
524+
p = newMockPlugin(pluginName, pluginVersion, projectVersion)
510525
np1 = newMockPlugin("Plugin", pluginVersion, projectVersion)
511526
np2 = mockPlugin{pluginName, plugin.Version{Number: -1}, []config.Version{projectVersion}}
512527
np3 = newMockPlugin(pluginName, pluginVersion)
513528
np4 = newMockPlugin(pluginName, pluginVersion, config.Version{})
514-
)
529+
})
515530

516531
Context("WithCommandName", func() {
517532
It("should use provided command name", func() {

pkg/cli/resource_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ var _ = Describe("resourceOptions", func() {
3232
)
3333

3434
var (
35+
fullGVK resource.GVK
36+
noDomainGVK resource.GVK
37+
noGroupGVK resource.GVK
38+
)
39+
40+
BeforeEach(func() {
3541
fullGVK = resource.GVK{
3642
Group: group,
3743
Domain: domain,
@@ -48,7 +54,7 @@ var _ = Describe("resourceOptions", func() {
4854
Version: version,
4955
Kind: kind,
5056
}
51-
)
57+
})
5258

5359
Context("validate", func() {
5460
DescribeTable("should succeed for valid options",
@@ -68,13 +74,14 @@ var _ = Describe("resourceOptions", func() {
6874

6975
Context("newResource", func() {
7076
DescribeTable("should succeed if the Resource is valid",
71-
func(options resourceOptions) {
77+
func(getOpts func() resourceOptions) {
78+
options := getOpts()
79+
7280
Expect(options.validate()).To(Succeed())
7381

7482
resource := options.newResource()
7583
Expect(resource.Validate()).To(Succeed())
7684
Expect(resource.GVK.IsEqualTo(options.GVK)).To(BeTrue())
77-
// Plural is checked in the next test
7885
Expect(resource.Path).To(Equal(""))
7986
Expect(resource.API).NotTo(BeNil())
8087
Expect(resource.API.CRDVersion).To(Equal(""))
@@ -86,9 +93,9 @@ var _ = Describe("resourceOptions", func() {
8693
Expect(resource.Webhooks.Validation).To(BeFalse())
8794
Expect(resource.Webhooks.Conversion).To(BeFalse())
8895
},
89-
Entry("full GVK", resourceOptions{GVK: fullGVK}),
90-
Entry("missing domain", resourceOptions{GVK: noDomainGVK}),
91-
Entry("missing group", resourceOptions{GVK: noGroupGVK}),
96+
Entry("full GVK", func() resourceOptions { return resourceOptions{GVK: fullGVK} }),
97+
Entry("missing domain", func() resourceOptions { return resourceOptions{GVK: noDomainGVK} }),
98+
Entry("missing group", func() resourceOptions { return resourceOptions{GVK: noGroupGVK} }),
9299
)
93100

94101
DescribeTable("should default the Plural by pluralizing the Kind",

0 commit comments

Comments
 (0)