Permalink
Browse files

compile test package *then* run tests

  • Loading branch information...
1 parent dc83f67 commit 11a64fec6d8591bd33ac7a4b9d9f63366e5b02e0 Onsi Fakhouri committed Feb 9, 2014
Showing with 53 additions and 48 deletions.
  1. +5 −1 ginkgo/main.go
  2. +48 −47 ginkgo/test_runner.go
View
6 ginkgo/main.go
@@ -106,7 +106,7 @@ func init() {
flag.IntVar(&(numCPU), "nodes", 1, "The number of parallel test nodes to run")
flag.BoolVar(&(parallelStream), "stream", !onWindows, "Aggregate parallel test output into one coherent stream (default: true)")
flag.BoolVar(&(recurse), "r", false, "Find and run test suites under the current directory recursively")
- flag.BoolVar(&(runMagicI), "i", false, "Run go test -i first, then run the test suite")
+ flag.BoolVar(&(runMagicI), "i", false, "[DEPRECATED] Run go test -i first, then run the test suite")
flag.BoolVar(&(race), "race", false, "Run tests with race detection enabled")
flag.BoolVar(&(cover), "cover", false, "Run tests with coverage analysis, will generate coverage profiles with the package name in the current directory")
flag.BoolVar(&(watch), "watch", false, "Monitor the target packages for changes, then run tests when changes are detected")
@@ -134,6 +134,10 @@ func init() {
}
func main() {
+ if runMagicI {
+ fmt.Printf("[DEPRECATION]\n -i is deprecated.\n Ginkgo now always runs with -i -- the -i flag will go away soon, so stop using it!\n")
+ }
+
if flag.NArg() > 0 {
args := flag.Args()
handled := handleSubcommands(args)
View
95 ginkgo/test_runner.go
@@ -11,17 +11,18 @@ import (
"io"
"os"
"os/exec"
+ "path/filepath"
"sync"
"time"
)
type testRunner struct {
numCPU int
parallelStream bool
- runMagicI bool
race bool
cover bool
executedCommands []*exec.Cmd
+ compiledArtifact string
reports []*bytes.Buffer
lock *sync.Mutex
@@ -31,7 +32,6 @@ func newTestRunner(numCPU int, parallelStream bool, runMagicI bool, race bool, c
return &testRunner{
numCPU: numCPU,
parallelStream: parallelStream,
- runMagicI: runMagicI,
race: race,
cover: cover,
executedCommands: []*exec.Cmd{},
@@ -41,14 +41,12 @@ func newTestRunner(numCPU int, parallelStream bool, runMagicI bool, race bool, c
}
func (t *testRunner) runSuite(suite *testsuite.TestSuite) bool {
- if t.runMagicI {
- err := t.runGoI(suite)
- if err != nil {
- return false
- }
+ var success bool
+ success = t.compileSuite(suite)
+ if !success {
+ return success
}
- var success bool
if suite.IsGinkgo {
if t.numCPU > 1 {
if t.parallelStream {
@@ -63,22 +61,41 @@ func (t *testRunner) runSuite(suite *testsuite.TestSuite) bool {
success = t.runGoTestSuite(suite)
}
+ t.cleanUpCompiledSuite()
return success
}
-func (t *testRunner) runGoI(suite *testsuite.TestSuite) error {
- args := []string{"test", "-i"}
+func (t *testRunner) compileSuite(suite *testsuite.TestSuite) bool {
+ args := []string{"test", "-c", "-i"}
if t.race {
args = append(args, "-race")
}
- args = append(args, suite.Path)
+ if t.cover {
+ args = append(args, "-cover", "-covermode=atomic")
+ }
cmd := exec.Command("go", args...)
+ cmd.Dir = suite.Path
output, err := cmd.CombinedOutput()
if err != nil {
- fmt.Printf("go test -i %s failed with:\n\n%s", suite.Path, output)
+ fmt.Printf("Failed to compile %s:\n\n%s\n", suite.Path, output)
+ t.compiledArtifact = ""
+ return false
}
+ t.compiledArtifact, _ = filepath.Abs(filepath.Join(suite.Path, fmt.Sprintf("%s.test", suite.PackageName)))
+ return true
+}
+
+func (t *testRunner) cleanUpCompiledSuite() {
+ os.Remove(t.compiledArtifact)
+}
- return err
+func (t *testRunner) runSerialGinkgoSuite(suite *testsuite.TestSuite) bool {
+ ginkgoArgs := config.BuildFlagArgs("ginkgo", config.GinkgoConfig, config.DefaultReporterConfig)
+ return t.runCompiledSuite(suite, ginkgoArgs, nil, os.Stdout, nil)
+}
+
+func (t *testRunner) runGoTestSuite(suite *testsuite.TestSuite) bool {
+ return t.runCompiledSuite(suite, []string{}, nil, os.Stdout, nil)
}
func (t *testRunner) runParallelGinkgoSuite(suite *testsuite.TestSuite) bool {
@@ -87,13 +104,12 @@ func (t *testRunner) runParallelGinkgoSuite(suite *testsuite.TestSuite) bool {
config.GinkgoConfig.ParallelNode = cpu + 1
config.GinkgoConfig.ParallelTotal = t.numCPU
- args := config.BuildFlagArgs("ginkgo", config.GinkgoConfig, config.DefaultReporterConfig)
- args = append(args, t.commonArgs(suite)...)
+ ginkgoArgs := config.BuildFlagArgs("ginkgo", config.GinkgoConfig, config.DefaultReporterConfig)
buffer := new(bytes.Buffer)
t.reports = append(t.reports, buffer)
- go t.runCommand(suite.Path, args, nil, buffer, completions)
+ go t.runCompiledSuite(suite, ginkgoArgs, nil, buffer, completions)
}
passed := true
@@ -122,25 +138,23 @@ func (t *testRunner) runAndStreamParallelGinkgoSuite(suite *testsuite.TestSuite)
server.RegisterReporters(aggregator)
server.Start()
-
+ defer server.Stop()
serverAddress := server.Address()
completions := make(chan bool)
-
for cpu := 0; cpu < t.numCPU; cpu++ {
config.GinkgoConfig.ParallelNode = cpu + 1
config.GinkgoConfig.ParallelTotal = t.numCPU
- args := config.BuildFlagArgs("ginkgo", config.GinkgoConfig, config.DefaultReporterConfig)
- args = append(args, t.commonArgs(suite)...)
+ ginkgoArgs := config.BuildFlagArgs("ginkgo", config.GinkgoConfig, config.DefaultReporterConfig)
env := os.Environ()
env = append(env, fmt.Sprintf("GINKGO_REMOTE_REPORTING_SERVER=%s", serverAddress))
buffer := new(bytes.Buffer)
t.reports = append(t.reports, buffer)
- go t.runCommand(suite.Path, args, env, buffer, completions)
+ go t.runCompiledSuite(suite, ginkgoArgs, env, buffer, completions)
}
for cpu := 0; cpu < t.numCPU; cpu++ {
@@ -178,39 +192,20 @@ func (t *testRunner) runAndStreamParallelGinkgoSuite(suite *testsuite.TestSuite)
os.Stdout.Sync()
}
- server.Stop()
-
return passed
}
-func (t *testRunner) runSerialGinkgoSuite(suite *testsuite.TestSuite) bool {
- args := config.BuildFlagArgs("ginkgo", config.GinkgoConfig, config.DefaultReporterConfig)
- args = append(args, t.commonArgs(suite)...)
- return t.runCommand(suite.Path, args, nil, os.Stdout, nil)
-}
-
-func (t *testRunner) runGoTestSuite(suite *testsuite.TestSuite) bool {
- args := t.commonArgs(suite)
- return t.runCommand(suite.Path, args, nil, os.Stdout, nil)
-}
-
-func (t *testRunner) commonArgs(suite *testsuite.TestSuite) []string {
- args := []string{}
- if t.race {
- args = append(args, "--race")
- }
+func (t *testRunner) runCompiledSuite(suite *testsuite.TestSuite, ginkgoArgs []string, env []string, stream io.Writer, completions chan bool) bool {
+ args := []string{"-test.v", "-test.timeout=24h"}
if t.cover {
- args = append([]string{"--cover", "--coverprofile=" + suite.PackageName + ".coverprofile"})
+ args = append(args, "--test.coverprofile="+suite.PackageName+".coverprofile")
}
- return args
-}
-
-func (t *testRunner) runCommand(path string, args []string, env []string, stream io.Writer, completions chan bool) bool {
- args = append([]string{"test", "-v", "-timeout=24h", path}, args...)
+ args = append(args, ginkgoArgs...)
- cmd := exec.Command("go", args...)
+ cmd := exec.Command(t.compiledArtifact, args...)
cmd.Env = env
+ cmd.Dir = suite.Path
t.lock.Lock()
t.executedCommands = append(t.executedCommands, cmd)
@@ -229,7 +224,11 @@ func (t *testRunner) runCommand(path string, args []string, env []string, stream
err := cmd.Start()
if err != nil {
- os.Exit(1)
+ fmt.Printf("Failed to run test suite!\n\t%s", err.Error())
+ if completions != nil {
+ completions <- false
+ }
+ return false
}
<-doneStreaming
@@ -251,6 +250,7 @@ func (t *testRunner) runCommand(path string, args []string, env []string, stream
if completions != nil {
completions <- (err == nil)
}
+
return err == nil
}
@@ -263,4 +263,5 @@ func (t *testRunner) abort(sig os.Signal) {
}
}
t.lock.Unlock()
+ t.cleanUpCompiledSuite()
}

0 comments on commit 11a64fe

Please sign in to comment.