From c5222dbc7bbcc002498f40347c6627b4528bac38 Mon Sep 17 00:00:00 2001 From: Roy Li Date: Fri, 25 Dec 2020 14:43:36 +0800 Subject: [PATCH] test: add test --- commandnew.go | 20 +++++++---- commandnew_test.go | 32 +++++++++++++++++ commandversion.go | 2 +- commandversion_test.go | 13 +++++++ go.sum | 1 + main.go | 12 +++++-- main_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 commandnew_test.go create mode 100644 commandversion_test.go create mode 100644 main_test.go diff --git a/commandnew.go b/commandnew.go index 8a3e248..62f34f7 100644 --- a/commandnew.go +++ b/commandnew.go @@ -24,6 +24,14 @@ func (x *NewCommand) Usage() string { } func (x *NewCommand) Execute(args []string) error { + code, err := x.Run(args) + + os.Exit(code) + + return err +} + +func (x *NewCommand) Run(args []string) (int, error) { var content string if isInputFromPipe() { @@ -33,7 +41,7 @@ func (x *NewCommand) Execute(args []string) error { } if content == "" { - return errors.New("you must specify the content of the memo") + return 1, errors.New("you must specify the content of the memo") } memo := client.Memo{ @@ -50,20 +58,18 @@ func (x *NewCommand) Execute(args []string) error { re, _ := err.(*client.ResponseError) if re.StatusCode >= 400 && re.StatusCode < 500 { - os.Exit(2) + return 2, err } else { - os.Exit(1) + return 1, err } default: fmt.Println(err) - os.Exit(1) + return 1, err } } log.Println(*responseMessage) - os.Exit(0) - - return nil + return 0, nil } func isInputFromPipe() bool { diff --git a/commandnew_test.go b/commandnew_test.go new file mode 100644 index 0000000..c80defa --- /dev/null +++ b/commandnew_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "testing" +) + +func TestNewCommand_Usage(t *testing.T) { + newCommand := NewCommand{} + + newCommand.Usage() +} + +func TestNewCommand_Run(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + requestBody, _ := ioutil.ReadAll(req.Body) + assert.Equal(t, `{"content":"测试内容"}`, string(requestBody[:])) + rw.Write([]byte(`{"code":0,"message":"记录成功"}`)) + })) + + newCommand := NewCommand{ + Api: server.URL, + } + + if _, err := newCommand.Run([]string{"测试内容"}); err != nil { + log.Fatal(err) + } +} diff --git a/commandversion.go b/commandversion.go index 271302b..5536103 100644 --- a/commandversion.go +++ b/commandversion.go @@ -5,7 +5,7 @@ import "fmt" type VersionCommand struct {} func (x VersionCommand) Execute(args []string) error { - fmt.Printf("Version: %s", Version) + fmt.Printf("Version: %s\n", Version) return nil } diff --git a/commandversion_test.go b/commandversion_test.go new file mode 100644 index 0000000..24413e8 --- /dev/null +++ b/commandversion_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "testing" +) + +func Test_CLI(t *testing.T) { + versionCommand := VersionCommand{} + + if err := versionCommand.Execute([]string{}); err != nil { + t.Fail() + } +} diff --git a/go.sum b/go.sum index 3d367a0..014bb7e 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,7 @@ golang.org/x/tools v0.0.0-20200530233709-52effbd89c51/go.mod h1:EkVYQZoAsY45+roY golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 0807ac1..bf55c94 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,10 @@ import ( ) func main() { + os.Exit(run()) +} + +func run() int { var options struct{} var parser = flags.NewParser(&options, flags.Default) @@ -22,11 +26,13 @@ func main() { case *flags.Error: fe, _ := err.(*flags.Error) if fe.Type == flags.ErrHelp { - os.Exit(0) + return 0 } - os.Exit(1) + return 1 default: - os.Exit(1) + return 1 } } + + return 0 } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..5f77553 --- /dev/null +++ b/main_test.go @@ -0,0 +1,81 @@ +package main + +import ( + "bytes" + "flag" + "github.com/stretchr/testify/assert" + "io" + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "testing" +) + +var update = flag.Bool("update", false, "update test files with results") + +func TestCLI_New(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + requestBody, _ := ioutil.ReadAll(req.Body) + assert.Equal(t, `{"content":"测试内容"}`, string(requestBody[:])) + rw.Write([]byte(`{"code":0,"message":"记录成功"}`)) + })) + + defer server.Close() + + if err := exec.Command("go", "run", ".", "new", "--api", server.URL, "测试内容").Run(); err != nil { + log.Fatal(err) + } +} + +func TestCLI_New_WithTag(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + requestBody, _ := ioutil.ReadAll(req.Body) + assert.Equal(t, `{"content":"测试内容\n\n#test"}`, string(requestBody[:])) + rw.Write([]byte(`{"code":0,"message":"记录成功"}`)) + })) + + defer server.Close() + + if err := exec.Command("go", "run", ".", "new", "--api", server.URL, "--tag", "test", "测试内容").Run(); err != nil { + log.Fatal(err) + } +} + +func TestCLI_New_Pipe(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + requestBody, _ := ioutil.ReadAll(req.Body) + assert.Equal(t, `{"content":"测试内容"}`, string(requestBody[:])) + rw.Write([]byte(`{"code":0,"message":"记录成功"}`)) + })) + + defer server.Close() + + command1 := exec.Command("echo", "测试内容") + command2 := exec.Command("go", "run", ".", "new", "--api", server.URL) + + r, w := io.Pipe() + command1.Stdout = w + command2.Stdin = r + + var command2Buffer bytes.Buffer + command2.Stdout = &command2Buffer + + logFatal(command1.Start()) + logFatal(command2.Start()) + logFatal(command1.Wait()) + logFatal(w.Close()) + logFatal(command2.Wait()) + logFatal(func() error { + _, err := io.Copy(os.Stdout, &command2Buffer) + return err + }()) +} + +func logFatal(err error) { + if err != nil { + log.Fatal(err) + } +}