-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
build.go
87 lines (71 loc) · 1.75 KB
/
build.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
83
84
85
86
87
// untested sections: 5
package gexec
import (
"go/build"
"io/ioutil"
"os"
"sync"
"github.com/onsi/gomega/gexec/gopackage"
"github.com/onsi/gomega/gexec/gotestpackage"
)
var (
mu sync.Mutex
tmpDir string
)
type GoPackage interface {
Build(args ...string) (string, error)
BuildWithEnvironment(envs []string, args ...string) (string, error)
BuildIn(gopath string, args ...string) (string, error)
}
func Get(packageName string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return nil, err
}
return gopackage.Get(tmpDir, packageName), nil
}
func GetTests(packagePath string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return nil, err
}
return gotestpackage.Get(tmpDir, build.Default.GOPATH, packagePath, nil)
}
func GetTestsWithEnvironment(envs []string, packagePath string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return nil, err
}
return gotestpackage.Get(tmpDir, build.Default.GOPATH, packagePath, envs)
}
func GetTestsIn(gopath, packagePath string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return nil, err
}
return gotestpackage.Get(tmpDir, gopath, packagePath, nil)
}
/*
You should call CleanupBuildArtifacts before your test ends to clean up any temporary artifacts generated by
gexec. In Ginkgo this is typically done in an AfterSuite callback.
*/
func CleanupBuildArtifacts() {
mu.Lock()
defer mu.Unlock()
if tmpDir != "" {
os.RemoveAll(tmpDir)
tmpDir = ""
}
}
func temporaryDirectory() (string, error) {
var err error
mu.Lock()
defer mu.Unlock()
if tmpDir == "" {
tmpDir, err = ioutil.TempDir("", "gexec_artifacts")
if err != nil {
return "", err
}
}
return ioutil.TempDir(tmpDir, "g")
}