completion_test.go 1.9 KB
Newer Older
LinuxSuRen's avatar
LinuxSuRen 已提交
1 2 3 4 5 6 7 8 9 10 11 12
package cmd

import (
	"bytes"
	"io/ioutil"
	"os"

	"github.com/golang/mock/gomock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

13
var _ = Describe("completion command", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
14
	var (
15 16 17 18
		ctrl    *gomock.Controller
		cmdArgs []string
		buf     *bytes.Buffer
		err     error
LinuxSuRen's avatar
LinuxSuRen 已提交
19 20 21 22 23 24 25
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		rootCmd.SetArgs([]string{})
		rootOptions.Jenkins = ""
		rootOptions.ConfigFile = "test.yaml"
26 27 28 29 30 31 32 33 34 35 36 37 38 39

		var data []byte
		data, err = generateSampleConfig()
		Expect(err).To(BeNil())
		err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
		Expect(err).To(BeNil())
	})

	JustBeforeEach(func() {
		rootCmd.SetArgs(cmdArgs)

		buf = new(bytes.Buffer)
		rootCmd.SetOutput(buf)
		_, err = rootCmd.ExecuteC()
LinuxSuRen's avatar
LinuxSuRen 已提交
40 41 42 43 44 45 46 47 48
	})

	AfterEach(func() {
		rootCmd.SetArgs([]string{})
		os.Remove(rootOptions.ConfigFile)
		rootOptions.ConfigFile = ""
		ctrl.Finish()
	})

49 50 51 52 53 54
	Context("with default option value", func() {
		BeforeEach(func() {
			cmdArgs = []string{"completion"}
		})

		It("should success", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
55
			Expect(err).To(BeNil())
56 57 58 59 60 61 62 63 64 65
			Expect(buf.String()).To(ContainSubstring("bash completion for jcli"))
		})
	})

	Context("generate zsh completion", func() {
		BeforeEach(func() {
			cmdArgs = []string{"completion", "--type", "zsh"}
		})

		It("should success", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
66
			Expect(err).To(BeNil())
67 68 69
			Expect(buf.String()).NotTo(Equal(""))
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
70

71 72 73 74
	Context("generate powerShell completion", func() {
		BeforeEach(func() {
			cmdArgs = []string{"completion", "--type", "powerShell"}
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
75

76
		It("should success", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
77
			Expect(err).To(BeNil())
78 79 80
			Expect(buf.String()).To(ContainSubstring("using namespace System.Management.Automation"))
		})
	})
LinuxSuRen's avatar
LinuxSuRen 已提交
81

82 83 84 85 86 87 88 89
	Context("generate unknown shell type completion", func() {
		BeforeEach(func() {
			cmdArgs = []string{"completion", "--type", "fake"}
		})

		It("error occurred", func() {
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("unknown shell type"))
LinuxSuRen's avatar
LinuxSuRen 已提交
90 91 92
		})
	})
})