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

gb/test: scale test improvements #437

Merged
merged 2 commits into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/gb/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"sort"

"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
Expand Down Expand Up @@ -45,6 +46,13 @@ See 'go help test'.
ctx.Force = F
ctx.SkipInstall = FF
r := test.TestResolver(ctx)

// gb build builds packages in dependency order, however
// gb test tests packages in alpha order. This matches the
// expected behaviour from go test; tests are executed in
// stable order.
sort.Strings(args)

pkgs, err := gb.ResolvePackages(r, args...)
if err != nil {
return err
Expand Down
48 changes: 39 additions & 9 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ func TestPackage(targets map[string]*gb.Action, pkg *gb.Package, flags []string)
testpkg.Scope = "test"
testpkg.Stale = true

// build dependencies
deps, err := gb.BuildDependencies(targets, testpkg)
if err != nil {
return nil, err
}

// only build the internal test if there is Go source or
// internal test files.
var testobj *gb.Action
if len(testpkg.GoFiles)+len(testpkg.CgoFiles)+len(testpkg.TestGoFiles) > 0 {
var err error

// build internal testpkg dependencies
deps, err := gb.BuildDependencies(targets, testpkg)
if err != nil {
return nil, err
}

testobj, err = gb.Compile(testpkg, deps...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -181,11 +181,41 @@ func TestPackage(targets map[string]*gb.Action, pkg *gb.Package, flags []string)
return err
}
}

// test binaries can be very large, so always unlink the
// binary after the test has run to free up temporary space
// technically this is done by ctx.Destroy(), but freeing
// the space earlier is important for projects with many
// packages
withcleanup := func(fn func() error) func() error {
return func() error {
file := testmainpkg.Binfile()
err := fn()
os.Remove(file)
return err
}
}
fn := withcleanup(cmd.Run)
fn = logInfoFn(fn, pkg.ImportPath)
// When used with the concurrent executor, building deps and
// linking the test binary can cause a lot of disk space to be
// pinned as linking will tend to occur more frequenty than retiring
// tests.
//
// To solve this, we merge the testmain compile step (which includes
// linking) and the execute and cleanup steps so they are executed
// as one atomic operation.

return &gb.Action{
Name: fmt.Sprintf("run: %s", cmd.Args),
Deps: []*gb.Action{testmain},
Run: logInfoFn(cmd.Run, pkg.ImportPath),
Deps: testmain.Deps,
Run: func() error {
err := testmain.Run()
if err != nil {
return err
}
return fn()
},
}, nil
}

Expand Down