未验证 提交 e5f8e59c 编写于 作者: Y Yanjun Shi 提交者: GitHub

Merge pull request #128 from LinuxSuRen/add-test-cases

Add more testcases
# Jenkins CLI # Jenkins CLI
[![Go Report Card](https://goreportcard.com/badge/jenkins-zh/jenkins-cli)](https://goreportcard.com/report/jenkins-zh/jenkins-cli) [![Go Report Card](https://goreportcard.com/badge/jenkins-zh/jenkins-cli)](https://goreportcard.com/report/jenkins-zh/jenkins-cli)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jenkins-zh_jenkins-cli&metric=alert_status)](https://sonarcloud.io/dashboard?id=jenkins-zh_jenkins-cli)
Jenkins CLI 可以帮忙你轻松地管理 Jenkins。不管你是一名插件开发者、管理员或者只是一个普通的 Jenkins 用户,它都是为你而生的! Jenkins CLI 可以帮忙你轻松地管理 Jenkins。不管你是一名插件开发者、管理员或者只是一个普通的 Jenkins 用户,它都是为你而生的!
......
# Jenkins CLI # Jenkins CLI
[![Go Report Card](https://goreportcard.com/badge/jenkins-zh/jenkins-cli)](https://goreportcard.com/report/jenkins-zh/jenkins-cli) [![Go Report Card](https://goreportcard.com/badge/jenkins-zh/jenkins-cli)](https://goreportcard.com/report/jenkins-zh/jenkins-cli)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jenkins-zh_jenkins-cli&metric=alert_status)](https://sonarcloud.io/dashboard?id=jenkins-zh_jenkins-cli)
Jenkins CLI allows you manage your Jenkins as an easy way. No matter you're a plugin Jenkins CLI allows you manage your Jenkins as an easy way. No matter you're a plugin
developer, administrator or just a regular user, it borns for you! developer, administrator or just a regular user, it borns for you!
......
package cmd
import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Table util test", func() {
var (
ctrl *gomock.Controller
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
config = nil
})
AfterEach(func() {
config = nil
ctrl.Finish()
})
Context("basic test", func() {
It("getJenkinsNames", func() {
config = &Config{
JenkinsServers: []JenkinsServer{JenkinsServer{
Name: "a",
}, JenkinsServer{
Name: "b",
}},
}
names := getJenkinsNames()
Expect(names).To(Equal([]string{"a", "b"}))
config.JenkinsServers = []JenkinsServer{}
names = getJenkinsNames()
Expect(names).To(Equal([]string{}))
})
It("getCurrentJenkins", func() {
config = &Config{}
current := getCurrentJenkins()
Expect(current).To(BeNil())
config.Current = "test"
config.JenkinsServers = []JenkinsServer{JenkinsServer{
Name: "test",
}}
current = getCurrentJenkins()
Expect(current).To(Equal(&config.JenkinsServers[0]))
})
})
})
...@@ -70,7 +70,7 @@ var pluginUploadCmd = &cobra.Command{ ...@@ -70,7 +70,7 @@ var pluginUploadCmd = &cobra.Command{
log.Fatal(err) log.Fatal(err)
} }
} else if len(args) == 0 { } else if len(args) == 0 {
executePreCmd(cmd, args) executePreCmd(cmd, args, os.Stdout)
path, _ := os.Getwd() path, _ := os.Getwd()
dirName := filepath.Base(path) dirName := filepath.Base(path)
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
...@@ -99,10 +100,10 @@ func getCmdPath(cmd *cobra.Command) string { ...@@ -99,10 +100,10 @@ func getCmdPath(cmd *cobra.Command) string {
return "" return ""
} }
func executePreCmd(cmd *cobra.Command, _ []string) { func executePreCmd(cmd *cobra.Command, _ []string, writer io.Writer) (err error) {
config := getConfig() config := getConfig()
if config == nil { if config == nil {
log.Fatal("Cannot find config file") err = fmt.Errorf("Cannot find config file")
return return
} }
...@@ -112,15 +113,17 @@ func executePreCmd(cmd *cobra.Command, _ []string) { ...@@ -112,15 +113,17 @@ func executePreCmd(cmd *cobra.Command, _ []string) {
continue 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, " ") array := strings.Split(command, " ")
cmd := exec.Command(array[0], array[1:]...) cmd := exec.Command(array[0], array[1:]...)
cmd.Stdout = os.Stdout cmd.Stdout = writer
if err := cmd.Run(); err != nil { err = cmd.Run()
log.Fatal(err) return
}
} }
package cmd package cmd
import ( import (
"bytes"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
...@@ -9,16 +11,22 @@ import ( ...@@ -9,16 +11,22 @@ import (
var _ = Describe("Root cmd test", func() { var _ = Describe("Root cmd test", func() {
var ( var (
ctrl *gomock.Controller ctrl *gomock.Controller
rootCmd *cobra.Command rootCmd *cobra.Command
successCmd string
errorCmd string
) )
BeforeEach(func() { BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT()) ctrl = gomock.NewController(GinkgoT())
rootCmd = &cobra.Command{Use: "root"} rootCmd = &cobra.Command{Use: "root"}
successCmd = "echo 1"
errorCmd = "exit 1"
config = nil
}) })
AfterEach(func() { AfterEach(func() {
config = nil
ctrl.Finish() ctrl.Finish()
}) })
...@@ -52,4 +60,108 @@ var _ = Describe("Root cmd test", func() { ...@@ -52,4 +60,108 @@ var _ = Describe("Root cmd test", func() {
Expect(path).To(Equal("sub1.sub2")) Expect(path).To(Equal("sub1.sub2"))
}) })
}) })
Context("execute cmd test", func() {
It("basic command", func() {
var buf bytes.Buffer
err := execute(successCmd, &buf)
Expect(buf.String()).To(Equal("1\n"))
Expect(err).To(BeNil())
})
It("error command", func() {
var buf bytes.Buffer
err := execute(errorCmd, &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: successCmd,
}},
}
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: successCmd,
}, PreHook{
Path: "test",
Command: "echo 2",
}, PreHook{
Path: "fake",
Command: successCmd,
}},
}
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(""))
})
It("basic use case with error command, should success", func() {
config = &Config{
PreHooks: []PreHook{PreHook{
Path: "test",
Command: errorCmd,
}},
}
rootCmd := &cobra.Command{}
subCmd := &cobra.Command{
Use: "test",
}
rootCmd.AddCommand(subCmd)
var buf bytes.Buffer
err := executePreCmd(subCmd, nil, &buf)
Expect(err).To(HaveOccurred())
})
})
}) })
...@@ -9,3 +9,5 @@ sonar.sources=. ...@@ -9,3 +9,5 @@ sonar.sources=.
# Encoding of the source code. Default is default system encoding # Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8 #sonar.sourceEncoding=UTF-8
sonar.go.exclusions=**/vendor/**,**/**/*_test.go
...@@ -106,4 +106,24 @@ var _ = Describe("Table util test", func() { ...@@ -106,4 +106,24 @@ var _ = Describe("Table util test", func() {
Expect(buffer.String()).To(Equal(comp)) Expect(buffer.String()).To(Equal(comp))
}) })
}) })
Context("basic function", func() {
It("shoud success", func() {
var buffer bytes.Buffer
table := CreateTable(&buffer)
table.Render()
Expect(buffer.String()).To(Equal(""))
table.AddRow("fake")
buffer.Reset()
table.Render()
Expect(buffer.String()).To(Equal("fake\n"))
table.Clear()
buffer.Reset()
table.Render()
Expect(buffer.String()).To(Equal(""))
})
})
}) })
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册