提交 65acba59 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Add more testcases

上级 33900add
......@@ -70,7 +70,7 @@ var pluginUploadCmd = &cobra.Command{
log.Fatal(err)
}
} else if len(args) == 0 {
executePreCmd(cmd, args)
executePreCmd(cmd, args, os.Stdout)
path, _ := os.Getwd()
dirName := filepath.Base(path)
......
......@@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"io"
"log"
"os"
"os/exec"
......@@ -99,10 +100,10 @@ func getCmdPath(cmd *cobra.Command) string {
return ""
}
func executePreCmd(cmd *cobra.Command, _ []string) {
func executePreCmd(cmd *cobra.Command, _ []string, writer io.Writer) (err error) {
config := getConfig()
if config == nil {
log.Fatal("Cannot find config file")
err = fmt.Errorf("Cannot find config file")
return
}
......@@ -112,15 +113,17 @@ func executePreCmd(cmd *cobra.Command, _ []string) {
continue
}
execute(preHook.Command)
if err = execute(preHook.Command, writer); err != nil {
return
}
}
return
}
func execute(command string) {
func execute(command string, writer io.Writer) (err error) {
array := strings.Split(command, " ")
cmd := exec.Command(array[0], array[1:]...)
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
cmd.Stdout = writer
err = cmd.Run()
return
}
package cmd
import (
"bytes"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
......@@ -52,4 +54,89 @@ var _ = Describe("Root cmd test", func() {
Expect(path).To(Equal("sub1.sub2"))
})
})
Context("execute cmd test", func() {
It("basic command", func() {
var buf bytes.Buffer
err := execute("echo 1", &buf)
Expect(buf.String()).To(Equal("1\n"))
Expect(err).To(BeNil())
})
It("error command", func() {
var buf bytes.Buffer
err := execute("exit 1", &buf)
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{
PreHooks: []PreHook{PreHook{
Path: "test",
Command: "echo 1",
}},
}
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{
PreHooks: []PreHook{PreHook{
Path: "test",
Command: "echo 1",
}, PreHook{
Path: "test",
Command: "echo 2",
}, PreHook{
Path: "fake",
Command: "echo 1",
}},
}
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(""))
})
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册