Skip to content

Commit 517a7ec

Browse files
authored
Merge pull request #1771 from camilamacedo86/cleanup-cli-test
🌱 improve cli tests
2 parents 76e8d21 + 14ce146 commit 517a7ec

File tree

2 files changed

+92
-82
lines changed

2 files changed

+92
-82
lines changed

pkg/cli/cli_suite_test.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -21,87 +21,9 @@ import (
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
24-
25-
"github.com/spf13/pflag"
26-
27-
internalconfig "sigs.k8s.io/kubebuilder/internal/config"
28-
"sigs.k8s.io/kubebuilder/pkg/model/config"
29-
"sigs.k8s.io/kubebuilder/pkg/plugin"
3024
)
3125

3226
func TestCLI(t *testing.T) {
3327
RegisterFailHandler(Fail)
3428
RunSpecs(t, "CLI Suite")
3529
}
36-
37-
// Test plugin types and constructors.
38-
type mockPlugin struct {
39-
name string
40-
version plugin.Version
41-
projectVersions []string
42-
}
43-
44-
func (p mockPlugin) Name() string { return p.name }
45-
func (p mockPlugin) Version() plugin.Version { return p.version }
46-
func (p mockPlugin) SupportedProjectVersions() []string { return p.projectVersions }
47-
48-
func (mockPlugin) UpdateContext(*plugin.Context) {}
49-
func (mockPlugin) BindFlags(*pflag.FlagSet) {}
50-
func (mockPlugin) InjectConfig(*config.Config) {}
51-
func (mockPlugin) Run() error { return nil }
52-
53-
func makeBasePlugin(name, version string, projVers ...string) plugin.Base {
54-
v, err := plugin.ParseVersion(version)
55-
if err != nil {
56-
panic(err)
57-
}
58-
return mockPlugin{name, v, projVers}
59-
}
60-
61-
func makePluginsForKeys(keys ...string) (plugins []plugin.Base) {
62-
for _, key := range keys {
63-
n, v := plugin.SplitKey(key)
64-
plugins = append(plugins, makeBasePlugin(n, v, internalconfig.DefaultVersion))
65-
}
66-
return
67-
}
68-
69-
type mockAllPlugin struct {
70-
mockPlugin
71-
mockInitPlugin
72-
mockCreateAPIPlugin
73-
mockCreateWebhookPlugin
74-
}
75-
76-
type mockInitPlugin struct{ mockPlugin }
77-
type mockCreateAPIPlugin struct{ mockPlugin }
78-
type mockCreateWebhookPlugin struct{ mockPlugin }
79-
80-
// GetInitPlugin will return the plugin which is responsible for initialized the project
81-
func (p mockInitPlugin) GetInitPlugin() plugin.Init { return p }
82-
83-
// GetCreateAPIPlugin will return the plugin which is responsible for scaffolding APIs for the project
84-
func (p mockCreateAPIPlugin) GetCreateAPIPlugin() plugin.CreateAPI { return p }
85-
86-
// GetCreateWebhookPlugin will return the plugin which is responsible for scaffolding webhooks for the project
87-
func (p mockCreateWebhookPlugin) GetCreateWebhookPlugin() plugin.CreateWebhook { return p }
88-
89-
func makeAllPlugin(name, version string, projectVersions ...string) plugin.Base {
90-
p := makeBasePlugin(name, version, projectVersions...).(mockPlugin)
91-
return mockAllPlugin{
92-
p,
93-
mockInitPlugin{p},
94-
mockCreateAPIPlugin{p},
95-
mockCreateWebhookPlugin{p},
96-
}
97-
}
98-
99-
func makeSetByProjVer(ps ...plugin.Base) map[string][]plugin.Base {
100-
set := make(map[string][]plugin.Base)
101-
for _, p := range ps {
102-
for _, version := range p.SupportedProjectVersions() {
103-
set[version] = append(set[version], p)
104-
}
105-
}
106-
return set
107-
}

pkg/cli/cli_test.go

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,90 @@ import (
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/pflag"
2426

27+
internalconfig "sigs.k8s.io/kubebuilder/internal/config"
2528
"sigs.k8s.io/kubebuilder/pkg/model/config"
2629
"sigs.k8s.io/kubebuilder/pkg/plugin"
2730
)
2831

