Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Add WithRace Context option
Browse files Browse the repository at this point in the history
Updates #96

Adds option to configure the context for a race build. This enables the
`race` build tag, and sets `Context.race`.
  • Loading branch information
davecheney committed Dec 15, 2015
1 parent 2cf83dc commit ae1a712
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
15 changes: 11 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Context struct {
Force bool // force rebuild of packages
SkipInstall bool // do not cache compiled packages
Verbose bool // verbose output
race bool // race detector requested

pkgs map[string]*Package // map of package paths to resolved packages

Expand All @@ -49,7 +50,7 @@ type Context struct {
func GOOS(goos string) func(*Context) error {
return func(c *Context) error {
if goos == "" {
return fmt.Errorf("goos cannot be blank")
return fmt.Errorf("GOOS cannot be blank")
}
c.gotargetos = goos
return nil
Expand All @@ -60,7 +61,7 @@ func GOOS(goos string) func(*Context) error {
func GOARCH(goarch string) func(*Context) error {
return func(c *Context) error {
if goarch == "" {
return fmt.Errorf("goarch cannot be blank")
return fmt.Errorf("GOARCH cannot be blank")
}
c.gotargetarch = goarch
return nil
Expand All @@ -70,12 +71,18 @@ func GOARCH(goarch string) func(*Context) error {
// Tags configured the context to use these additional build tags
func Tags(tags ...string) func(*Context) error {
return func(c *Context) error {
c.buildtags = make([]string, len(tags))
copy(c.buildtags, tags)
c.buildtags = append(c.buildtags, tags...)
return nil
}
}

// WithRace enables the race detector and adds the tag "race" to
// the Context build tags.
func WithRace(c *Context) error {
c.race = true
return Tags("race")(c)
}

// NewContext returns a new build context from this project.
// By default this context will use the gc toolchain with the
// host's GOOS and GOARCH values.
Expand Down
85 changes: 85 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gb

import (
"fmt"
"reflect"
"runtime"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -63,3 +65,86 @@ func TestContextCtxString(t *testing.T) {
}
}
}

func TestContextOptions(t *testing.T) {
matches := func(want Context) func(t *testing.T, got *Context) {
return func(t *testing.T, got *Context) {
if !reflect.DeepEqual(got, &want) {
t.Errorf("got %#v, want %#v", got, &want)
}
}
}

tests := []struct {
ctx Context
fn func(*Context) error
err error
expect func(*testing.T, *Context)
}{{
// assert that an zero context is not altered by the test rig.
fn: func(*Context) error { return nil },
expect: matches(Context{}),
}, {
// test blank GOOS is an error
fn: GOOS(""),
err: fmt.Errorf("GOOS cannot be blank"),
}, {
// test blank GOARCH is an error
fn: GOARCH(""),
err: fmt.Errorf("GOARCH cannot be blank"),
}, {
ctx: Context{
gotargetos: "bar",
gotargetarch: "baz",
},
fn: GOOS("foo"),
expect: matches(Context{
gotargetos: "foo",
gotargetarch: "baz",
}),
}, {
ctx: Context{
gotargetos: "bar",
gotargetarch: "baz",
},
fn: GOARCH("foo"),
expect: matches(Context{
gotargetos: "bar",
gotargetarch: "foo",
}),
}, {
fn: Tags(),
expect: matches(Context{}),
}, {
fn: Tags("foo"),
expect: matches(Context{buildtags: []string{"foo"}}),
}, {
ctx: Context{buildtags: []string{"foo"}},
fn: Tags("bar"),
expect: matches(Context{buildtags: []string{"foo", "bar"}}),
}, {
fn: WithRace,
expect: matches(Context{
buildtags: []string{"race"},
race: true,
}),
}, {
ctx: Context{buildtags: []string{"zzz"}},
fn: WithRace,
expect: matches(Context{
buildtags: []string{"zzz", "race"},
race: true,
}),
}}

for i, tt := range tests {
ctx := tt.ctx
err := tt.fn(&ctx)
switch {
case !reflect.DeepEqual(err, tt.err):
t.Errorf("test %d: expected err: %v, got %v", i+1, tt.err, err)
case err == nil:
tt.expect(t, &ctx)
}
}
}

0 comments on commit ae1a712

Please sign in to comment.