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)) {
|
func withTestProcess(name string, t *testing.T, fn func(p *DebuggedProcess, fixture protest.Fixture)) {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
|
|
||||||
fixture := protest.Fixtures[name]
|
fixture := protest.BuildFixture(name)
|
||||||
p, err := Launch([]string{fixture.Path})
|
p, err := Launch([]string{fixture.Path})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Launch():", err)
|
t.Fatal("Launch():", err)
|
||||||
|
@ -4,11 +4,9 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,12 +23,10 @@ type Fixture struct {
|
|||||||
// Fixtures is a map of Fixture.Name to Fixture.
|
// Fixtures is a map of Fixture.Name to Fixture.
|
||||||
var Fixtures map[string]Fixture = make(map[string]Fixture)
|
var Fixtures map[string]Fixture = make(map[string]Fixture)
|
||||||
|
|
||||||
// RunTestsWithFixtures will pre-compile test fixtures before running test
|
func BuildFixture(name string) Fixture {
|
||||||
// methods. Test binaries are deleted before exiting.
|
if f, ok := Fixtures[name]; ok {
|
||||||
func RunTestsWithFixtures(m *testing.M) {
|
return f
|
||||||
// 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).
|
|
||||||
parent := ".."
|
parent := ".."
|
||||||
fixturesDir := filepath.Join(parent, "_fixtures")
|
fixturesDir := filepath.Join(parent, "_fixtures")
|
||||||
for depth := 0; depth < 10; depth++ {
|
for depth := 0; depth < 10; depth++ {
|
||||||
@ -39,28 +35,11 @@ func RunTestsWithFixtures(m *testing.M) {
|
|||||||
}
|
}
|
||||||
fixturesDir = filepath.Join(parent, fixturesDir)
|
fixturesDir = filepath.Join(parent, fixturesDir)
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(fixturesDir); err != nil {
|
|
||||||
fmt.Println("Couldn't locate fixtures directory")
|
|
||||||
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
|
// Make a (good enough) random temporary file name
|
||||||
r := make([]byte, 4)
|
r := make([]byte, 4)
|
||||||
rand.Read(r)
|
rand.Read(r)
|
||||||
path := filepath.Join(fixturesDir, src.Name())
|
path := filepath.Join(fixturesDir, name+".go")
|
||||||
name := strings.TrimSuffix(src.Name(), filepath.Ext(src.Name()))
|
|
||||||
tmpfile := filepath.Join(os.TempDir(), fmt.Sprintf("%s.%s", name, hex.EncodeToString(r)))
|
tmpfile := filepath.Join(os.TempDir(), fmt.Sprintf("%s.%s", name, hex.EncodeToString(r)))
|
||||||
|
|
||||||
// Build the test binary
|
// Build the test binary
|
||||||
@ -68,17 +47,18 @@ func RunTestsWithFixtures(m *testing.M) {
|
|||||||
fmt.Printf("Error compiling %s: %s\n", path, err)
|
fmt.Printf("Error compiling %s: %s\n", path, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("Compiled test binary %s: %s\n", name, tmpfile)
|
|
||||||
|
|
||||||
source, _ := filepath.Abs(path)
|
source, _ := filepath.Abs(path)
|
||||||
Fixtures[name] = Fixture{Name: name, Path: tmpfile, Source: source}
|
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()
|
status := m.Run()
|
||||||
|
|
||||||
// Remove the fixtures.
|
// Remove the fixtures.
|
||||||
// TODO(danmace): Not sure why yet, but doing these removes in a defer isn't
|
|
||||||
// working.
|
|
||||||
for _, f := range Fixtures {
|
for _, f := range Fixtures {
|
||||||
os.Remove(f.Path)
|
os.Remove(f.Path)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func withTestClient(name string, t *testing.T, fn func(c service.Client)) {
|
|||||||
}
|
}
|
||||||
server := NewServer(&Config{
|
server := NewServer(&Config{
|
||||||
Listener: listener,
|
Listener: listener,
|
||||||
ProcessArgs: []string{protest.Fixtures[name].Path},
|
ProcessArgs: []string{protest.BuildFixture(name).Path},
|
||||||
}, false)
|
}, false)
|
||||||
go server.Run()
|
go server.Run()
|
||||||
client := NewClient(listener.Addr().String())
|
client := NewClient(listener.Addr().String())
|
||||||
|
Loading…
Reference in New Issue
Block a user