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

import (
LinuxSuRen's avatar
LinuxSuRen 已提交
4 5
	"bytes"

LinuxSuRen's avatar
LinuxSuRen 已提交
6 7 8 9 10 11 12 13
	"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 已提交
14 15 16 17
		ctrl       *gomock.Controller
		rootCmd    *cobra.Command
		successCmd string
		errorCmd   string
LinuxSuRen's avatar
LinuxSuRen 已提交
18 19 20 21 22
	)

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

	AfterEach(func() {
29
		config = nil
LinuxSuRen's avatar
LinuxSuRen 已提交
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 62
		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 已提交
63 64 65 66

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

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

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

			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 已提交
89
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
90
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
91
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
				}},
			}

			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 已提交
109
				PreHooks: []CommndHook{CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
110
					Path:    "test",
LinuxSuRen's avatar
LinuxSuRen 已提交
111
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
112
				}, CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
113 114
					Path:    "test",
					Command: "echo 2",
LinuxSuRen's avatar
LinuxSuRen 已提交
115
				}, CommndHook{
LinuxSuRen's avatar
LinuxSuRen 已提交
116
					Path:    "fake",
LinuxSuRen's avatar
LinuxSuRen 已提交
117
					Command: successCmd,
LinuxSuRen's avatar
LinuxSuRen 已提交
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 146
				}},
			}

			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 已提交
147 148 149

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

			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 已提交
166
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
167
})