Skip to content

Commit 70d7ef8

Browse files
authored
Merge pull request #1784 from Adirio/cli-options-tests
🌱 Add tests for cli's options that were not tested
2 parents bfad7a0 + da95ec1 commit 70d7ef8

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
@@ -137,28 +137,40 @@ var _ = Describe("CLI", func() {
137137
Context("with no plugins specified", func() {
138138
It("should return a valid CLI", func() {
139139
By("setting one plugin")
140-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1))
140+
c, err = New(
141+
WithDefaultPlugins(pluginAV1),
142+
WithPlugins(pluginAV1),
143+
)
141144
Expect(err).NotTo(HaveOccurred())
142145
Expect(c).NotTo(BeNil())
143146
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1)))
144147
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Plugin{pluginAV1}))
145148

146149
By("setting two plugins with different names and versions")
147-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV2))
150+
c, err = New(
151+
WithDefaultPlugins(pluginAV1),
152+
WithPlugins(pluginAV1, pluginBV2),
153+
)
148154
Expect(err).NotTo(HaveOccurred())
149155
Expect(c).NotTo(BeNil())
150156
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV2)))
151157
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Plugin{pluginAV1}))
152158

153159
By("setting two plugins with the same names and different versions")
154-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV2))
160+
c, err = New(
161+
WithDefaultPlugins(pluginAV1),
162+
WithPlugins(pluginAV1, pluginAV2),
163+
)
155164
Expect(err).NotTo(HaveOccurred())
156165
Expect(c).NotTo(BeNil())
157166
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginAV2)))
158167
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Plugin{pluginAV1}))
159168

160169
By("setting two plugins with different names and the same version")
161-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV1))
170+
c, err = New(
171+
WithDefaultPlugins(pluginAV1),
172+
WithPlugins(pluginAV1, pluginBV1),
173+
)
162174
Expect(err).NotTo(HaveOccurred())
163175
Expect(c).NotTo(BeNil())
164176
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV1)))
@@ -171,15 +183,22 @@ var _ = Describe("CLI", func() {
171183
Expect(err).To(MatchError(`no plugins for project version "3-alpha"`))
172184

173185
By("not setting any plugin")
174-
_, err = New(WithDefaultPlugins(pluginAV1))
186+
_, err = New(
187+
WithDefaultPlugins(pluginAV1),
188+
)
175189
Expect(err).To(MatchError(`no plugins for project version "3-alpha"`))
176190

177191
By("not setting any default plugins")
178-
_, err = New(WithPlugins(pluginAV1))
192+
_, err = New(
193+
WithPlugins(pluginAV1),
194+
)
179195
Expect(err).To(MatchError(`no default plugins for project version "3-alpha"`))
180196

181197
By("setting two plugins of the same name and version")
182-
_, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV1))
198+
_, err = New(
199+
WithDefaultPlugins(pluginAV1),
200+
WithPlugins(pluginAV1, pluginAV1),
201+
)
183202
Expect(err).To(MatchError(`broken pre-set plugins: two plugins have the same key: "go.example.com/v1"`))
184203
})
185204
})
@@ -201,31 +220,43 @@ var _ = Describe("CLI", func() {
201220
It("should return a valid CLI", func() {
202221
By(`setting cliPluginKey to "go"`)
203222
setPluginsFlag("go")
204-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV2))
223+
c, err = New(
224+
WithDefaultPlugins(pluginAV1),
225+
WithPlugins(pluginAV1, pluginAV2),
226+
)
205227
Expect(err).NotTo(HaveOccurred())
206228
Expect(c).NotTo(BeNil())
207229
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginAV2)))
208230
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Plugin{pluginAV1}))
209231

210232
By(`setting cliPluginKey to "go/v1"`)
211233
setPluginsFlag("go/v1")
212-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV2))
234+
c, err = New(
235+
WithDefaultPlugins(pluginAV1),
236+
WithPlugins(pluginAV1, pluginBV2),
237+
)
213238
Expect(err).NotTo(HaveOccurred())
214239
Expect(c).NotTo(BeNil())
215240
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV2)))
216241
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Plugin{pluginAV1}))
217242

218243
By(`setting cliPluginKey to "go/v2"`)
219244
setPluginsFlag("go/v2")
220-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginBV2))
245+
c, err = New(
246+
WithDefaultPlugins(pluginAV1),
247+
WithPlugins(pluginAV1, pluginBV2),
248+
)
221249
Expect(err).NotTo(HaveOccurred())
222250
Expect(c).NotTo(BeNil())
223251
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(pluginAV1, pluginBV2)))
224252
Expect(c.(*cli).resolvedPlugins).To(Equal([]plugin.Plugin{pluginBV2}))
225253

