Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/onsi/gomega/gexec/build.go
2880 views
1
// untested sections: 5
2
3
package gexec
4
5
import (
6
"errors"
7
"fmt"
8
"go/build"
9
"os"
10
"os/exec"
11
"path"
12
"path/filepath"
13
"runtime"
14
"strings"
15
"sync"
16
17
"github.com/onsi/gomega/internal/gutil"
18
)
19
20
var (
21
mu sync.Mutex
22
tmpDir string
23
)
24
25
/*
26
Build uses go build to compile the package at packagePath. The resulting binary is saved off in a temporary directory.
27
A path pointing to this binary is returned.
28
29
Build uses the $GOPATH set in your environment. If $GOPATH is not set and you are using Go 1.8+,
30
it will use the default GOPATH instead. It passes the variadic args on to `go build`.
31
*/
32
func Build(packagePath string, args ...string) (compiledPath string, err error) {
33
return doBuild(build.Default.GOPATH, packagePath, nil, args...)
34
}
35
36
/*
37
BuildWithEnvironment is identical to Build but allows you to specify env vars to be set at build time.
38
*/
39
func BuildWithEnvironment(packagePath string, env []string, args ...string) (compiledPath string, err error) {
40
return doBuild(build.Default.GOPATH, packagePath, env, args...)
41
}
42
43
/*
44
BuildIn is identical to Build but allows you to specify a custom $GOPATH (the first argument).
45
*/
46
func BuildIn(gopath string, packagePath string, args ...string) (compiledPath string, err error) {
47
return doBuild(gopath, packagePath, nil, args...)
48
}
49
50
func doBuild(gopath, packagePath string, env []string, args ...string) (compiledPath string, err error) {
51
executable, err := newExecutablePath(gopath, packagePath)
52
if err != nil {
53
return "", err
54
}
55
56
cmdArgs := append([]string{"build"}, args...)
57
cmdArgs = append(cmdArgs, "-o", executable, packagePath)
58
59
build := exec.Command("go", cmdArgs...)
60
build.Env = replaceGoPath(os.Environ(), gopath)
61
build.Env = append(build.Env, env...)
62
63
output, err := build.CombinedOutput()
64
if err != nil {
65
return "", fmt.Errorf("Failed to build %s:\n\nError:\n%s\n\nOutput:\n%s", packagePath, err, string(output))
66
}
67
68
return executable, nil
69
}
70
71
/*
72
CompileTest uses go test to compile the test package at packagePath. The resulting binary is saved off in a temporary directory.
73
A path pointing to this binary is returned.
74
75
CompileTest uses the $GOPATH set in your environment. If $GOPATH is not set and you are using Go 1.8+,
76
it will use the default GOPATH instead. It passes the variadic args on to `go test`.
77
78
Deprecated: CompileTest makes GOPATH assumptions that don't translate well to the go modules world.
79
*/
80
func CompileTest(packagePath string, args ...string) (compiledPath string, err error) {
81
return doCompileTest(build.Default.GOPATH, packagePath, nil, args...)
82
}
83
84
/*
85
GetAndCompileTest is identical to CompileTest but `go get` the package before compiling tests.
86
87
Deprecated: GetAndCompileTest makes GOPATH assumptions that don't translate well to the go modules world.
88
*/
89
func GetAndCompileTest(packagePath string, args ...string) (compiledPath string, err error) {
90
if err := getForTest(build.Default.GOPATH, packagePath, []string{"GO111MODULE=off"}); err != nil {
91
return "", err
92
}
93
94
return doCompileTest(build.Default.GOPATH, packagePath, []string{"GO111MODULE=off"}, args...)
95
}
96
97
/*
98
CompileTestWithEnvironment is identical to CompileTest but allows you to specify env vars to be set at build time.
99
100
Deprecated: CompileTestWithEnvironment makes GOPATH assumptions that don't translate well to the go modules world.
101
*/
102
func CompileTestWithEnvironment(packagePath string, env []string, args ...string) (compiledPath string, err error) {
103
return doCompileTest(build.Default.GOPATH, packagePath, env, args...)
104
}
105
106
/*
107
GetAndCompileTestWithEnvironment is identical to GetAndCompileTest but allows you to specify env vars to be set at build time.
108
109
Deprecated: GetAndCompileTestWithEnvironment makes GOPATH assumptions that don't translate well to the go modules world.
110
*/
111
func GetAndCompileTestWithEnvironment(packagePath string, env []string, args ...string) (compiledPath string, err error) {
112
if err := getForTest(build.Default.GOPATH, packagePath, append(env, "GO111MODULE=off")); err != nil {
113
return "", err
114
}
115
116
return doCompileTest(build.Default.GOPATH, packagePath, append(env, "GO111MODULE=off"), args...)
117
}
118
119
/*
120
CompileTestIn is identical to CompileTest but allows you to specify a custom $GOPATH (the first argument).
121
122
Deprecated: CompileTestIn makes GOPATH assumptions that don't translate well to the go modules world.
123
*/
124
func CompileTestIn(gopath string, packagePath string, args ...string) (compiledPath string, err error) {
125
return doCompileTest(gopath, packagePath, nil, args...)
126
}
127
128
/*
129
GetAndCompileTestIn is identical to GetAndCompileTest but allows you to specify a custom $GOPATH (the first argument).
130
*/
131
func GetAndCompileTestIn(gopath string, packagePath string, args ...string) (compiledPath string, err error) {
132
if err := getForTest(gopath, packagePath, []string{"GO111MODULE=off"}); err != nil {
133
return "", err
134
}
135
136
return doCompileTest(gopath, packagePath, []string{"GO111MODULE=off"}, args...)
137
}
138
139
func isLocalPackage(packagePath string) bool {
140
return strings.HasPrefix(packagePath, ".")
141
}
142
143
func getForTest(gopath, packagePath string, env []string) error {
144
if isLocalPackage(packagePath) {
145
return nil
146
}
147
148
return doGet(gopath, packagePath, env, "-t")
149
}
150
151
func doGet(gopath, packagePath string, env []string, args ...string) error {
152
args = append(args, packagePath)
153
args = append([]string{"get"}, args...)
154
155
goGet := exec.Command("go", args...)
156
goGet.Dir = gopath
157
goGet.Env = replaceGoPath(os.Environ(), gopath)
158
goGet.Env = append(goGet.Env, env...)
159
160
output, err := goGet.CombinedOutput()
161
if err != nil {
162
return fmt.Errorf("Failed to get %s:\n\nError:\n%s\n\nOutput:\n%s", packagePath, err, string(output))
163
}
164
165
return nil
166
}
167
168
func doCompileTest(gopath, packagePath string, env []string, args ...string) (compiledPath string, err error) {
169
executable, err := newExecutablePath(gopath, packagePath, ".test")
170
if err != nil {
171
return "", err
172
}
173
174
cmdArgs := append([]string{"test", "-c"}, args...)
175
cmdArgs = append(cmdArgs, "-o", executable, packagePath)
176
177
build := exec.Command("go", cmdArgs...)
178
build.Env = replaceGoPath(os.Environ(), gopath)
179
build.Env = append(build.Env, env...)
180
181
output, err := build.CombinedOutput()
182
if err != nil {
183
return "", fmt.Errorf("Failed to build %s:\n\nError:\n%s\n\nOutput:\n%s", packagePath, err, string(output))
184
}
185
186
return executable, nil
187
}
188
189
func replaceGoPath(environ []string, newGoPath string) []string {
190
newEnviron := []string{}
191
for _, v := range environ {
192
if !strings.HasPrefix(v, "GOPATH=") {
193
newEnviron = append(newEnviron, v)
194
}
195
}
196
return append(newEnviron, "GOPATH="+newGoPath)
197
}
198
199
func newExecutablePath(gopath, packagePath string, suffixes ...string) (string, error) {
200
tmpDir, err := temporaryDirectory()
201
if err != nil {
202
return "", err
203
}
204
205
if len(gopath) == 0 {
206
return "", errors.New("$GOPATH not provided when building " + packagePath)
207
}
208
209
executable := filepath.Join(tmpDir, path.Base(packagePath))
210
211
if runtime.GOOS == "windows" {
212
executable += ".exe"
213
}
214
215
return executable, nil
216
}
217
218
/*
219
You should call CleanupBuildArtifacts before your test ends to clean up any temporary artifacts generated by
220
gexec. In Ginkgo this is typically done in an AfterSuite callback.
221
*/
222
func CleanupBuildArtifacts() {
223
mu.Lock()
224
defer mu.Unlock()
225
if tmpDir != "" {
226
os.RemoveAll(tmpDir)
227
tmpDir = ""
228
}
229
}
230
231
func temporaryDirectory() (string, error) {
232
var err error
233
mu.Lock()
234
defer mu.Unlock()
235
if tmpDir == "" {
236
tmpDir, err = gutil.MkdirTemp("", "gexec_artifacts")
237
if err != nil {
238
return "", err
239
}
240
}
241
242
return gutil.MkdirTemp(tmpDir, "g")
243
}
244
245