From 3ffbe2d7b75547cb305791c3cc9d1757beeb3547 Mon Sep 17 00:00:00 2001 From: Dan Mace Date: Mon, 27 Apr 2015 11:51:02 -0400 Subject: [PATCH] Precompile fixtures --- service/rest/integration_test.go | 86 +++++++++++++++++++------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/service/rest/integration_test.go b/service/rest/integration_test.go index f950b316..e11164c4 100644 --- a/service/rest/integration_test.go +++ b/service/rest/integration_test.go @@ -3,36 +3,59 @@ package rest import ( "crypto/rand" "encoding/hex" + "fmt" + "io/ioutil" "net" "os" "os/exec" "path/filepath" + "strings" "testing" "github.com/derekparker/delve/service" "github.com/derekparker/delve/service/api" ) -const ( - continuetestprog = "../../_fixtures/continuetestprog" - testprog = "../../_fixtures/testprog" - testnextprog = "../../_fixtures/testnextprog" - testthreads = "../../_fixtures/testthreads" -) +var fixtures map[string]string = make(map[string]string) -func withTestClient(name string, t *testing.T, fn func(c service.Client)) { - // Make a (good enough) random temporary file name - r := make([]byte, 4) - rand.Read(r) - file := filepath.Join(os.TempDir(), filepath.Base(name)+hex.EncodeToString(r)) - - // Build the test binary - if err := exec.Command("go", "build", "-gcflags=-N -l", "-o", file, name+".go").Run(); err != nil { - t.Fatalf("Could not compile %s due to %s", name, err) +func TestMain(m *testing.M) { + fixturesDir := "../../_fixtures" + sources, err := ioutil.ReadDir(fixturesDir) + if err != nil { + fmt.Printf("Couldn't read fixtures dir: %v\n", err) + os.Exit(1) } - t.Logf("Compiled test binary %s", file) - defer os.Remove(file) + 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) + fixtures[name] = tmpfile + } + + status := m.Run() + + for _, f := range fixtures { + os.Remove(f) + } + + os.Exit(status) +} + +func withTestClient(name string, t *testing.T, fn func(c service.Client)) { listener, err := net.Listen("tcp", "localhost:0") if err != nil { t.Fatalf("couldn't start listener: %s\n", err) @@ -40,7 +63,7 @@ func withTestClient(name string, t *testing.T, fn func(c service.Client)) { server := NewServer(&Config{ Listener: listener, - ProcessArgs: []string{file}, + ProcessArgs: []string{fixtures[name]}, }) go server.Run() @@ -51,7 +74,7 @@ func withTestClient(name string, t *testing.T, fn func(c service.Client)) { } func TestClientServer_exit(t *testing.T) { - withTestClient(continuetestprog, t, func(c service.Client) { + withTestClient("continuetestprog", t, func(c service.Client) { state, err := c.GetState() if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -73,7 +96,7 @@ func TestClientServer_exit(t *testing.T) { } func TestClientServer_step(t *testing.T) { - withTestClient(testprog, t, func(c service.Client) { + withTestClient("testprog", t, func(c service.Client) { _, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.helloworld"}) if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -95,19 +118,12 @@ func TestClientServer_step(t *testing.T) { }) } -//func TestClientServer_next(t *testing.T) { type nextTest struct { begin, end int } func testnext(testcases []nextTest, initialLocation string, t *testing.T) { - fp, err := filepath.Abs(testnextprog) - if err != nil { - t.Fatal(err) - } - fp = fp + ".go" - - withTestClient(testnextprog, t, func(c service.Client) { + withTestClient("testnextprog", t, func(c service.Client) { bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: initialLocation}) if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -125,7 +141,7 @@ func testnext(testcases []nextTest, initialLocation string, t *testing.T) { for _, tc := range testcases { if state.CurrentThread.Line != tc.begin { - t.Fatalf("Program not stopped at correct spot expected %d was %s:%d", tc.begin, filepath.Base(fp), state.CurrentThread.Line) + t.Fatalf("Program not stopped at correct spot expected %d was %d", tc.begin, state.CurrentThread.Line) } t.Logf("Next for scenario %#v", tc) @@ -135,7 +151,7 @@ func testnext(testcases []nextTest, initialLocation string, t *testing.T) { } if state.CurrentThread.Line != tc.end { - t.Fatalf("Program did not continue to correct next location expected %d was %s:%d", tc.end, filepath.Base(fp), state.CurrentThread.Line) + t.Fatalf("Program did not continue to correct next location expected %d was %d", tc.end, state.CurrentThread.Line) } } }) @@ -179,7 +195,7 @@ func TestNextFunctionReturn(t *testing.T) { } func TestClientServer_breakpointInMainThread(t *testing.T) { - withTestClient(testprog, t, func(c service.Client) { + withTestClient("testprog", t, func(c service.Client) { bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.helloworld"}) if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -200,7 +216,7 @@ func TestClientServer_breakpointInMainThread(t *testing.T) { } func TestClientServer_breakpointInSeparateGoroutine(t *testing.T) { - withTestClient(testthreads, t, func(c service.Client) { + withTestClient("testthreads", t, func(c service.Client) { _, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.anotherthread"}) if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -219,7 +235,7 @@ func TestClientServer_breakpointInSeparateGoroutine(t *testing.T) { } func TestClientServer_breakAtNonexistentPoint(t *testing.T) { - withTestClient(testprog, t, func(c service.Client) { + withTestClient("testprog", t, func(c service.Client) { _, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "nowhere"}) if err == nil { t.Fatal("Should not be able to break at non existent function") @@ -228,7 +244,7 @@ func TestClientServer_breakAtNonexistentPoint(t *testing.T) { } func TestClientServer_clearBreakpoint(t *testing.T) { - withTestClient(testprog, t, func(c service.Client) { + withTestClient("testprog", t, func(c service.Client) { bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.sleepytime"}) if err != nil { t.Fatalf("Unexpected error: %v", err) @@ -256,7 +272,7 @@ func TestClientServer_clearBreakpoint(t *testing.T) { } func TestClientServer_switchThread(t *testing.T) { - withTestClient(testnextprog, t, func(c service.Client) { + withTestClient("testnextprog", t, func(c service.Client) { // With invalid thread id _, err := c.SwitchThread(-1) if err == nil { -- GitLab