From 6fb40b06dae29df16e50b79214655133346ed274 Mon Sep 17 00:00:00 2001 From: voidint Date: Sun, 15 Jan 2017 22:15:51 +0800 Subject: [PATCH] Add unit test cases for tool package --- tool/builder_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++++ tool/golang_test.go | 46 +++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 tool/builder_test.go create mode 100644 tool/golang_test.go diff --git a/tool/builder_test.go b/tool/builder_test.go new file mode 100644 index 0000000..277cbf7 --- /dev/null +++ b/tool/builder_test.go @@ -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) + }) +} diff --git a/tool/golang_test.go b/tool/golang_test.go new file mode 100644 index 0000000..96179ad --- /dev/null +++ b/tool/golang_test.go @@ -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")) + }) +}