@@ -21,11 +21,90 @@ import (
21
21
22
22
. "github.com/onsi/ginkgo"
23
23
. "github.com/onsi/gomega"
24
+ "github.com/spf13/cobra"
25
+ "github.com/spf13/pflag"
24
26
27
+ internalconfig "sigs.k8s.io/kubebuilder/internal/config"
25
28
"sigs.k8s.io/kubebuilder/pkg/model/config"
26
29
"sigs.k8s.io/kubebuilder/pkg/plugin"
27
30
)
28
31
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
+
29
108
var _ = Describe ("CLI" , func () {
30
109
31
110
var (
@@ -152,10 +231,19 @@ var _ = Describe("CLI", func() {
152
231
})
153
232
})
154
233
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
+ })
155
247
})
156
248
157
249
})
158
-
159
- func setPluginsFlag (key string ) {
160
- os .Args = append (os .Args , "init" , "--" + pluginsFlag , key )
161
- }
0 commit comments