Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix alpha extra commands parent command #2092

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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