package cmd import ( "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/spf13/cobra" ) var _ = Describe("Root cmd test", func() { var ( ctrl *gomock.Controller rootCmd *cobra.Command ) BeforeEach(func() { ctrl = gomock.NewController(GinkgoT()) rootCmd = &cobra.Command{Use: "root"} }) AfterEach(func() { ctrl.Finish() }) Context("PreHook test", func() { It("only with root cmd", func() { path := getCmdPath(rootCmd) Expect(path).To(Equal("")) }) It("one sub cmd", func() { subCmd := &cobra.Command{ Use: "sub", } rootCmd.AddCommand(subCmd) path := getCmdPath(subCmd) Expect(path).To(Equal("sub")) }) It("two sub cmds", func() { sub1Cmd := &cobra.Command{ Use: "sub1", } sub2Cmd := &cobra.Command{ Use: "sub2", } rootCmd.AddCommand(sub1Cmd) sub1Cmd.AddCommand(sub2Cmd) path := getCmdPath(sub2Cmd) Expect(path).To(Equal("sub1.sub2")) }) }) })