Codebase list golang-gomega / 3433d7b
Add BuildWithEnvironment() to gexec Henry Stanley 7 years ago
2 changed file(s) with 44 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
2323 Build uses the $GOPATH set in your environment. It passes the variadic args on to `go build`.
2424 */
2525 func Build(packagePath string, args ...string) (compiledPath string, err error) {
26 return BuildIn(os.Getenv("GOPATH"), packagePath, args...)
27 }
28
29 /*
30 BuildWithEnvironment is identical to Build but allows you to specify env vars to be set at build time.
31 */
32 func BuildWithEnvironment(packagePath string, env map[string]string, args ...string) (compiledPath string, err error) {
33 for key, val := range env {
34 os.Setenv(key, val)
35 defer os.Unsetenv(key)
36 }
37
2638 return BuildIn(os.Getenv("GOPATH"), packagePath, args...)
2739 }
2840
00 package gexec_test
11
22 import (
3 "os"
4
35 . "github.com/onsi/ginkgo"
46 . "github.com/onsi/gomega"
57 "github.com/onsi/gomega/gexec"
68 )
79
10 var packagePath = "./_fixture/firefly"
11
812 var _ = Describe(".Build", func() {
9 var packagePath = "./_fixture/firefly"
10
1113 Context("when there have been previous calls to Build", func() {
1214 BeforeEach(func() {
1315 _, err := gexec.Build(packagePath)
3436 })
3537 })
3638 })
39
40 var _ = Describe(".BuildWithEnvironment", func() {
41 var err error
42
43 It("compiles the specified package with the specified env vars", func() {
44 env := map[string]string{
45 "GOOS": "linux",
46 "GOARCH": "amd64",
47 }
48
49 compiledPath, err := gexec.BuildWithEnvironment(packagePath, env)
50 Ω(err).ShouldNot(HaveOccurred())
51 Ω(compiledPath).Should(BeAnExistingFile())
52 })
53
54 It("returns the environment to a good state", func() {
55 knownGoodEnv := os.Environ()
56
57 env := map[string]string{
58 "THIS_ENV_VAR": "SHOULD_NOT_BE_SET",
59 }
60
61 _, err = gexec.BuildWithEnvironment(packagePath, env)
62 Ω(err).ShouldNot(HaveOccurred())
63
64 Ω(os.Environ()).Should(Equal(knownGoodEnv))
65 })
66 })