Merge pull request #181 from henryaj/add-build-for-platform
Add gexec.BuildWithEnvironment
diff --git a/gexec/build.go b/gexec/build.go
index 220c8c4..d11b2fd 100644
--- a/gexec/build.go
+++ b/gexec/build.go
@@ -24,13 +24,24 @@
Build uses the $GOPATH set in your environment. It passes the variadic args on to `go build`.
*/
func Build(packagePath string, args ...string) (compiledPath string, err error) {
- return BuildIn(os.Getenv("GOPATH"), packagePath, args...)
+ return doBuild(os.Getenv("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(os.Getenv("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...)
+}
+
+func doBuild(gopath, packagePath string, env []string, args ...string) (compiledPath string, err error) {
tmpDir, err := temporaryDirectory()
if err != nil {
return "", err
@@ -50,6 +61,7 @@
build := exec.Command("go", cmdArgs...)
build.Env = append([]string{"GOPATH=" + gopath}, os.Environ()...)
+ build.Env = append(build.Env, env...)
output, err := build.CombinedOutput()
if err != nil {
diff --git a/gexec/build_test.go b/gexec/build_test.go
index 7bd62fe..8df0790 100644
--- a/gexec/build_test.go
+++ b/gexec/build_test.go
@@ -1,14 +1,16 @@
package gexec_test
import (
+ "os"
+
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
-var _ = Describe(".Build", func() {
- var packagePath = "./_fixture/firefly"
+var packagePath = "./_fixture/firefly"
+var _ = Describe(".Build", func() {
Context("when there have been previous calls to Build", func() {
BeforeEach(func() {
_, err := gexec.Build(packagePath)
@@ -35,3 +37,23 @@
})
})
})
+
+var _ = Describe(".BuildWithEnvironment", func() {
+ var err error
+ env := []string{
+ "GOOS=linux",
+ "GOARCH=amd64",
+ }
+
+ It("compiles the specified package with the specified env vars", func() {
+ compiledPath, err := gexec.BuildWithEnvironment(packagePath, env)
+ Ω(err).ShouldNot(HaveOccurred())
+ Ω(compiledPath).Should(BeAnExistingFile())
+ })
+
+ It("returns the environment to a good state", func() {
+ _, err = gexec.BuildWithEnvironment(packagePath, env)
+ Ω(err).ShouldNot(HaveOccurred())
+ Ω(os.Environ()).ShouldNot(ContainElement("GOOS=linux"))
+ })
+})