Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

410/gexec gopackage type #414

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions gexec/_fixture/firefly/main_test.go
@@ -0,0 +1,7 @@
package main_test

import "testing"

func Test(t *testing.T) {
t.Log("Hum, it seems okay.")
}
81 changes: 27 additions & 54 deletions gexec/build.go
Expand Up @@ -3,87 +3,60 @@
package gexec

import (
"errors"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/onsi/gomega/gexec/gopackage"
"github.com/onsi/gomega/gexec/gotestpackage"
)

var (
mu sync.Mutex
tmpDir string
)

/*
Build uses go build to compile the package at packagePath. The resulting binary is saved off in a temporary directory.
A path pointing to this binary is returned.

Build uses the $GOPATH set in your environment. If $GOPATH is not set and you are using Go 1.8+,
it will use the default GOPATH instead. It passes the variadic args on to `go build`.
*/
func Build(packagePath string, args ...string) (compiledPath string, err error) {
return doBuild(build.Default.GOPATH, packagePath, nil, args...)
}

/*
BuildWithEnvironment is identical to Build but allows you to specify env vars to be set at build time.
*/
func BuildWithEnvironment(packagePath string, env []string, args ...string) (compiledPath string, err error) {
return doBuild(build.Default.GOPATH, packagePath, env, args...)
}

/*
BuildIn is identical to Build but allows you to specify a custom $GOPATH (the first argument).
*/
func BuildIn(gopath string, packagePath string, args ...string) (compiledPath string, err error) {
return doBuild(gopath, packagePath, nil, args...)
type GoPackage interface {
Build(args ...string) (string, error)
BuildWithEnvironment(envs []string, args ...string) (string, error)
BuildIn(gopath string, args ...string) (string, error)
}

func replaceGoPath(environ []string, newGoPath string) []string {
newEnviron := []string{}
for _, v := range environ {
if !strings.HasPrefix(v, "GOPATH=") {
newEnviron = append(newEnviron, v)
}
func Get(packageName string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return nil, err
}
return append(newEnviron, "GOPATH="+newGoPath)

return gopackage.Get(tmpDir, packageName), nil
}

func doBuild(gopath, packagePath string, env []string, args ...string) (compiledPath string, err error) {
func GetTests(packagePath string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return "", err
return nil, err
}

if len(gopath) == 0 {
return "", errors.New("$GOPATH not provided when building " + packagePath)
}
return gotestpackage.Get(tmpDir, build.Default.GOPATH, packagePath, nil)
}

executable := filepath.Join(tmpDir, path.Base(packagePath))
if runtime.GOOS == "windows" {
executable += ".exe"
func GetTestsWithEnvironment(envs []string, packagePath string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return nil, err
}

cmdArgs := append([]string{"build"}, args...)
cmdArgs = append(cmdArgs, "-o", executable, packagePath)

build := exec.Command("go", cmdArgs...)
build.Env = replaceGoPath(os.Environ(), gopath)
build.Env = append(build.Env, env...)
return gotestpackage.Get(tmpDir, build.Default.GOPATH, packagePath, envs)
}

output, err := build.CombinedOutput()
func GetTestsIn(gopath, packagePath string) (GoPackage, error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return "", fmt.Errorf("Failed to build %s:\n\nError:\n%s\n\nOutput:\n%s", packagePath, err, string(output))
return nil, err
}

return executable, nil
return gotestpackage.Get(tmpDir, gopath, packagePath, nil)
}

/*
Expand Down