Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Dec 25, 2020
1 parent cb8b5ba commit c5222db
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 11 deletions.
20 changes: 13 additions & 7 deletions commandnew.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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{
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions commandnew_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion commandversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions commandversion_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
)

func main() {
os.Exit(run())
}

func run() int {
var options struct{}
var parser = flags.NewParser(&options, flags.Default)

Expand All @@ -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
}
81 changes: 81 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c5222db

Please sign in to comment.