226254
By(`setting cliPluginKey to "go.test.com/v2"`)
227255
setPluginsFlag("go.test.com/v2")
228-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...))
256+
c, err = New(
257+
WithDefaultPlugins(pluginAV1),
258+
WithPlugins(allPlugins...),
259+
)
229260
Expect(err).NotTo(HaveOccurred())
230261
Expect(c).NotTo(BeNil())
231262
Expect(c.(*cli).pluginsFromOptions).To(Equal(makeSetByProjVer(allPlugins...)))
@@ -235,21 +266,98 @@ var _ = Describe("CLI", func() {
235266
It("should return an error", func() {
236267
By(`setting cliPluginKey to an non-existent key "foo"`)
237268
setPluginsFlag("foo")
238-
_, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(pluginAV1, pluginAV2))
269+
_, err = New(
270+
WithDefaultPlugins(pluginAV1),
271+
WithPlugins(pluginAV1, pluginAV2),
272+
)
239273
Expect(err).To(MatchError(errAmbiguousPlugin{
240274
key: "foo",
241275
msg: `no names match, possible plugins: ["go.example.com/v1" "go.example.com/v2"]`,
242276
}))
243277
})
244278
})
245279

246-
Context("with extra commands set", func() {
280+
Context("WithCommandName", func() {
281+
It("should use the provided command name", func() {
282+
commandName := "other-command"
283+
c, err = New(
284+
WithCommandName(commandName),
285+
WithDefaultPlugins(pluginAV1),
286+
WithPlugins(allPlugins...),
287+
)
288+
Expect(err).NotTo(HaveOccurred())
289+
Expect(c).NotTo(BeNil())
290+
Expect(c.(*cli).commandName).To(Equal(commandName))
291+
})
292+
})
293+
294+
Context("WithDefaultProjectVersion", func() {
295+
var defaultProjectVersion string
296+
297+
It("should use the provided default project version", func() {
298+
By(`using version "2"`)
299+
defaultProjectVersion = "2"
300+
c, err = New(
301+
WithDefaultProjectVersion(defaultProjectVersion),
302+
WithDefaultPlugins(pluginAV1),
303+
WithPlugins(allPlugins...),
304+
)
305+
Expect(err).NotTo(HaveOccurred())
306+
Expect(c).NotTo(BeNil())
307+
Expect(c.(*cli).defaultProjectVersion).To(Equal(defaultProjectVersion))
308+
309+
By(`using version "3-alpha"`)
310+
defaultProjectVersion = "3-alpha"
311+
c, err = New(
312+
WithDefaultProjectVersion(defaultProjectVersion),
313+
WithDefaultPlugins(pluginAV1),
314+
WithPlugins(allPlugins...),
315+
)
316+
Expect(err).NotTo(HaveOccurred())
317+
Expect(c).NotTo(BeNil())
318+
Expect(c.(*cli).defaultProjectVersion).To(Equal(defaultProjectVersion))
319+
})
320+
321+
It("should fail for invalid project versions", func() {
322+
By(`using version "0"`)
323+
defaultProjectVersion = "0"
324+
c, err = New(
325+
WithDefaultProjectVersion(defaultProjectVersion),
326+
WithDefaultPlugins(pluginAV1),
327+
WithPlugins(allPlugins...),
328+
)
329+
Expect(err).To(HaveOccurred())
330+
331+
By(`using version "1-gamma"`)
332+
defaultProjectVersion = "1-gamma"
333+
c, err = New(
334+
WithDefaultProjectVersion(defaultProjectVersion),
335+
WithDefaultPlugins(pluginAV1),
336+
WithPlugins(allPlugins...),
337+
)
338+
Expect(err).To(HaveOccurred())
339+
340+
By(`using version "1alpha"`)
341+
defaultProjectVersion = "1alpha"
342+
c, err = New(
343+
WithDefaultProjectVersion(defaultProjectVersion),
344+
WithDefaultPlugins(pluginAV1),
345+
WithPlugins(allPlugins...),
346+
)
347+
Expect(err).To(HaveOccurred())
348+
})
349+
})
350+
351+
Context("WithExtraCommands", func() {
247352
It("should work successfully with extra commands", func() {
248-
setPluginsFlag("go.test.com/v2")
249353
commandTest := &cobra.Command{
250354
Use: "example",
251355
}
252-
c, err = New(WithDefaultPlugins(pluginAV1), WithPlugins(allPlugins...), WithExtraCommands(commandTest))
356+
c, err = New(
357+
WithExtraCommands(commandTest),
358+
WithDefaultPlugins(pluginAV1),
359+
WithPlugins(allPlugins...),
360+
)
253361
Expect(err).NotTo(HaveOccurred())
254362
Expect(c).NotTo(BeNil())
255363
Expect(c.(*cli).extraCommands[0]).NotTo(BeNil())

0 commit comments

Comments
 (0)