Skip to content

Commit

Permalink
Merge pull request #2092 from Adirio/fix-alpha-subcommands
Browse files Browse the repository at this point in the history
🐛 Fix alpha extra commands parent command
  • Loading branch information
k8s-ci-robot authored Mar 15, 2021
2 parents 4b4d28a + 05be3c2 commit bebd89c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *CLI) addExtraAlphaCommands() error {
return fmt.Errorf("command %q already exists", fmt.Sprintf("%s %s", alphaCommand, cmd.Name()))
}
}
c.cmd.AddCommand(cmd)
alpha.AddCommand(cmd)
}
return nil
}
39 changes: 30 additions & 9 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func setPluginsFlag(value string) {
setFlag(pluginsFlag, value)
}

func hasSubCommand(c *CLI, name string) bool {
for _, subcommand := range c.cmd.Commands() {
func hasSubCommand(cmd *cobra.Command, name string) bool {
for _, subcommand := range cmd.Commands() {
if subcommand.Name() == name {
return true
}
Expand Down Expand Up @@ -677,15 +677,15 @@ var _ = Describe("CLI", func() {
const version = "version string"
c, err = New(WithVersion(version))
Expect(err).NotTo(HaveOccurred())
Expect(hasSubCommand(c, "version")).To(BeTrue())
Expect(hasSubCommand(c.cmd, "version")).To(BeTrue())
})
})

When("enabling completion", func() {
It("should create a valid CLI", func() {
c, err = New(WithCompletion())
Expect(err).NotTo(HaveOccurred())
Expect(hasSubCommand(c, "completion")).To(BeTrue())
Expect(hasSubCommand(c.cmd, "completion")).To(BeTrue())
})
})

Expand All @@ -712,22 +712,43 @@ var _ = Describe("CLI", func() {
})

When("providing extra commands", func() {
var extraCommand *cobra.Command

It("should create a valid CLI for non-conflicting ones", func() {
extraCommand = &cobra.Command{Use: "extra"}
extraCommand := &cobra.Command{Use: "extra"}
c, err = New(WithExtraCommands(extraCommand))
Expect(err).NotTo(HaveOccurred())
Expect(hasSubCommand(c, extraCommand.Use)).To(BeTrue())
Expect(hasSubCommand(c.cmd, extraCommand.Use)).To(BeTrue())
})

It("should return an error for conflicting ones", func() {
extraCommand = &cobra.Command{Use: "init"}
extraCommand := &cobra.Command{Use: "init"}
_, err = New(WithExtraCommands(extraCommand))
Expect(err).To(HaveOccurred())
})
})

When("providing extra alpha commands", func() {
It("should create a valid CLI for non-conflicting ones", func() {
extraAlphaCommand := &cobra.Command{Use: "extra"}
c, err = New(WithExtraAlphaCommands(extraAlphaCommand))
Expect(err).NotTo(HaveOccurred())
var alpha *cobra.Command
for _, subcmd := range c.cmd.Commands() {
if subcmd.Name() == alphaCommand {
alpha = subcmd
break
}
}
Expect(alpha).NotTo(BeNil())
Expect(hasSubCommand(alpha, extraAlphaCommand.Use)).To(BeTrue())
})

It("should return an error for conflicting ones", func() {
extraAlphaCommand := &cobra.Command{Use: "extra"}
_, err = New(WithExtraAlphaCommands(extraAlphaCommand, extraAlphaCommand))
Expect(err).To(HaveOccurred())
})
})

When("providing deprecated plugins", func() {
It("should succeed and print the deprecation notice", func() {
const (
Expand Down

0 comments on commit bebd89c

Please sign in to comment.