Skip to content

Commit da95ec1

Browse files
committed
Add tests for cli's options that were not tested
Signed-off-by: Adrian Orive <[email protected]>
1 parent b2983b2 commit da95ec1

File tree

2 files changed

+124
-15
lines changed

2 files changed

+124
-15
lines changed

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
func main() {
2929
c, err := cli.New(
30+
cli.WithCommandName("kubebuilder"),
3031
cli.WithPlugins(
3132
&pluginv2.Plugin{},
3233
&pluginv3.Plugin{},

pkg/cli/cli_test.go

Lines changed: 123 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,40 @@ var _ = Describe("CLI", func() {
125125
Context("with no plugins specified", func() {
126126
It("should return a valid CLI", func() {
127127
By("setting one plugin")
128-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1))
128+
c, err = New(
129+
WithDefaultPlugins(pluginAV1),
130+
WithPlugins(pluginAV1),
131+
)
129132
Expect(err).NotTo(HaveOccurred())
130133
Expect(c).NotTo(BeNil())
131134
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1)))
132135
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Base{pluginAV1}))
133136

134137
By("setting two plugins with different names and versions")
135-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV2))
138+
c, err = New(
139+
WithDefaultPlugins(pluginAV1),
140+
WithPlugins(pluginAV1, pluginBV2),
141+
)
136142
Expect(err).NotTo(HaveOccurred())
137143
Expect(c).NotTo(BeNil())
138144
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV2)))
139145
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Base{pluginAV1}))
140146

141147
By("setting two plugins with the same names and different versions")
142-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV2))
148+
c, err = New(
149+
WithDefaultPlugins(pluginAV1),
150+
WithPlugins(pluginAV1, pluginAV2),
151+
)
143152
Expect(err).NotTo(HaveOccurred())
144153
Expect(c).NotTo(BeNil())
145154
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginAV2)))
146155
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Base{pluginAV1}))
147156

148157
By("setting two plugins with different names and the same version")
149-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV1))
158+
c, err = New(
159+
WithDefaultPlugins(pluginAV1),
160+
WithPlugins(pluginAV1, pluginBV1),
161+
)
150162
Expect(err).NotTo(HaveOccurred())
151163
Expect(c).NotTo(BeNil())
152164
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV1)))
@@ -159,15 +171,22 @@ var _ = Describe("CLI", func() {
159171
Expect(err).To(MatchError(`no plugins for project version "3-alpha"`))
160172

161173
By("not setting any plugin")
162-
_, err = New(WithDefaultPlugins(pluginAV1))
174+
_, err = New(
175+
WithDefaultPlugins(pluginAV1),
176+
)
163177
Expect(err).To(MatchError(`no plugins for project version "3-alpha"`))
164178

165179
By("not setting any default plugins")
166-
_, err = New(WithPlugins(pluginAV1))
180+
_, err = New(
181+
WithPlugins(pluginAV1),
182+
)
167183
Expect(err).To(MatchError(`no default plugins for project version "3-alpha"`))
168184

169185
By("setting two plugins of the same name and version")
170-
_, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV1))
186+
_, err = New(
187+
WithDefaultPlugins(pluginAV1),
188+
WithPlugins(pluginAV1, pluginAV1),
189+
)
171190
Expect(err).To(MatchError(`broken pre-set plugins: two plugins have the same key: "go.example.com/v1"`))
172191
})
173192
})
@@ -189,31 +208,43 @@ var _ = Describe("CLI", func() {
189208
It("should return a valid CLI", func() {
190209
By(`setting cliPluginKey to "go"`)
191210
setPluginsFlag("go")
192-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV2))
211+
c, err = New(
212+
WithDefaultPlugins(pluginAV1),
213+
WithPlugins(pluginAV1, pluginAV2),
214+
)
193215
Expect(err).NotTo(HaveOccurred())
194216
Expect(c).NotTo(BeNil())
195217
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginAV2)))
196218
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Base{pluginAV1}))
197219

198220
By(`setting cliPluginKey to "go/v1"`)
199221
setPluginsFlag("go/v1")
200-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV2))
222+
c, err = New(
223+
WithDefaultPlugins(pluginAV1),
224+
WithPlugins(pluginAV1, pluginBV2),
225+
)
201226
Expect(err).NotTo(HaveOccurred())
202227
Expect(c).NotTo(BeNil())
203228
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV2)))
204229
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Base{pluginAV1}))
205230

206231
By(`setting cliPluginKey to "go/v2"`)
207232
setPluginsFlag("go/v2")
208-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV2))
233+
c, err = New(
234+
WithDefaultPlugins(pluginAV1),
235+
WithPlugins(pluginAV1, pluginBV2),
236+
)
209237
Expect(err).NotTo(HaveOccurred())
210238
Expect(c).NotTo(BeNil())
211239
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV2)))
212240
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Base{pluginBV2}))
213241