32+
// Test plugin types and constructors.
33+
type mockPlugin struct {
34+
name string
35+
version plugin.Version
36+
projectVersions []string
37+
}
38+
39+
func (p mockPlugin) Name() string { return p.name }
40+
func (p mockPlugin) Version() plugin.Version { return p.version }
41+
func (p mockPlugin) SupportedProjectVersions() []string { return p.projectVersions }
42+
43+
func (mockPlugin) UpdateContext(*plugin.Context) {}
44+
func (mockPlugin) BindFlags(*pflag.FlagSet) {}
45+
func (mockPlugin) InjectConfig(*config.Config) {}
46+
func (mockPlugin) Run() error { return nil }
47+
48+
func makeBasePlugin(name, version string, projVers ...string) plugin.Base {
49+
v, err := plugin.ParseVersion(version)
50+
if err != nil {
51+
panic(err)
52+
}
53+
return mockPlugin{name, v, projVers}
54+
}
55+
56+
func makePluginsForKeys(keys ...string) (plugins []plugin.Base) {
57+
for _, key := range keys {
58+
n, v := plugin.SplitKey(key)
59+
plugins = append(plugins, makeBasePlugin(n, v, internalconfig.DefaultVersion))
60+
}
61+
return
62+
}
63+
64+
type mockAllPlugin struct {
65+
mockPlugin
66+
mockInitPlugin
67+
mockCreateAPIPlugin
68+
mockCreateWebhookPlugin
69+
}
70+
71+
type mockInitPlugin struct{ mockPlugin }
72+
type mockCreateAPIPlugin struct{ mockPlugin }
73+
type mockCreateWebhookPlugin struct{ mockPlugin }
74+
75+
// GetInitPlugin will return the plugin which is responsible for initialized the project
76+
func (p mockInitPlugin) GetInitPlugin() plugin.Init { return p }
77+
78+
// GetCreateAPIPlugin will return the plugin which is responsible for scaffolding APIs for the project
79+
func (p mockCreateAPIPlugin) GetCreateAPIPlugin() plugin.CreateAPI { return p }
80+
81+
// GetCreateWebhookPlugin will return the plugin which is responsible for scaffolding webhooks for the project
82+
func (p mockCreateWebhookPlugin) GetCreateWebhookPlugin() plugin.CreateWebhook { return p }
83+
84+
func makeAllPlugin(name, version string, projectVersions ...string) plugin.Base {
85+
p := makeBasePlugin(name, version, projectVersions...).(mockPlugin)
86+
return mockAllPlugin{
87+
p,
88+
mockInitPlugin{p},
89+
mockCreateAPIPlugin{p},
90+
mockCreateWebhookPlugin{p},
91+
}
92+
}
93+
94+
func makeSetByProjVer(ps ...plugin.Base) map[string][]plugin.Base {
95+
set := make(map[string][]plugin.Base)
96+
for _, p := range ps {
97+
for _, version := range p.SupportedProjectVersions() {
98+
set[version] = append(set[version], p)
99+
}
100+
}
101+
return set
102+
}
103+
104+
func setPluginsFlag(key string) {
105+
os.Args = append(os.Args, "init", "--"+pluginsFlag, key)
106+
}
107+
29108
var _ = Describe("CLI", func() {
30109

31110
var (
@@ -152,10 +231,19 @@ var _ = Describe("CLI", func() {
152231
})
153232
})
154233

234+
Context("with extra commands set", func() {
235+
It("should work successfully with extra commands", func() {
236+
setPluginsFlag("go.test.com/v2")
237+
commandTest := &cobra.Command{
238+
Use: "example",
239+
}
240+
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...), WithExtraCommands(commandTest))
241+
Expect(err).NotTo(HaveOccurred())
242+
Expect(c).NotTo(BeNil())
243+
Expect(c.(*cli).extraCommands[0]).NotTo(BeNil())
244+
Expect(c.(*cli).extraCommands[0].Use).To(Equal(commandTest.Use))
245+
})
246+
})
155247
})
156248

157249
})
158-
159-
func setPluginsFlag(key string) {
160-
os.Args = append(os.Args, "init", "--"+pluginsFlag, key)
161-
}

0 commit comments

Comments
 (0)