From efc5003cdd1bc20406d0348df59b66ceb914e4f2 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Fri, 15 May 2015 09:23:58 -0500 Subject: [PATCH] Compile fixtures on demand, only once --- proctl/proctl_test.go | 2 +- proctl/test/support.go | 62 +++++++++++--------------------- service/rest/integration_test.go | 2 +- 3 files changed, 23 insertions(+), 43 deletions(-) diff --git a/proctl/proctl_test.go b/proctl/proctl_test.go index f7423841..02f76700 100644 --- a/proctl/proctl_test.go +++ b/proctl/proctl_test.go @@ -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) diff --git a/proctl/test/support.go b/proctl/test/support.go index 17de7ae8..b179bc09 100644 --- a/proctl/test/support.go +++ b/proctl/test/support.go @@ -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) } diff --git a/service/rest/integration_test.go b/service/rest/integration_test.go index 9547d153..9026dced 100644 --- a/service/rest/integration_test.go +++ b/service/rest/integration_test.go @@ -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())