build_test.go 2.9 KB

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