Skip to content

Commit 55222be

Browse files
authored
Merge pull request #4701 from kersten/chore/cli-test-ginkgo-isolation
🌱 (chore): refactor unit tests to isolate mutable test data in CLI spec to improve maintainability
2 parents be5f5b6 + c079280 commit 55222be

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

pkg/cli/cli_test.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ func hasSubCommand(cmd *cobra.Command, name string) bool {
8080
var _ = Describe("CLI", func() {
8181
var (
8282
c *CLI
83-
projectVersion = config.Version{Number: 3}
83+
projectVersion config.Version
8484
)
8585

8686
BeforeEach(func() {
8787
c = &CLI{
8888
fs: machinery.Filesystem{FS: afero.NewMemMapFs()},
8989
}
90+
91+
projectVersion = config.Version{Number: 3}
9092
})
9193

9294
Context("buildCmd", func() {
@@ -297,7 +299,11 @@ plugins:
297299
})
298300

299301
Context("getInfoFromDefaults", func() {
300-
pluginKeys := []string{"go.kubebuilder.io/v2"}
302+
var pluginKeys []string
303+
304+
BeforeEach(func() {
305+
pluginKeys = []string{"go.kubebuilder.io/v2"}
306+
})
301307

302308
It("should be a no-op if already have plugin keys", func() {
303309
c.pluginKeys = pluginKeys
@@ -335,33 +341,33 @@ plugins:
335341
})
336342

337343
Context("resolvePlugins", func() {
338-
pluginKeys := []string{
339-
"foo.example.com/v1",
340-
"bar.example.com/v1",
341-
"baz.example.com/v1",
342-
"foo.kubebuilder.io/v1",
343-
"foo.kubebuilder.io/v2",
344-
"bar.kubebuilder.io/v1",
345-
"bar.kubebuilder.io/v2",
346-
}
347-
348-
plugins := makeMockPluginsFor(projectVersion, pluginKeys...)
349-
plugins = append(plugins,
350-
newMockPlugin("invalid.kubebuilder.io", "v1"),
351-
newMockPlugin("only1.kubebuilder.io", "v1",
352-
config.Version{Number: 1}),
353-
newMockPlugin("only2.kubebuilder.io", "v1",
354-
config.Version{Number: 2}),
355-
newMockPlugin("1and2.kubebuilder.io", "v1",
356-
config.Version{Number: 1}, config.Version{Number: 2}),
357-
newMockPlugin("2and3.kubebuilder.io", "v1",
358-
config.Version{Number: 2}, config.Version{Number: 3}),
359-
newMockPlugin("1-2and3.kubebuilder.io", "v1",
360-
config.Version{Number: 1}, config.Version{Number: 2}, config.Version{Number: 3}),
361-
)
362-
pluginMap := makeMapFor(plugins...)
363-
364344
BeforeEach(func() {
345+
pluginKeys := []string{
346+
"foo.example.com/v1",
347+
"bar.example.com/v1",
348+
"baz.example.com/v1",
349+
"foo.kubebuilder.io/v1",
350+
"foo.kubebuilder.io/v2",
351+
"bar.kubebuilder.io/v1",
352+
"bar.kubebuilder.io/v2",
353+
}
354+
355+
plugins := makeMockPluginsFor(projectVersion, pluginKeys...)
356+
plugins = append(plugins,
357+
newMockPlugin("invalid.kubebuilder.io", "v1"),
358+
newMockPlugin("only1.kubebuilder.io", "v1",
359+
config.Version{Number: 1}),
360+
newMockPlugin("only2.kubebuilder.io", "v1",
361+
config.Version{Number: 2}),
362+
newMockPlugin("1and2.kubebuilder.io", "v1",
363+
config.Version{Number: 1}, config.Version{Number: 2}),
364+
newMockPlugin("2and3.kubebuilder.io", "v1",
365+
config.Version{Number: 2}, config.Version{Number: 3}),
366+
newMockPlugin("1-2and3.kubebuilder.io", "v1",
367+
config.Version{Number: 1}, config.Version{Number: 2}, config.Version{Number: 3}),
368+
)
369+
pluginMap := makeMapFor(plugins...)
370+
365371
c.plugins = pluginMap
366372
})
367373

0 commit comments

Comments
 (0)