214242
By(`setting cliPluginKey to "go.test.com/v2"`)
215243
setPluginsFlag("go.test.com/v2")
216-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...))
244+
c, err = New(
245+
WithDefaultPlugins(pluginAV1),
246+
WithPlugins(allPlugins...),
247+
)
217248
Expect(err).NotTo(HaveOccurred())
218249
Expect(c).NotTo(BeNil())
219250
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(allPlugins...)))
@@ -223,21 +254,98 @@ var _ = Describe("CLI", func() {
223254
It("should return an error", func() {
224255
By(`setting cliPluginKey to an non-existent key "foo"`)
225256
setPluginsFlag("foo")
226-
_, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV2))
257+
_, err = New(
258+
WithDefaultPlugins(pluginAV1),
259+
WithPlugins(pluginAV1, pluginAV2),
260+
)
227261
Expect(err).To(MatchError(errAmbiguousPlugin{
228262
key: "foo",
229263
msg: `no names match, possible plugins: ["go.example.com/v1" "go.example.com/v2"]`,
230264
}))
231265
})
232266
})
233267

234-
Context("with extra commands set", func() {
268+
Context("WithCommandName", func() {
269+
It("should use the provided command name", func() {
270+
commandName := "other-command"
271+
c, err = New(
272+
WithCommandName(commandName),
273+
WithDefaultPlugins(pluginAV1),
274+
WithPlugins(allPlugins...),
275+
)
276+
Expect(err).NotTo(HaveOccurred())
277+
Expect(c).NotTo(BeNil())
278+
Expect(c.(*cli).commandName).To(Equal(commandName))
279+
})
280+
})
281+
282+
Context("WithDefaultProjectVersion", func() {
283+
var defaultProjectVersion string
284+
285+
It("should use the provided default project version", func() {
286+
By(`using version "2"`)
287+
defaultProjectVersion = "2"
288+
c, err = New(
289+
WithDefaultProjectVersion(defaultProjectVersion),
290+
WithDefaultPlugins(pluginAV1),
291+
WithPlugins(allPlugins...),
292+
)
293+
Expect(err).NotTo(HaveOccurred())
294+
Expect(c).NotTo(BeNil())
295+
Expect(c.(*cli).defaultProjectVersion).To(Equal(defaultProjectVersion))
296+
297+
By(`using version "3-alpha"`)
298+
defaultProjectVersion = "3-alpha"
299+
c, err = New(
300+
WithDefaultProjectVersion(defaultProjectVersion),
301+
WithDefaultPlugins(pluginAV1),
302+
WithPlugins(allPlugins...),
303+
)
304+
Expect(err).NotTo(HaveOccurred())
305+
Expect(c).NotTo(BeNil())
306+
Expect(c.(*cli).defaultProjectVersion).To(Equal(defaultProjectVersion))
307+
})
308+
309+
It("should fail for invalid project versions", func() {
310+
By(`using version "0"`)
311+
defaultProjectVersion = "0"
312+
c, err = New(
313+
WithDefaultProjectVersion(defaultProjectVersion),
314+
WithDefaultPlugins(pluginAV1),
315+
WithPlugins(allPlugins...),
316+
)
317+
Expect(err).To(HaveOccurred())
318+
319+
By(`using version "1-gamma"`)
320+
defaultProjectVersion = "1-gamma"
321+
c, err = New(
322+
WithDefaultProjectVersion(defaultProjectVersion),
323+
WithDefaultPlugins(pluginAV1),
324+
WithPlugins(allPlugins...),
325+
)
326+
Expect(err).To(HaveOccurred())
327+
328+
By(`using version "1alpha"`)
329+
defaultProjectVersion = "1alpha"
330+
c, err = New(
331+
WithDefaultProjectVersion(defaultProjectVersion),
332+
WithDefaultPlugins(pluginAV1),
333+
WithPlugins(allPlugins...),
334+
)
335+
Expect(err).To(HaveOccurred())
336+
})
337+
})
338+
339+
Context("WithExtraCommands", func() {
235340
It("should work successfully with extra commands", func() {
236-
setPluginsFlag("go.test.com/v2")
237341
commandTest := &cobra.Command{
238342
Use: "example",
239343
}
240-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...), WithExtraCommands(commandTest))
344+
c, err = New(
345+
WithExtraCommands(commandTest),
346+
WithDefaultPlugins(pluginAV1),
347+
WithPlugins(allPlugins...),
348+
)
241349
Expect(err).NotTo(HaveOccurred())
242350
Expect(c).NotTo(BeNil())
243351
Expect(c.(*cli).extraCommands[0]).NotTo(BeNil())

0 commit comments

Comments
 (0)