提交 3ffbe2d7 编写于 作者: D Dan Mace 提交者: Derek Parker

Precompile fixtures

上级 2954e03a
...@@ -3,36 +3,59 @@ package rest ...@@ -3,36 +3,59 @@ package rest
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt"
"io/ioutil"
"net" "net"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/derekparker/delve/service" "github.com/derekparker/delve/service"
"github.com/derekparker/delve/service/api" "github.com/derekparker/delve/service/api"
) )
const ( var fixtures map[string]string = make(map[string]string)
continuetestprog = "../../_fixtures/continuetestprog"
testprog = "../../_fixtures/testprog"
testnextprog = "../../_fixtures/testnextprog"
testthreads = "../../_fixtures/testthreads"
)
func withTestClient(name string, t *testing.T, fn func(c service.Client)) { func TestMain(m *testing.M) {
// Make a (good enough) random temporary file name fixturesDir := "../../_fixtures"
r := make([]byte, 4) sources, err := ioutil.ReadDir(fixturesDir)
rand.Read(r) if err != nil {
file := filepath.Join(os.TempDir(), filepath.Base(name)+hex.EncodeToString(r)) fmt.Printf("Couldn't read fixtures dir: %v\n", err)
os.Exit(1)
// 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)
} }
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") listener, err := net.Listen("tcp", "localhost:0")
if err != nil { if err != nil {
t.Fatalf("couldn't start listener: %s\n", err) 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)) { ...@@ -40,7 +63,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{file}, ProcessArgs: []string{fixtures[name]},
}) })
go server.Run() go server.Run()
...@@ -51,7 +74,7 @@ func withTestClient(name string, t *testing.T, fn func(c service.Client)) { ...@@ -51,7 +74,7 @@ func withTestClient(name string, t *testing.T, fn func(c service.Client)) {
} }
func TestClientServer_exit(t *testing.T) { func TestClientServer_exit(t *testing.T) {
withTestClient(continuetestprog, t, func(c service.Client) { withTestClient("continuetestprog", t, func(c service.Client) {
state, err := c.GetState() state, err := c.GetState()
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
...@@ -73,7 +96,7 @@ func TestClientServer_exit(t *testing.T) { ...@@ -73,7 +96,7 @@ func TestClientServer_exit(t *testing.T) {
} }
func TestClientServer_step(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"}) _, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.helloworld"})
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
...@@ -95,19 +118,12 @@ func TestClientServer_step(t *testing.T) { ...@@ -95,19 +118,12 @@ func TestClientServer_step(t *testing.T) {
}) })
} }
//func TestClientServer_next(t *testing.T) {
type nextTest struct { type nextTest struct {
begin, end int begin, end int
} }
func testnext(testcases []nextTest, initialLocation string, t *testing.T) { func testnext(testcases []nextTest, initialLocation string, t *testing.T) {
fp, err := filepath.Abs(testnextprog) withTestClient("testnextprog", t, func(c service.Client) {
if err != nil {
t.Fatal(err)
}
fp = fp + ".go"
withTestClient(testnextprog, t, func(c service.Client) {
bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: initialLocation}) bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: initialLocation})
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
...@@ -125,7 +141,7 @@ func testnext(testcases []nextTest, initialLocation string, t *testing.T) { ...@@ -125,7 +141,7 @@ func testnext(testcases []nextTest, initialLocation string, t *testing.T) {
for _, tc := range testcases { for _, tc := range testcases {
if state.CurrentThread.Line != tc.begin { 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) t.Logf("Next for scenario %#v", tc)
...@@ -135,7 +151,7 @@ func testnext(testcases []nextTest, initialLocation string, t *testing.T) { ...@@ -135,7 +151,7 @@ func testnext(testcases []nextTest, initialLocation string, t *testing.T) {
} }
if state.CurrentThread.Line != tc.end { 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) { ...@@ -179,7 +195,7 @@ func TestNextFunctionReturn(t *testing.T) {
} }
func TestClientServer_breakpointInMainThread(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"}) bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.helloworld"})
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
...@@ -200,7 +216,7 @@ func TestClientServer_breakpointInMainThread(t *testing.T) { ...@@ -200,7 +216,7 @@ func TestClientServer_breakpointInMainThread(t *testing.T) {
} }
func TestClientServer_breakpointInSeparateGoroutine(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"}) _, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.anotherthread"})
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
...@@ -219,7 +235,7 @@ func TestClientServer_breakpointInSeparateGoroutine(t *testing.T) { ...@@ -219,7 +235,7 @@ func TestClientServer_breakpointInSeparateGoroutine(t *testing.T) {
} }
func TestClientServer_breakAtNonexistentPoint(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"}) _, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "nowhere"})
if err == nil { if err == nil {
t.Fatal("Should not be able to break at non existent function") t.Fatal("Should not be able to break at non existent function")
...@@ -228,7 +244,7 @@ func TestClientServer_breakAtNonexistentPoint(t *testing.T) { ...@@ -228,7 +244,7 @@ func TestClientServer_breakAtNonexistentPoint(t *testing.T) {
} }
func TestClientServer_clearBreakpoint(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"}) bp, err := c.CreateBreakPoint(&api.BreakPoint{FunctionName: "main.sleepytime"})
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
...@@ -256,7 +272,7 @@ func TestClientServer_clearBreakpoint(t *testing.T) { ...@@ -256,7 +272,7 @@ func TestClientServer_clearBreakpoint(t *testing.T) {
} }
func TestClientServer_switchThread(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 // With invalid thread id
_, err := c.SwitchThread(-1) _, err := c.SwitchThread(-1)
if err == nil { if err == nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册