Skip to content

Commit

Permalink
Add unit test cases for tool package
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Jan 15, 2017
1 parent 6413e59 commit 6fb40b0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tool/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package tool

import (
"os/exec"
"reflect"
"testing"

"github.com/bouk/monkey"
. "github.com/smartystreets/goconvey/convey"
"github.com/voidint/gbb/config"
)

func TestBuild(t *testing.T) {
var cmd *exec.Cmd
monkey.PatchInstanceMethod(reflect.TypeOf(cmd), "Run", func(c *exec.Cmd) error {
return nil
})
defer monkey.UnpatchInstanceMethod(reflect.TypeOf(cmd), "Run")

Convey("调用gb build编译", t, func() {
Convey("包含变量表达式", func() {
c := &config.Config{
Version: "0.3.0",
Tool: "gb build",
Package: "github.com/voidint/gbb/build",
}

Convey("包含非法变量表达式", func() {
c.Variables = []config.Variable{
{Variable: "Date", Value: "xxxx"},
}
err := Build(c, true, "./")
So(err, ShouldNotBeNil)
})

Convey("包含合法变量表达式", func() {
c.Variables = []config.Variable{
{Variable: "Date", Value: "{{.Date}}"},
}
err := Build(c, true, "./")
So(err, ShouldBeNil)
})

})

Convey("不包含变量表达式", func() {
c := &config.Config{
Version: "0.3.0",
Tool: "gb build",
}

err := Build(c, true, "./")
So(err, ShouldBeNil)
})
})

Convey("调用go build编译", t, func() {
c := &config.Config{
Version: "0.3.0",
Tool: "go build",
Package: "github.com/voidint/gbb/build",
Variables: []config.Variable{
{Variable: "Date", Value: "{{.Date}}"},
},
}

err := Build(c, true, "./")
So(err, ShouldBeNil)

})

Convey("调用非法的编译工具编译", t, func() {
c := &config.Config{
Version: "0.3.0",
Tool: "unsupported tool",
}

err := Build(c, true, "./")
So(err, ShouldEqual, ErrBuildTool)
})
}
46 changes: 46 additions & 0 deletions tool/golang_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tool

import (
"testing"

"os"

"path/filepath"
"strings"

. "github.com/smartystreets/goconvey/convey"
)

func TestHasMain(t *testing.T) {
Convey("检测指定的go源代码文件中是否包含main函数", t, func() {
Convey("不包含main函数的go源代码文件", func() {
yes, err := hasMain("./golang.go")
So(err, ShouldBeNil)
So(yes, ShouldBeFalse)
})
Convey("包含main函数的go源代码文件", func() {
yes, err := hasMain("../main.go")
So(err, ShouldBeNil)
So(yes, ShouldBeTrue)
})
Convey("非go源代码文件", func() {
yes, err := hasMain("../gbb.json")
So(err, ShouldNotBeNil)
So(yes, ShouldBeFalse)
})
})
}

func TestWalkMainDir(t *testing.T) {
Convey("遍历根目录及其子目录查找包含main函数的源代码文件路径", t, func() {
dir, err := os.Getwd()
So(err, ShouldBeNil)
So(strings.HasSuffix(dir, filepath.Join("src", "github.com", "voidint", "gbb", "tool")), ShouldBeTrue)
workspace := strings.TrimRight(dir, "tool")
paths, err := walkMainDir(workspace)
So(err, ShouldBeNil)
So(paths, ShouldNotBeEmpty)
So(len(paths), ShouldEqual, 1)
So(paths[0], ShouldEqual, filepath.Join(workspace, "main.go"))
})
}

0 comments on commit 6fb40b0

Please sign in to comment.