root_test.go 3.3 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3
package cmd

import (
LinuxSuRen's avatar
LinuxSuRen 已提交
4
	"bytes"
LinuxSuRen's avatar
LinuxSuRen 已提交
5 6 7 8 9 10 11 12
	"github.com/golang/mock/gomock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/spf13/cobra"
)

var _ = Describe("Root cmd test", func() {
	var (
LinuxSuRen's avatar
LinuxSuRen 已提交
13 14 15 16
		ctrl       *gomock.Controller
		rootCmd    *cobra.Command
		successCmd string
		errorCmd   string
LinuxSuRen's avatar
LinuxSuRen 已提交
17 18 19 20 21
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		rootCmd = &cobra.Command{Use: "root"}
LinuxSuRen's avatar
LinuxSuRen 已提交
22 23
		successCmd = "echo 1"
		errorCmd = "exit 1"
24
		config = nil
LinuxSuRen's avatar
LinuxSuRen 已提交
25 26 27
	})

	AfterEach(func() {
28
		config = nil
LinuxSuRen's avatar
LinuxSuRen 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
		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"))
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
62 63 64 65

	Context("execute cmd test", func() {
		It("basic command", func() {
			var buf bytes.Buffer
LinuxSuRen's avatar
LinuxSuRen 已提交
66
			err := execute(successCmd, &buf)
LinuxSuRen's avatar
LinuxSuRen 已提交
67 68 69 70 71 72 73

			Expect(buf.String()).To(Equal("1\n"))
			Expect(err).To(BeNil())
		})

		It("error command", func() {
			var buf bytes.Buffer
LinuxSuRen's avatar
LinuxSuRen 已提交
74
			err := execute(errorCmd, &buf)
LinuxSuRen's avatar
LinuxSuRen 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87

			Expect(err).To(HaveOccurred())
		})
	})

	Context("execute pre cmd", func() {
		It("should error", func() {
			err := executePreCmd(nil, nil, nil)
			Expect(err).To(HaveOccurred())
		})

		It("basic use case with one preHook, should success", func() {
			config = &Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
88
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
89
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
90
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
				}},
			}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal("1\n"))
		})

		It("basic use case with many preHooks, should success", func() {
			config = &Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
108
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
109
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
110
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
111
				}, CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
112 113
					Path:    "test",
					Command: "echo 2",
LinuxSuRen's avatar
LinuxSuRen 已提交
114
				}, CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
115
					Path:    "fake",
LinuxSuRen's avatar
LinuxSuRen 已提交
116
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
				}},
			}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal("1\n2\n"))
		})

		It("basic use case without preHooks, should success", func() {
			config = &Config{}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(BeNil())
			Expect(buf.String()).To(Equal(""))
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
146 147 148

		It("basic use case with error command, should success", func() {
			config = &Config{
LinuxSuRen's avatar
LinuxSuRen 已提交
149
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
150
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
151
					Command: errorCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
				}},
			}

			rootCmd := &cobra.Command{}
			subCmd := &cobra.Command{
				Use: "test",
			}
			rootCmd.AddCommand(subCmd)

			var buf bytes.Buffer
			err := executePreCmd(subCmd, nil, &buf)
			Expect(err).To(HaveOccurred())
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
165
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
166
})