-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test cases for tool package
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
} |