Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
CatchZeng committed Jan 21, 2022
1 parent 08607fa commit 21fbf61
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL := /bin/bash
BASEDIR = $(shell pwd)

APP_NAME=dingtalk
APP_VERSION=1.3.2
APP_VERSION=1.3.3
IMAGE_NAME="catchzeng/${APP_NAME}:${APP_VERSION}"
IMAGE_LATEST="catchzeng/${APP_NAME}:latest"

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ docker pull catchzeng/dingtalk

```sh
# Go 1.16+
go install github.com/CatchZeng/dingtalk@v1.3.2
go install github.com/CatchZeng/dingtalk@v1.3.3

# Go version < 1.16
go get -u github.com/CatchZeng/dingtalk@v1.3.2
go get -u github.com/CatchZeng/dingtalk@v1.3.3
```

## 使用方法
Expand Down
4 changes: 2 additions & 2 deletions READMEEN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ Go to [releases](https://github.com/CatchZeng/dingtalk/releases/) to download th

```sh
# Go 1.16+
go install github.com/CatchZeng/dingtalk@v1.3.2
go install github.com/CatchZeng/dingtalk@v1.3.3

# Go version < 1.16
go get -u github.com/CatchZeng/dingtalk@v1.3.2
go get -u github.com/CatchZeng/dingtalk@v1.3.3
```

## Usage
Expand Down
6 changes: 6 additions & 0 deletions cmd/dingtalk/actionCard.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,32 @@ var actionCardCmd = &cobra.Command{
func runActionCardCmd(_ *cobra.Command, args []string) {
if len(actionCardVars.Title) < 1 {
log.Fatal("title can not be empty")
return
}

if len(actionCardVars.Text) < 1 {
log.Fatal("text can not be empty")
return
}

var isOverallJump = false
if len(actionCardVars.SingleTitle) < 1 {
if len(btnTitles) < 1 {
log.Fatal("btns can not be empty when singleTitle is empty")
return
}
} else {
isOverallJump = true
if len(actionCardVars.SingleURL) < 1 {
log.Fatal("singleURL can not be empty")
return
}
}

client, err := newClient()
if err != nil {
log.Fatal(err.Error())
return
}

msg := dingtalk.NewActionCardMessage()
Expand All @@ -53,6 +58,7 @@ func runActionCardCmd(_ *cobra.Command, args []string) {
} else {
if len(btnTitles) != len(btnActionURLs) {
log.Fatal("btnTitles & btnActionURLs count must be equal")
return
}

for i := 0; i < len(btnTitles); i++ {
Expand Down
5 changes: 5 additions & 0 deletions cmd/dingtalk/actionCard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
)

func Test_runActionCardCmd(t *testing.T) {
fakeExit := func(int) {
log.Print("fake exit")
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()

t.Run("title is empty", func(t *testing.T) {
var buf bytes.Buffer
Expand Down
3 changes: 3 additions & 0 deletions cmd/dingtalk/feedCard.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ var feedCardCmd = &cobra.Command{
func runFeedCardCmd(_ *cobra.Command, args []string) {
if len(feedCardVars.titles) < 1 || len(feedCardVars.picURLs) < 1 || len(feedCardVars.messageURLs) < 1 {
log.Fatal("titles & picURLs & messageURLs can not be empty")
return
}

if len(feedCardVars.titles) == len(feedCardVars.picURLs) && len(feedCardVars.picURLs) == len(feedCardVars.messageURLs) {
client, err := newClient()
if err != nil {
log.Fatal(err.Error())
return
}

msg := dingtalk.NewFeedCardMessage()
Expand All @@ -36,6 +38,7 @@ func runFeedCardCmd(_ *cobra.Command, args []string) {
}
if err != nil {
log.Fatal(err.Error())
return
}
} else {
log.Fatal("titles & picURLs & messageURLs count must be equal")
Expand Down
6 changes: 6 additions & 0 deletions cmd/dingtalk/feedCard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
)

func Test_runFeedCardCmd(t *testing.T) {
fakeExit := func(int) {
log.Print("fake exit")
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()

const emptyMsg = "titles & picURLs & messageURLs can not be empty"
const differentCountMsg = "titles & picURLs & messageURLs count must be equal"

Expand Down
4 changes: 4 additions & 0 deletions cmd/dingtalk/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ var linkCmd = &cobra.Command{
func runLinkCmd(_ *cobra.Command, args []string) {
if len(linkVars.title) < 1 {
log.Fatal("title can not be empty")
return
}

if len(linkVars.text) < 1 {
log.Fatal("text can not be empty")
return
}

if len(linkVars.messageURL) < 1 {
log.Fatal("messageURL can not be empty")
return
}

client, err := newClient()
if err != nil {
log.Fatal(err.Error())
return
}

msg := dingtalk.NewLinkMessage().
Expand Down
6 changes: 6 additions & 0 deletions cmd/dingtalk/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
)

func Test_runLinkCmd(t *testing.T) {
fakeExit := func(int) {
log.Print("fake exit")
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()

t.Run("title is empty", func(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
Expand Down
3 changes: 3 additions & 0 deletions cmd/dingtalk/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ var markdownCmd = &cobra.Command{
func runMarkdownCmd(_ *cobra.Command, args []string) {
if len(markdownVars.title) < 1 {
log.Fatal("title can not be empty")
return
}

if len(markdownVars.text) < 1 {
log.Fatal("text can not be empty")
return
}

client, err := newClient()
if err != nil {
log.Fatal(err.Error())
return
}

msg := dingtalk.NewMarkdownMessage().
Expand Down
6 changes: 6 additions & 0 deletions cmd/dingtalk/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
)

func Test_runMarkdownCmd(t *testing.T) {
fakeExit := func(int) {
log.Print("fake exit")
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()

t.Run("title is empty", func(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
Expand Down
8 changes: 8 additions & 0 deletions cmd/dingtalk/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package dingtalk

import (
"errors"
"log"
"os"
"testing"

"bou.ke/monkey"
"github.com/CatchZeng/dingtalk/configs"
)

func Test_newClient(t *testing.T) {
fakeExit := func(int) {
log.Print("fake exit")
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()

t.Run("getAccessToken return empty", func(t *testing.T) {
accessToken = ""
_, err := newClient()
Expand Down
2 changes: 2 additions & 0 deletions cmd/dingtalk/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ var textCmd = &cobra.Command{
func runTextCmd(_ *cobra.Command, _ []string) {
if len(textVars.content) < 1 {
log.Fatal("content can not be empty")
return
}

client, err := newClient()
if err != nil {
log.Fatal(err.Error())
return
}

msg := dingtalk.NewTextMessage().
Expand Down
5 changes: 5 additions & 0 deletions cmd/dingtalk/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
)

func Test_runTextCmd(t *testing.T) {
fakeExit := func(int) {
log.Print("fake exit")
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()

t.Run("content is empty", func(t *testing.T) {
var buf bytes.Buffer
Expand Down
2 changes: 1 addition & 1 deletion cmd/dingtalk/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
version = "1.3.2"
version = "1.3.3"
buildTime = "2022/01/21"
)

Expand Down

0 comments on commit 21fbf61

Please sign in to comment.