21 Build uses go build to compile the package at packagePath. The resulting binary is saved off in a temporary directory.
22 A path pointing to this binary is returned.
24 Build uses the $GOPATH set in your environment. It passes the variadic args on to `go build`.
26 func Build(packagePath string, args ...string) (compiledPath string, err error) {
27 return doBuild(os.Getenv("GOPATH"), packagePath, nil, args...)
31 BuildWithEnvironment is identical to Build but allows you to specify env vars to be set at build time.
33 func BuildWithEnvironment(packagePath string, env []string, args ...string) (compiledPath string, err error) {
34 return doBuild(os.Getenv("GOPATH"), packagePath, env, args...)
38 BuildIn is identical to Build but allows you to specify a custom $GOPATH (the first argument).
40 func BuildIn(gopath string, packagePath string, args ...string) (compiledPath string, err error) {
41 return doBuild(gopath, packagePath, nil, args...)
44 func doBuild(gopath, packagePath string, env []string, args ...string) (compiledPath string, err error) {
45 tmpDir, err := temporaryDirectory()
51 return "", errors.New("$GOPATH not provided when building " + packagePath)
54 executable := filepath.Join(tmpDir, path.Base(packagePath))
55 if runtime.GOOS == "windows" {
56 executable = executable + ".exe"
59 cmdArgs := append([]string{"build"}, args...)
60 cmdArgs = append(cmdArgs, "-o", executable, packagePath)
62 build := exec.Command("go", cmdArgs...)
63 build.Env = append([]string{"GOPATH=" + gopath}, os.Environ()...)
64 build.Env = append(build.Env, env...)
66 output, err := build.CombinedOutput()
68 return "", fmt.Errorf("Failed to build %s:\n\nError:\n%s\n\nOutput:\n%s", packagePath, err, string(output))
71 return executable, nil
75 You should call CleanupBuildArtifacts before your test ends to clean up any temporary artifacts generated by
76 gexec. In Ginkgo this is typically done in an AfterSuite callback.
78 func CleanupBuildArtifacts() {
87 func temporaryDirectory() (string, error) {
92 tmpDir, err = ioutil.TempDir("", "gexec_artifacts")
98 return ioutil.TempDir(tmpDir, "g")