package cmdtest

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
)

func Build(mainPath string, args ...string) (string, error) {
	return BuildIn(os.Getenv("GOPATH"), mainPath, args...)
}

func BuildIn(gopath string, mainPath string, args ...string) (string, error) {
	if len(gopath) == 0 {
		panic("$GOPATH not provided when building " + mainPath)
	}

	tmpdir, err := ioutil.TempDir("", "test_cmd_main")
	if err != nil {
		return "", err
	}

	executable := filepath.Join(tmpdir, filepath.Base(mainPath))

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

	build := exec.Command("go", cmdArgs...)
	build.Stdout = os.Stdout
	build.Stderr = os.Stderr
	build.Stdin = os.Stdin
	build.Env = append(os.Environ(), "GOPATH="+gopath)

	err = build.Run()
	if err != nil {
		return "", err
	}

	return executable, nil
}

package cmdtest

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
)

func Build(mainPath string, args ...string) (string, error) {
	return BuildIn(os.Getenv("GOPATH"), mainPath, args...)
}

func BuildIn(gopath string, mainPath string, args ...string) (string, error) {
	if len(gopath) == 0 {
		panic("$GOPATH not provided when building " + mainPath)
	}

	tmpdir, err := ioutil.TempDir("", "test_cmd_main")
	if err != nil {
		return "", err
	}

	executable := filepath.Join(tmpdir, filepath.Base(mainPath))

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

	build := exec.Command("go", cmdArgs...)
	build.Stdout = os.Stdout
	build.Stderr = os.Stderr
	build.Stdin = os.Stdin
	build.Env = []string{"GOPATH=" + gopath}

	err = build.Run()
	if err != nil {
		return "", err
	}

	return executable, nil
}

--- 
+++ 
@@ -30,7 +30,7 @@
 	build.Stdout = os.Stdout
 	build.Stderr = os.Stderr
 	build.Stdin = os.Stdin
-	build.Env = []string{"GOPATH=" + gopath}
+	build.Env = append(os.Environ(), "GOPATH="+gopath)
 
 	err = build.Run()
 	if err != nil {
Include existing env vars in Build

Signed-off-by: Max Brunsfeld <8ee73e75b50cc292b5052b11f2ca25336d3e974e@pivotallabs.com>
