Compile fixtures on demand, only once
This commit is contained in:
parent
dfff9b5384
commit
efc5003cdd
@ -17,7 +17,7 @@ func TestMain(m *testing.M) {
|
||||
func withTestProcess(name string, t *testing.T, fn func(p *DebuggedProcess, fixture protest.Fixture)) {
|
||||
runtime.LockOSThread()
|
||||
|
||||
fixture := protest.Fixtures[name]
|
||||
fixture := protest.BuildFixture(name)
|
||||
p, err := Launch([]string{fixture.Path})
|
||||
if err != nil {
|
||||
t.Fatal("Launch():", err)
|
||||
|
@ -4,11 +4,9 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -25,12 +23,10 @@ type Fixture struct {
|
||||
// Fixtures is a map of Fixture.Name to Fixture.
|
||||
var Fixtures map[string]Fixture = make(map[string]Fixture)
|
||||
|
||||
// RunTestsWithFixtures will pre-compile test fixtures before running test
|
||||
// methods. Test binaries are deleted before exiting.
|
||||
func RunTestsWithFixtures(m *testing.M) {
|
||||
// Find the fixtures directory; this is necessary because the test file's
|
||||
// nesting level can vary. Only look up to a maxdepth of 10 (which seems
|
||||
// like a sane alternative to recursion).
|
||||
func BuildFixture(name string) Fixture {
|
||||
if f, ok := Fixtures[name]; ok {
|
||||
return f
|
||||
}
|
||||
parent := ".."
|
||||
fixturesDir := filepath.Join(parent, "_fixtures")
|
||||
for depth := 0; depth < 10; depth++ {
|
||||
@ -39,46 +35,30 @@ func RunTestsWithFixtures(m *testing.M) {
|
||||
}
|
||||
fixturesDir = filepath.Join(parent, fixturesDir)
|
||||
}
|
||||
if _, err := os.Stat(fixturesDir); err != nil {
|
||||
fmt.Println("Couldn't locate fixtures directory")
|
||||
|
||||
// Make a (good enough) random temporary file name
|
||||
r := make([]byte, 4)
|
||||
rand.Read(r)
|
||||
path := filepath.Join(fixturesDir, name+".go")
|
||||
tmpfile := filepath.Join(os.TempDir(), fmt.Sprintf("%s.%s", name, hex.EncodeToString(r)))
|
||||
|
||||
// Build the test binary
|
||||
if err := exec.Command("go", "build", "-gcflags=-N -l", "-o", tmpfile, path).Run(); err != nil {
|
||||
fmt.Printf("Error compiling %s: %s\n", path, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Collect all files which look like fixture source files.
|
||||
sources, err := ioutil.ReadDir(fixturesDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Couldn't read fixtures dir: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Compile the fixtures.
|
||||
for _, src := range sources {
|
||||
if src.IsDir() {
|
||||
continue
|
||||
}
|
||||
// Make a (good enough) random temporary file name
|
||||
r := make([]byte, 4)
|
||||
rand.Read(r)
|
||||
path := filepath.Join(fixturesDir, src.Name())
|
||||
name := strings.TrimSuffix(src.Name(), filepath.Ext(src.Name()))
|
||||
tmpfile := filepath.Join(os.TempDir(), fmt.Sprintf("%s.%s", name, hex.EncodeToString(r)))
|
||||
|
||||
// Build the test binary
|
||||
if err := exec.Command("go", "build", "-gcflags=-N -l", "-o", tmpfile, path).Run(); err != nil {
|
||||
fmt.Printf("Error compiling %s: %s\n", path, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Compiled test binary %s: %s\n", name, tmpfile)
|
||||
|
||||
source, _ := filepath.Abs(path)
|
||||
Fixtures[name] = Fixture{Name: name, Path: tmpfile, Source: source}
|
||||
}
|
||||
source, _ := filepath.Abs(path)
|
||||
Fixtures[name] = Fixture{Name: name, Path: tmpfile, Source: source}
|
||||
return Fixtures[name]
|
||||
}
|
||||
|
||||
// RunTestsWithFixtures will pre-compile test fixtures before running test
|
||||
// methods. Test binaries are deleted before exiting.
|
||||
func RunTestsWithFixtures(m *testing.M) {
|
||||
status := m.Run()
|
||||
|
||||
// Remove the fixtures.
|
||||
// TODO(danmace): Not sure why yet, but doing these removes in a defer isn't
|
||||
// working.
|
||||
for _, f := range Fixtures {
|
||||
os.Remove(f.Path)
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ func withTestClient(name string, t *testing.T, fn func(c service.Client)) {
|
||||
}
|
||||
server := NewServer(&Config{
|
||||
Listener: listener,
|
||||
ProcessArgs: []string{protest.Fixtures[name].Path},
|
||||
ProcessArgs: []string{protest.BuildFixture(name).Path},
|
||||
}, false)
|
||||
go server.Run()
|
||||
client := NewClient(listener.Addr().String())
|
||||
|
Loading…
Reference in New Issue
Block a user