Skip to content

Commit

Permalink
Add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Sep 3, 2017
1 parent 9a6869e commit 5471931
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tool/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func setupConfig(conf *config.Config) (err error) {
if err = setupVars(conf); err != nil {
return err
}
return setupTool(conf)
setupTool(conf)
return nil
}

// setupVars 若定义了变量,则将变量求值后将值重置到Variable的Value属性中。
Expand All @@ -81,7 +82,7 @@ func setupVars(conf *config.Config) (err error) {

// setupTool 若定义了变量,则将变量作为ldflags选项的值追加到tool内容中。
// 变量求值应该在调用本函数前完成。
func setupTool(conf *config.Config) (err error) {
func setupTool(conf *config.Config) {
var buf bytes.Buffer
for i := range conf.Variables {
buf.WriteString(fmt.Sprintf(`-X "%s.%s=%s"`, conf.Importpath, conf.Variables[i].Variable, conf.Variables[i].Value))
Expand All @@ -91,12 +92,12 @@ func setupTool(conf *config.Config) (err error) {
}
ldflags := buf.String()
if ldflags == "" {
return nil
return
}

if !strings.Contains(conf.Tool, ldflagsOPT) {
conf.Tool = fmt.Sprintf("%s %s '%s'", conf.Tool, ldflagsOPT, ldflags) // 直接增加-ldflags选项及其值
return nil
return
}

cmdArgs := shellwords.Split(strings.Replace(conf.Tool, "=", " ", -1))
Expand All @@ -115,5 +116,5 @@ func setupTool(conf *config.Config) (err error) {
}

conf.Tool = strings.Join(cmdArgs, " ")
return nil
return
}
113 changes: 113 additions & 0 deletions tool/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tool

import (
"bytes"
"errors"
"fmt"
"os"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/bouk/monkey"
. "github.com/smartystreets/goconvey/convey"
"github.com/voidint/gbb/config"
"github.com/voidint/gbb/variable"
)

func initDir() {
Expand Down Expand Up @@ -137,3 +139,114 @@ func TestChdir(t *testing.T) {
})
})
}

func Test_setupVars(t *testing.T) {
const (
dateName = "Date"
dateVal = "2017-09-03T16:58:19+08:00"
commitName = "GitCommit"
commitVal = "9a6869e1591752d29973535b48a5ecfe7471eb49"
)

Convey("设置config指针中的变量值", t, func() {
Convey("未定义变量,无需对变量求值", func() {
So(setupVars(&config.Config{}), ShouldBeNil)
})

Convey("变量求值成功", func() {
monkey.Patch(variable.Eval, func(expr string, debug bool) (val string, err error) {
switch expr {
case variable.DefaultDateExpr:
return dateVal, nil
case variable.DefaultGitCommitExpr:
return commitVal, nil
}
panic("unreachable")
})
defer monkey.Unpatch(variable.Eval)
conf := config.Config{
Variables: []config.Variable{
{Variable: dateName, Value: variable.DefaultDateExpr},
{Variable: commitName, Value: variable.DefaultGitCommitExpr},
},
}
So(setupVars(&conf), ShouldBeNil)
So(conf.Variables[0].Value, ShouldEqual, dateVal)
So(conf.Variables[1].Value, ShouldEqual, commitVal)
})

Convey("变量求值发生错误", func() {
ErrEval := errors.New("eval error")
monkey.Patch(variable.Eval, func(expr string, debug bool) (val string, err error) {
return "", ErrEval
})
defer monkey.Unpatch(variable.Eval)

conf := config.Config{
Variables: []config.Variable{
{Variable: dateName, Value: variable.DefaultDateExpr},
{Variable: commitName, Value: variable.DefaultGitCommitExpr},
},
}
So(setupVars(&conf), ShouldEqual, ErrEval)
})
})
}

func Test_setupTool(t *testing.T) {
const (
dateName = "Date"
dateVal = "2017-09-03T16:58:19+08:00"
commitName = "GitCommit"
commitVal = "9a6869e1591752d29973535b48a5ecfe7471eb49"
)
Convey("设置config指针中的tool值", t, func() {
// Convey("未定义变量", func() {
// setupTool(&config.Config{})
// })

Convey("未设置-ldflags选项", func() {
conf := config.Config{
Tool: "go build",
Importpath: "github.com/voidint/gbb/build",
Variables: []config.Variable{
{Variable: dateName, Value: dateVal},
{Variable: commitName, Value: commitVal},
},
}

var buf bytes.Buffer
for i := range conf.Variables {
buf.WriteString(fmt.Sprintf(`-X "%s.%s=%s"`, conf.Importpath, conf.Variables[i].Variable, conf.Variables[i].Value))
if i < len(conf.Variables)-1 {
buf.WriteByte(' ')
}
}

setupTool(&conf)
So(conf.Tool, ShouldEqual, fmt.Sprintf("go build -ldflags '%s'", buf.String()))
})

Convey("已设置-ldflags选项", func() {
conf := config.Config{
Tool: "go build -ldflags='-s -w'",
Importpath: "github.com/voidint/gbb/build",
Variables: []config.Variable{
{Variable: dateName, Value: dateVal},
{Variable: commitName, Value: commitVal},
},
}

var buf bytes.Buffer
for i := range conf.Variables {
buf.WriteString(fmt.Sprintf(`-X "%s.%s=%s"`, conf.Importpath, conf.Variables[i].Variable, conf.Variables[i].Value))
if i < len(conf.Variables)-1 {
buf.WriteByte(' ')
}
}

setupTool(&conf)
So(conf.Tool, ShouldEqual, fmt.Sprintf("go build -ldflags '-s -w %s'", buf.String()))
})
})
}
1 change: 1 addition & 0 deletions util/scan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package util

0 comments on commit 5471931

Please sign in to comment.