forked from go-delve/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.go
82 lines (69 loc) · 1.92 KB
/
support.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package test
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"runtime"
)
// Fixture is a test binary.
type Fixture struct {
// Name is the short name of the fixture.
Name string
// Path is the absolute path to the test binary.
Path string
// Source is the absolute path of the test binary source.
Source string
}
// Fixtures is a map of Fixture.Name to Fixture.
var Fixtures map[string]Fixture = make(map[string]Fixture)
func FindFixturesDir() string {
parent := ".."
fixturesDir := "_fixtures"
for depth := 0; depth < 10; depth++ {
if _, err := os.Stat(fixturesDir); err == nil {
break
}
fixturesDir = filepath.Join(parent, fixturesDir)
}
return fixturesDir
}
func BuildFixture(name string) Fixture {
if f, ok := Fixtures[name]; ok {
return f
}
fixturesDir := FindFixturesDir()
// 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)))
buildFlags := []string{"build"}
if runtime.GOOS == "windows" {
// Work-around for https://github.com/golang/go/issues/13154
buildFlags = append(buildFlags, "-ldflags=-linkmode internal")
}
buildFlags = append(buildFlags, "-gcflags=-N -l", "-o", tmpfile, path)
// Build the test binary
if err := exec.Command("go", buildFlags...).Run(); err != nil {
fmt.Printf("Error compiling %s: %s\n", path, err)
os.Exit(1)
}
source, _ := filepath.Abs(path)
source = filepath.ToSlash(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) int {
status := m.Run()
// Remove the fixtures.
for _, f := range Fixtures {
os.Remove(f.Path)
}
return status
}