...

Source file src/github.com/onsi/gomega/gexec/build_test.go

Documentation: github.com/onsi/gomega/gexec

     1  package gexec_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gexec"
    11  	"github.com/onsi/gomega/internal/gutil"
    12  )
    13  
    14  var packagePath = "./_fixture/firefly"
    15  
    16  var _ = Describe(".Build", func() {
    17  	When("there have been previous calls to CompileTest", func() {
    18  		BeforeEach(func() {
    19  			_, err := gexec.CompileTest(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  			Expect(filepath.Base(compiledPath)).Should(MatchRegexp(`firefly(|.exe)$`))
    28  		})
    29  
    30  		Context("and CleanupBuildArtifacts has been called", func() {
    31  			BeforeEach(func() {
    32  				gexec.CleanupBuildArtifacts()
    33  			})
    34  
    35  			It("compiles the specified package", func() {
    36  				fireflyPath, err := gexec.Build(packagePath)
    37  				Expect(err).ShouldNot(HaveOccurred())
    38  				Expect(fireflyPath).Should(BeAnExistingFile())
    39  			})
    40  		})
    41  	})
    42  
    43  	When("there have been previous calls to Build", func() {
    44  		BeforeEach(func() {
    45  			_, err := gexec.Build(packagePath)
    46  			Expect(err).ShouldNot(HaveOccurred())
    47  		})
    48  
    49  		It("compiles the specified package", func() {
    50  			compiledPath, err := gexec.Build(packagePath)
    51  			Expect(err).ShouldNot(HaveOccurred())
    52  			Expect(compiledPath).Should(BeAnExistingFile())
    53  		})
    54  
    55  		Context("and CleanupBuildArtifacts has been called", func() {
    56  			BeforeEach(func() {
    57  				gexec.CleanupBuildArtifacts()
    58  			})
    59  
    60  			It("compiles the specified package", func() {
    61  				fireflyPath, err := gexec.Build(packagePath)
    62  				Expect(err).ShouldNot(HaveOccurred())
    63  				Expect(fireflyPath).Should(BeAnExistingFile())
    64  			})
    65  		})
    66  	})
    67  })
    68  
    69  var _ = Describe(".BuildWithEnvironment", func() {
    70  	var err error
    71  	env := []string{
    72  		"GOOS=linux",
    73  		"GOARCH=amd64",
    74  	}
    75  
    76  	It("compiles the specified package with the specified env vars", func() {
    77  		compiledPath, err := gexec.BuildWithEnvironment(packagePath, env)
    78  		Expect(err).ShouldNot(HaveOccurred())
    79  		Expect(compiledPath).Should(BeAnExistingFile())
    80  	})
    81  
    82  	It("returns the environment to a good state", func() {
    83  		_, err = gexec.BuildWithEnvironment(packagePath, env)
    84  		Expect(err).ShouldNot(HaveOccurred())
    85  		Expect(os.Environ()).ShouldNot(ContainElement("GOOS=linux"))
    86  	})
    87  })
    88  
    89  var _ = Describe(".BuildIn", func() {
    90  	const (
    91  		target = "github.com/onsi/gomega/gexec/_fixture/firefly/"
    92  	)
    93  
    94  	var (
    95  		original string
    96  		gopath   string
    97  	)
    98  
    99  	BeforeEach(func() {
   100  		var err error
   101  		original = os.Getenv("GOPATH")
   102  		gopath, err = gutil.MkdirTemp("", "")
   103  		Expect(err).NotTo(HaveOccurred())
   104  		copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
   105  		Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
   106  		Expect(os.Environ()).To(ContainElement(fmt.Sprintf("GOPATH=%s", filepath.Join(os.TempDir(), "emptyFakeGopath"))))
   107  	})
   108  
   109  	AfterEach(func() {
   110  		if original == "" {
   111  			Expect(os.Unsetenv("GOPATH")).To(Succeed())
   112  		} else {
   113  			Expect(os.Setenv("GOPATH", original)).To(Succeed())
   114  		}
   115  		if gopath != "" {
   116  			os.RemoveAll(gopath)
   117  		}
   118  	})
   119  
   120  	It("appends the gopath env var", func() {
   121  		compiledPath, err := gexec.BuildIn(gopath, target)
   122  		Expect(err).NotTo(HaveOccurred())
   123  		Expect(compiledPath).Should(BeAnExistingFile())
   124  	})
   125  
   126  	It("resets GOPATH to its original value", func() {
   127  		_, err := gexec.BuildIn(gopath, target)
   128  		Expect(err).NotTo(HaveOccurred())
   129  		Expect(os.Getenv("GOPATH")).To(Equal(filepath.Join(os.TempDir(), "emptyFakeGopath")))
   130  	})
   131  })
   132  
   133  var _ = Describe(".CompileTest", func() {
   134  	Context("a remote package", Label("network"), func() {
   135  		const remotePackage = "github.com/onsi/ginkgo/types"
   136  
   137  		It("compiles the specified test package", func() {
   138  			compiledPath, err := gexec.GetAndCompileTest(remotePackage)
   139  			Expect(err).ShouldNot(HaveOccurred())
   140  			Expect(compiledPath).Should(BeAnExistingFile())
   141  		})
   142  	})
   143  
   144  	When("there have been previous calls to CompileTest", func() {
   145  		BeforeEach(func() {
   146  			_, err := gexec.CompileTest(packagePath)
   147  			Expect(err).ShouldNot(HaveOccurred())
   148  		})
   149  
   150  		It("compiles the specified test package", func() {
   151  			compiledPath, err := gexec.CompileTest(packagePath)
   152  			Expect(err).ShouldNot(HaveOccurred())
   153  			Expect(compiledPath).Should(BeAnExistingFile())
   154  		})
   155  
   156  		Context("and CleanupBuildArtifacts has been called", func() {
   157  			BeforeEach(func() {
   158  				gexec.CleanupBuildArtifacts()
   159  			})
   160  
   161  			It("compiles the specified test package", func() {
   162  				fireflyTestPath, err := gexec.CompileTest(packagePath)
   163  				Expect(err).ShouldNot(HaveOccurred())
   164  				Expect(fireflyTestPath).Should(BeAnExistingFile())
   165  			})
   166  		})
   167  	})
   168  
   169  	When("there have been previous calls to Build", func() {
   170  		BeforeEach(func() {
   171  			_, err := gexec.Build(packagePath)
   172  			Expect(err).ShouldNot(HaveOccurred())
   173  		})
   174  
   175  		It("compiles the specified test package", func() {
   176  			compiledPath, err := gexec.CompileTest(packagePath)
   177  			Expect(err).ShouldNot(HaveOccurred())
   178  			Expect(compiledPath).Should(BeAnExistingFile())
   179  		})
   180  
   181  		Context("and CleanupBuildArtifacts has been called", func() {
   182  			BeforeEach(func() {
   183  				gexec.CleanupBuildArtifacts()
   184  			})
   185  
   186  			It("compiles the specified test package", func() {
   187  				fireflyTestPath, err := gexec.CompileTest(packagePath)
   188  				Expect(err).ShouldNot(HaveOccurred())
   189  				Expect(fireflyTestPath).Should(BeAnExistingFile())
   190  			})
   191  		})
   192  	})
   193  })
   194  
   195  var _ = Describe(".CompileTestWithEnvironment", func() {
   196  	var err error
   197  	env := []string{
   198  		"GOOS=linux",
   199  		"GOARCH=amd64",
   200  	}
   201  
   202  	Context("a remote package", Label("network"), func() {
   203  		const remotePackage = "github.com/onsi/ginkgo/types"
   204  
   205  		It("compiles the specified test package with the specified env vars", func() {
   206  			compiledPath, err := gexec.GetAndCompileTestWithEnvironment(remotePackage, env)
   207  			Expect(err).ShouldNot(HaveOccurred())
   208  			Expect(compiledPath).Should(BeAnExistingFile())
   209  		})
   210  	})
   211  
   212  	It("compiles the specified test package with the specified env vars", func() {
   213  		compiledPath, err := gexec.CompileTestWithEnvironment(packagePath, env)
   214  		Expect(err).ShouldNot(HaveOccurred())
   215  		Expect(compiledPath).Should(BeAnExistingFile())
   216  	})
   217  
   218  	It("returns the environment to a good state", func() {
   219  		_, err = gexec.CompileTestWithEnvironment(packagePath, env)
   220  		Expect(err).ShouldNot(HaveOccurred())
   221  		Expect(os.Environ()).ShouldNot(ContainElement("GOOS=linux"))
   222  	})
   223  })
   224  
   225  var _ = Describe(".CompiledTestIn", func() {
   226  	const target = "github.com/onsi/gomega/gexec/_fixture/firefly"
   227  
   228  	var (
   229  		original string
   230  		gopath   string
   231  	)
   232  
   233  	BeforeEach(func() {
   234  		var err error
   235  		original = os.Getenv("GOPATH")
   236  		gopath, err = gutil.MkdirTemp("", "")
   237  		Expect(err).NotTo(HaveOccurred())
   238  		Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
   239  		Expect(os.Environ()).To(ContainElement(fmt.Sprintf("GOPATH=%s", filepath.Join(os.TempDir(), "emptyFakeGopath"))))
   240  	})
   241  
   242  	AfterEach(func() {
   243  		if original == "" {
   244  			Expect(os.Unsetenv("GOPATH")).To(Succeed())
   245  		} else {
   246  			Expect(os.Setenv("GOPATH", original)).To(Succeed())
   247  		}
   248  		if gopath != "" {
   249  			os.RemoveAll(gopath)
   250  		}
   251  	})
   252  
   253  	Context("a remote package", Label("network"), func() {
   254  		const remotePackage = "github.com/onsi/ginkgo/types"
   255  
   256  		It("compiles the specified test package", func() {
   257  			compiledPath, err := gexec.GetAndCompileTestIn(gopath, remotePackage)
   258  			Expect(err).ShouldNot(HaveOccurred())
   259  			Expect(compiledPath).Should(BeAnExistingFile())
   260  		})
   261  	})
   262  
   263  	It("appends the gopath env var", func() {
   264  		compiledPath, err := gexec.CompileTestIn(gopath, target)
   265  		Expect(err).NotTo(HaveOccurred())
   266  		Expect(compiledPath).Should(BeAnExistingFile())
   267  	})
   268  
   269  	It("resets GOPATH to its original value", func() {
   270  		_, err := gexec.CompileTestIn(gopath, target)
   271  		Expect(err).NotTo(HaveOccurred())
   272  		Expect(os.Getenv("GOPATH")).To(Equal(filepath.Join(os.TempDir(), "emptyFakeGopath")))
   273  	})
   274  })
   275  
   276  func copyFile(source, directory, basename string) {
   277  	Expect(os.MkdirAll(directory, 0755)).To(Succeed())
   278  	content, err := gutil.ReadFile(source)
   279  	Expect(err).NotTo(HaveOccurred())
   280  	Expect(gutil.WriteFile(filepath.Join(directory, basename), content)).To(Succeed())
   281  }
   282  

View as plain text