From 606fba28a39e54737fa2dc5f018d99906a0a9a47 Mon Sep 17 00:00:00 2001 From: Onsi Fakhouri Date: Fri, 12 Jun 2020 12:50:41 -0600 Subject: [PATCH] ensure integration tests pass in an environment sans GOPATH --- ginkgo/main.go | 4 +- go.mod | 1 + go.sum | 2 + integration/_fixtures/watch_fixtures/A/A.go | 2 +- .../_fixtures/watch_fixtures/A/A_test.go | 2 +- integration/_fixtures/watch_fixtures/B/B.go | 2 +- .../_fixtures/watch_fixtures/B/B_test.go | 2 +- .../_fixtures/watch_fixtures/C/C_test.go | 2 +- integration/_fixtures/watch_fixtures/D/D.go | 2 +- .../_fixtures/watch_fixtures/D/D_test.go | 2 +- integration/coverage_test.go | 1 + integration/integration_suite_test.go | 6 +- integration/run_test.go | 2 +- integration/subcommand_test.go | 4 +- integration/verbose_and_succinct_test.go | 8 +- integration/watch_test.go | 106 ++++++++++-------- 16 files changed, 85 insertions(+), 63 deletions(-) diff --git a/ginkgo/main.go b/ginkgo/main.go index 0741ba8c9..f60c48a72 100644 --- a/ginkgo/main.go +++ b/ginkgo/main.go @@ -288,9 +288,9 @@ func findSuites(args []string, recurseForAll bool, skipPackage string, allowPrec } func goFmt(path string) { - err := exec.Command("go", "fmt", path).Run() + out, err := exec.Command("go", "fmt", path).CombinedOutput() if err != nil { - complainAndQuit("Could not fmt: " + err.Error()) + complainAndQuit("Could not fmt: " + err.Error() + "\n" + string(out)) } } diff --git a/go.mod b/go.mod index 1f7125228..212b2454c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/nxadm/tail v1.4.4 github.com/onsi/gomega v1.10.1 + github.com/sclevine/agouti v3.0.0+incompatible // indirect golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 golang.org/x/text v0.3.2 // indirect ) diff --git a/go.sum b/go.sum index 2b774f3e8..beb29b3e6 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/sclevine/agouti v3.0.0+incompatible h1:8IBJS6PWz3uTlMP3YBIR5f+KAldcGuOeFkFbUWfBgK4= +github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/integration/_fixtures/watch_fixtures/A/A.go b/integration/_fixtures/watch_fixtures/A/A.go index de2c6bbb7..5e37c94f3 100644 --- a/integration/_fixtures/watch_fixtures/A/A.go +++ b/integration/_fixtures/watch_fixtures/A/A.go @@ -1,6 +1,6 @@ package A -import "github.com/onsi/B" +import "$ROOT_PATH$/B" func DoIt() string { return B.DoIt() diff --git a/integration/_fixtures/watch_fixtures/A/A_test.go b/integration/_fixtures/watch_fixtures/A/A_test.go index 003530aae..69dde9f4d 100644 --- a/integration/_fixtures/watch_fixtures/A/A_test.go +++ b/integration/_fixtures/watch_fixtures/A/A_test.go @@ -1,7 +1,7 @@ package A_test import ( - . "github.com/onsi/ginkgo/integration/_fixtures/watch_fixtures/A" + . "$ROOT_PATH$/A" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/integration/_fixtures/watch_fixtures/B/B.go b/integration/_fixtures/watch_fixtures/B/B.go index 990bab365..855b93221 100644 --- a/integration/_fixtures/watch_fixtures/B/B.go +++ b/integration/_fixtures/watch_fixtures/B/B.go @@ -1,6 +1,6 @@ package B -import "github.com/onsi/C" +import "$ROOT_PATH$/C" func DoIt() string { return C.DoIt() diff --git a/integration/_fixtures/watch_fixtures/B/B_test.go b/integration/_fixtures/watch_fixtures/B/B_test.go index b147913c0..d4620b3eb 100644 --- a/integration/_fixtures/watch_fixtures/B/B_test.go +++ b/integration/_fixtures/watch_fixtures/B/B_test.go @@ -1,7 +1,7 @@ package B_test import ( - . "github.com/onsi/ginkgo/integration/_fixtures/watch_fixtures/B" + . "$ROOT_PATH$/B" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/integration/_fixtures/watch_fixtures/C/C_test.go b/integration/_fixtures/watch_fixtures/C/C_test.go index 7703fefa3..a6b41ce63 100644 --- a/integration/_fixtures/watch_fixtures/C/C_test.go +++ b/integration/_fixtures/watch_fixtures/C/C_test.go @@ -1,7 +1,7 @@ package C_test import ( - . "github.com/onsi/ginkgo/integration/_fixtures/watch_fixtures/C" + . "$ROOT_PATH$/C" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/integration/_fixtures/watch_fixtures/D/D.go b/integration/_fixtures/watch_fixtures/D/D.go index 4371b852f..1c6f49517 100644 --- a/integration/_fixtures/watch_fixtures/D/D.go +++ b/integration/_fixtures/watch_fixtures/D/D.go @@ -1,6 +1,6 @@ package D -import "github.com/onsi/C" +import "$ROOT_PATH$/C" func DoIt() string { return C.DoIt() diff --git a/integration/_fixtures/watch_fixtures/D/D_test.go b/integration/_fixtures/watch_fixtures/D/D_test.go index 097945bf9..e4ff82daa 100644 --- a/integration/_fixtures/watch_fixtures/D/D_test.go +++ b/integration/_fixtures/watch_fixtures/D/D_test.go @@ -1,7 +1,7 @@ package D_test import ( - . "github.com/onsi/ginkgo/integration/_fixtures/watch_fixtures/C" + . "$ROOT_PATH$/D" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/integration/coverage_test.go b/integration/coverage_test.go index 3b4528ce5..962538cb1 100644 --- a/integration/coverage_test.go +++ b/integration/coverage_test.go @@ -18,6 +18,7 @@ var _ = Describe("Coverage Specs", func() { AfterEach(func() { removeSuccessfully("./_fixtures/coverage_fixture/coverage_fixture.coverprofile") }) + It("works", func() { session := startGinkgo("./_fixtures/coverage_fixture", "-cover") Eventually(session).Should(gexec.Exit(0)) diff --git a/integration/integration_suite_test.go b/integration/integration_suite_test.go index 3000e98c6..e4d8b4473 100644 --- a/integration/integration_suite_test.go +++ b/integration/integration_suite_test.go @@ -1,6 +1,7 @@ package integration_test import ( + "fmt" "io" "io/ioutil" "os" @@ -34,8 +35,8 @@ var _ = SynchronizedBeforeSuite(func() []byte { }) var _ = BeforeEach(func() { - var err error - tmpDir, err = ioutil.TempDir("", "ginkgo-run") + tmpDir = fmt.Sprintf("./ginko-run-%d", GinkgoParallelNode()) + err := os.Mkdir(tmpDir, 0700) Ω(err).ShouldNot(HaveOccurred()) }) @@ -45,6 +46,7 @@ var _ = AfterEach(func() { }) var _ = SynchronizedAfterSuite(func() {}, func() { + os.RemoveAll(tmpDir) gexec.CleanupBuildArtifacts() }) diff --git a/integration/run_test.go b/integration/run_test.go index 6c270b61b..c300e1c2c 100644 --- a/integration/run_test.go +++ b/integration/run_test.go @@ -50,7 +50,7 @@ var _ = Describe("Running Specs", func() { }) It("should run the ginkgo style tests", func() { - session := startGinkgo(tmpDir, "--noColor", pathToTest) + session := startGinkgo(tmpDir, "--noColor", "ginkgo") Eventually(session).Should(gexec.Exit(0)) output := string(session.Out.Contents()) diff --git a/integration/subcommand_test.go b/integration/subcommand_test.go index 1c2b35c94..f46777b69 100644 --- a/integration/subcommand_test.go +++ b/integration/subcommand_test.go @@ -98,7 +98,7 @@ var _ = Describe("Subcommand", func() { func Test{{.FormattedName}}(t *testing.T) { // This is a {{.Package}} test }`), 0666) - session := startGinkgo(pkgPath, "bootstrap", "--template", templateFile) + session := startGinkgo(pkgPath, "bootstrap", "--template", ".bootstrap") Eventually(session).Should(gexec.Exit(0)) output := session.Out.Contents() @@ -311,7 +311,7 @@ var _ = Describe("Subcommand", func() { Describe("ginkgo bootstrap/generate", func() { var pkgPath string BeforeEach(func() { - pkgPath = tmpPath("some crazy-thing") + pkgPath = tmpPath("some-crazy-thing") os.Mkdir(pkgPath, 0777) }) diff --git a/integration/verbose_and_succinct_test.go b/integration/verbose_and_succinct_test.go index 8238762d1..9b78f0b6c 100644 --- a/integration/verbose_and_succinct_test.go +++ b/integration/verbose_and_succinct_test.go @@ -45,7 +45,7 @@ var _ = Describe("Verbose And Succinct Mode", func() { Context("with no flags set", func() { It("should default to succinct mode", func() { - session := startGinkgo(pathToTest, "--noColor", pathToTest, otherPathToTest) + session := startGinkgo(tmpDir, "--noColor", "ginkgo", "more_ginkgo") Eventually(session).Should(gexec.Exit(0)) output := session.Out.Contents() @@ -56,7 +56,7 @@ var _ = Describe("Verbose And Succinct Mode", func() { Context("with --succinct=false", func() { It("should not be in succinct mode", func() { - session := startGinkgo(pathToTest, "--noColor", "--succinct=false", pathToTest, otherPathToTest) + session := startGinkgo(tmpDir, "--noColor", "--succinct=false", "ginkgo", "more_ginkgo") Eventually(session).Should(gexec.Exit(0)) output := session.Out.Contents() @@ -67,7 +67,7 @@ var _ = Describe("Verbose And Succinct Mode", func() { Context("with -v", func() { It("should not be in succinct mode, but should be verbose", func() { - session := startGinkgo(pathToTest, "--noColor", "-v", pathToTest, otherPathToTest) + session := startGinkgo(tmpDir, "--noColor", "-v", "ginkgo", "more_ginkgo") Eventually(session).Should(gexec.Exit(0)) output := session.Out.Contents() @@ -78,7 +78,7 @@ var _ = Describe("Verbose And Succinct Mode", func() { }) It("should emit output from Bys", func() { - session := startGinkgo(pathToTest, "--noColor", "-v", pathToTest) + session := startGinkgo(tmpDir, "--noColor", "-v", "ginkgo") Eventually(session).Should(gexec.Exit(0)) output := session.Out.Contents() diff --git a/integration/watch_test.go b/integration/watch_test.go index 4f711c123..6bbbe65b7 100644 --- a/integration/watch_test.go +++ b/integration/watch_test.go @@ -1,10 +1,10 @@ package integration_test import ( - "go/build" "io/ioutil" "os" "path/filepath" + "strings" "time" . "github.com/onsi/ginkgo" @@ -20,11 +20,31 @@ var _ = Describe("Watch", func() { var pathC string var session *gexec.Session + fixUpImportPath := func(path string) { + files, err := ioutil.ReadDir(path) + Expect(err).NotTo(HaveOccurred()) + + for _, f := range files { + filePath := filepath.Join(path, f.Name()) + + r, err := os.Open(filePath) + Ω(err).ShouldNot(HaveOccurred()) + + src, err := ioutil.ReadAll(r) + Ω(err).ShouldNot(HaveOccurred()) + out := strings.ReplaceAll(string(src), "$ROOT_PATH$", "github.com/onsi/ginkgo/integration/"+rootPath) + r.Close() + + err = ioutil.WriteFile(filePath, []byte(out), 0666) + Ω(err).ShouldNot(HaveOccurred()) + } + } + BeforeEach(func() { rootPath = tmpPath("root") - pathA = filepath.Join(rootPath, "src", "github.com", "onsi", "A") - pathB = filepath.Join(rootPath, "src", "github.com", "onsi", "B") - pathC = filepath.Join(rootPath, "src", "github.com", "onsi", "C") + pathA = filepath.Join(rootPath, "A") + pathB = filepath.Join(rootPath, "B") + pathC = filepath.Join(rootPath, "C") err := os.MkdirAll(pathA, 0700) Ω(err).ShouldNot(HaveOccurred()) @@ -36,18 +56,13 @@ var _ = Describe("Watch", func() { Ω(err).ShouldNot(HaveOccurred()) copyIn(fixturePath(filepath.Join("watch_fixtures", "A")), pathA, false) + fixUpImportPath(pathA) copyIn(fixturePath(filepath.Join("watch_fixtures", "B")), pathB, false) + fixUpImportPath(pathB) copyIn(fixturePath(filepath.Join("watch_fixtures", "C")), pathC, false) + fixUpImportPath(pathC) }) - startGinkgoWithGopath := func(args ...string) *gexec.Session { - cmd := ginkgoCommand(rootPath, args...) - os.Setenv("GOPATH", rootPath+":"+build.Default.GOPATH) - session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) - Ω(err).ShouldNot(HaveOccurred()) - return session - } - modifyFile := func(path string) { time.Sleep(time.Second) content, err := ioutil.ReadFile(path) @@ -58,15 +73,15 @@ var _ = Describe("Watch", func() { } modifyCode := func(pkgToModify string) { - modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+".go")) + modifyFile(filepath.Join(rootPath, pkgToModify, pkgToModify+".go")) } modifyJSON := func(pkgToModify string) { - modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+".json")) + modifyFile(filepath.Join(rootPath, pkgToModify, pkgToModify+".json")) } modifyTest := func(pkgToModify string) { - modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+"_test.go")) + modifyFile(filepath.Join(rootPath, pkgToModify, pkgToModify+"_test.go")) } AfterEach(func() { @@ -76,7 +91,7 @@ var _ = Describe("Watch", func() { }) It("should be set up correctly", func() { - session = startGinkgoWithGopath("-r") + session = startGinkgo(rootPath, "-r") Eventually(session).Should(gexec.Exit(0)) Ω(session.Out.Contents()).Should(ContainSubstring("A Suite")) Ω(session.Out.Contents()).Should(ContainSubstring("B Suite")) @@ -86,7 +101,7 @@ var _ = Describe("Watch", func() { Context("when watching just one test suite", func() { It("should immediately run, and should rerun when the test suite changes", func() { - session = startGinkgoWithGopath("watch", "-succinct", pathA) + session = startGinkgo(rootPath, "watch", "-succinct", "A") Eventually(session).Should(gbytes.Say("A Suite")) modifyCode("A") Eventually(session).Should(gbytes.Say("Detected changes in")) @@ -97,7 +112,7 @@ var _ = Describe("Watch", func() { Context("when watching several test suites", func() { It("should not immediately run, but should rerun a test when its code changes", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r") + session = startGinkgo(rootPath, "watch", "-succinct", "-r") Eventually(session).Should(gbytes.Say("Identified 3 test suites")) Consistently(session).ShouldNot(gbytes.Say("A Suite|B Suite|C Suite")) modifyCode("A") @@ -111,11 +126,11 @@ var _ = Describe("Watch", func() { Describe("watching dependencies", func() { Context("with a depth of 2", func() { It("should watch down to that depth", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2") + session = startGinkgo(rootPath, "watch", "-succinct", "-r", "-depth=2") Eventually(session).Should(gbytes.Say("Identified 3 test suites")) - Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`)) - Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`)) - Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`)) + Eventually(session).Should(gbytes.Say(`A \[`)) + Eventually(session).Should(gbytes.Say(`B \[`)) + Eventually(session).Should(gbytes.Say(`C \[`)) modifyCode("A") Eventually(session).Should(gbytes.Say("Detected changes in")) @@ -138,11 +153,11 @@ var _ = Describe("Watch", func() { Context("with a depth of 1", func() { It("should watch down to that depth", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=1") + session = startGinkgo(rootPath, "watch", "-succinct", "-r", "-depth=1") Eventually(session).Should(gbytes.Say("Identified 3 test suites")) - Eventually(session).Should(gbytes.Say(`A \[1 dependency\]`)) - Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`)) - Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`)) + Eventually(session).Should(gbytes.Say(`A \[`)) + Eventually(session).Should(gbytes.Say(`B \[`)) + Eventually(session).Should(gbytes.Say(`C \[`)) modifyCode("A") Eventually(session).Should(gbytes.Say("Detected changes in")) @@ -165,11 +180,11 @@ var _ = Describe("Watch", func() { Context("with a depth of 0", func() { It("should not watch any dependencies", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=0") + session = startGinkgo(rootPath, "watch", "-succinct", "-r", "-depth=0") Eventually(session).Should(gbytes.Say("Identified 3 test suites")) - Eventually(session).Should(gbytes.Say(`A \[0 dependencies\]`)) - Eventually(session).Should(gbytes.Say(`B \[0 dependencies\]`)) - Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`)) + Eventually(session).Should(gbytes.Say(`A \[`)) + Eventually(session).Should(gbytes.Say(`B \[`)) + Eventually(session).Should(gbytes.Say(`C \[`)) modifyCode("A") Eventually(session).Should(gbytes.Say("Detected changes in")) @@ -189,11 +204,11 @@ var _ = Describe("Watch", func() { }) It("should not trigger dependents when tests are changed", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2") + session = startGinkgo(rootPath, "watch", "-succinct", "-r", "-depth=2") Eventually(session).Should(gbytes.Say("Identified 3 test suites")) - Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`)) - Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`)) - Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`)) + Eventually(session).Should(gbytes.Say(`A \[`)) + Eventually(session).Should(gbytes.Say(`B \[`)) + Eventually(session).Should(gbytes.Say(`C \[`)) modifyTest("A") Eventually(session).Should(gbytes.Say("Detected changes in")) @@ -215,11 +230,11 @@ var _ = Describe("Watch", func() { Describe("adjusting the watch regular expression", func() { Describe("the default regular expression", func() { It("should only trigger when go files are changed", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2") + session = startGinkgo(rootPath, "watch", "-succinct", "-r", "-depth=2") Eventually(session).Should(gbytes.Say("Identified 3 test suites")) - Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`)) - Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`)) - Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`)) + Eventually(session).Should(gbytes.Say(`A \[`)) + Eventually(session).Should(gbytes.Say(`B \[`)) + Eventually(session).Should(gbytes.Say(`C \[`)) modifyJSON("C") Consistently(session).ShouldNot(gbytes.Say("Detected changes in")) @@ -229,11 +244,11 @@ var _ = Describe("Watch", func() { Describe("modifying the regular expression", func() { It("should trigger if the regexp matches", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2", `-watchRegExp=\.json$`) + session = startGinkgo(rootPath, "watch", "-succinct", "-r", "-depth=2", `-watchRegExp=\.json$`) Eventually(session).Should(gbytes.Say("Identified 3 test suites")) - Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`)) - Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`)) - Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`)) + Eventually(session).Should(gbytes.Say(`A \[`)) + Eventually(session).Should(gbytes.Say(`B \[`)) + Eventually(session).Should(gbytes.Say(`C \[`)) modifyJSON("C") Eventually(session).Should(gbytes.Say("Detected changes in")) @@ -246,19 +261,20 @@ var _ = Describe("Watch", func() { Describe("when new test suite is added", func() { It("should start monitoring that test suite", func() { - session = startGinkgoWithGopath("watch", "-succinct", "-r") + session = startGinkgo(rootPath, "watch", "-succinct", "-r") Eventually(session).Should(gbytes.Say("Watching 3 suites")) - pathD := filepath.Join(rootPath, "src", "github.com", "onsi", "D") + pathD := filepath.Join(rootPath, "D") err := os.MkdirAll(pathD, 0700) Ω(err).ShouldNot(HaveOccurred()) copyIn(fixturePath(filepath.Join("watch_fixtures", "D")), pathD, false) + fixUpImportPath(pathD) Eventually(session).Should(gbytes.Say("Detected 1 new suite")) - Eventually(session).Should(gbytes.Say(`D \[1 dependency\]`)) + Eventually(session).Should(gbytes.Say(`D \[`)) Eventually(session).Should(gbytes.Say("D Suite")) modifyCode("D")