1package gexec_test 2 3import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/onsi/gomega/gexec" 12) 13 14var packagePath = "./_fixture/firefly" 15 16var _ = Describe(".Build", func() { 17 Context("when there have been previous calls to Build", func() { 18 BeforeEach(func() { 19 _, err := gexec.Build(packagePath) 20 Expect(err).ShouldNot(HaveOccurred()) 21 }) 22 23 It("compiles the specified package", func() { 24 compiledPath, err := gexec.Build(packagePath) 25 Expect(err).ShouldNot(HaveOccurred()) 26 Expect(compiledPath).Should(BeAnExistingFile()) 27 }) 28 29 Context("and CleanupBuildArtifacts has been called", func() { 30 BeforeEach(func() { 31 gexec.CleanupBuildArtifacts() 32 }) 33 34 It("compiles the specified package", func() { 35 var err error 36 fireflyPath, err = gexec.Build(packagePath) 37 Expect(err).ShouldNot(HaveOccurred()) 38 Expect(fireflyPath).Should(BeAnExistingFile()) 39 }) 40 }) 41 }) 42}) 43 44var _ = Describe(".BuildWithEnvironment", func() { 45 var err error 46 env := []string{ 47 "GOOS=linux", 48 "GOARCH=amd64", 49 } 50 51 It("compiles the specified package with the specified env vars", func() { 52 compiledPath, err := gexec.BuildWithEnvironment(packagePath, env) 53 Expect(err).ShouldNot(HaveOccurred()) 54 Expect(compiledPath).Should(BeAnExistingFile()) 55 }) 56 57 It("returns the environment to a good state", func() { 58 _, err = gexec.BuildWithEnvironment(packagePath, env) 59 Expect(err).ShouldNot(HaveOccurred()) 60 Expect(os.Environ()).ShouldNot(ContainElement("GOOS=linux")) 61 }) 62}) 63 64var _ = Describe(".BuildIn", func() { 65 const ( 66 target = "github.com/onsi/gomega/gexec/_fixture/firefly/" 67 ) 68 69 var ( 70 original string 71 gopath string 72 ) 73 74 BeforeEach(func() { 75 var err error 76 original = os.Getenv("GOPATH") 77 gopath, err = ioutil.TempDir("", "") 78 Expect(err).NotTo(HaveOccurred()) 79 copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go") 80 Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed()) 81 Expect(os.Environ()).To(ContainElement(fmt.Sprintf("GOPATH=%s", filepath.Join(os.TempDir(), "emptyFakeGopath")))) 82 }) 83 84 AfterEach(func() { 85 if original == "" { 86 Expect(os.Unsetenv("GOPATH")).To(Succeed()) 87 } else { 88 Expect(os.Setenv("GOPATH", original)).To(Succeed()) 89 } 90 if gopath != "" { 91 os.RemoveAll(gopath) 92 } 93 }) 94 95 It("appends the gopath env var", func() { 96 _, err := gexec.BuildIn(gopath, target) 97 Expect(err).NotTo(HaveOccurred()) 98 }) 99 100 It("resets GOPATH to its original value", func() { 101 _, err := gexec.BuildIn(gopath, target) 102 Expect(err).NotTo(HaveOccurred()) 103 Expect(os.Getenv("GOPATH")).To(Equal(filepath.Join(os.TempDir(), "emptyFakeGopath"))) 104 }) 105}) 106 107func copyFile(source, directory, basename string) { 108 Expect(os.MkdirAll(directory, 0755)).To(Succeed()) 109 content, err := ioutil.ReadFile(source) 110 Expect(err).NotTo(HaveOccurred()) 111 Expect(ioutil.WriteFile(filepath.Join(directory, basename), content, 0644)).To(Succeed()) 112} 113