123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- package integration_test
- import (
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- . "github.com/onsi/ginkgo"
- "github.com/onsi/ginkgo/types"
- . "github.com/onsi/gomega"
- "github.com/onsi/gomega/gexec"
- )
- var _ = Describe("Subcommand", func() {
- Describe("ginkgo bootstrap", func() {
- var pkgPath string
- BeforeEach(func() {
- pkgPath = tmpPath("foo")
- os.Mkdir(pkgPath, 0777)
- })
- It("should generate a bootstrap file, as long as one does not exist", func() {
- session := startGinkgo(pkgPath, "bootstrap")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_suite_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_test"))
- Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
- Ω(content).Should(ContainSubstring("RegisterFailHandler"))
- Ω(content).Should(ContainSubstring("RunSpecs"))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
- session = startGinkgo(pkgPath, "bootstrap")
- Eventually(session).Should(gexec.Exit(1))
- output = session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_suite_test.go already exists"))
- })
- It("should import nodot declarations when told to", func() {
- session := startGinkgo(pkgPath, "bootstrap", "--nodot")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_suite_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_test"))
- Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
- Ω(content).Should(ContainSubstring("RegisterFailHandler"))
- Ω(content).Should(ContainSubstring("RunSpecs"))
- Ω(content).Should(ContainSubstring("var It = ginkgo.It"))
- Ω(content).Should(ContainSubstring("var Ω = gomega.Ω"))
- Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/ginkgo"`))
- Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/gomega"`))
- })
- It("should generate an agouti bootstrap file when told to", func() {
- session := startGinkgo(pkgPath, "bootstrap", "--agouti")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_suite_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_test"))
- Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
- Ω(content).Should(ContainSubstring("RegisterFailHandler"))
- Ω(content).Should(ContainSubstring("RunSpecs"))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
- Ω(content).Should(ContainSubstring("\t" + `"github.com/sclevine/agouti"`))
- })
- It("should generate a bootstrap file using a template when told to", func() {
- templateFile := filepath.Join(pkgPath, ".bootstrap")
- ioutil.WriteFile(templateFile, []byte(`package {{.Package}}
- import (
- {{.GinkgoImport}}
- {{.GomegaImport}}
- "testing"
- "binary"
- )
- func Test{{.FormattedName}}(t *testing.T) {
- // This is a {{.Package}} test
- }`), 0666)
- session := startGinkgo(pkgPath, "bootstrap", "--template", templateFile)
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_suite_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_test"))
- Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo"`))
- Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
- Ω(content).Should(ContainSubstring(`"binary"`))
- Ω(content).Should(ContainSubstring("// This is a foo_test test"))
- })
- })
- Describe("nodot", func() {
- It("should update the declarations in the bootstrap file", func() {
- pkgPath := tmpPath("foo")
- os.Mkdir(pkgPath, 0777)
- session := startGinkgo(pkgPath, "bootstrap", "--nodot")
- Eventually(session).Should(gexec.Exit(0))
- byteContent, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- content := string(byteContent)
- content = strings.Replace(content, "var It =", "var MyIt =", -1)
- content = strings.Replace(content, "var Ω = gomega.Ω\n", "", -1)
- err = ioutil.WriteFile(filepath.Join(pkgPath, "foo_suite_test.go"), []byte(content), os.ModePerm)
- Ω(err).ShouldNot(HaveOccurred())
- session = startGinkgo(pkgPath, "nodot")
- Eventually(session).Should(gexec.Exit(0))
- byteContent, err = ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(byteContent).Should(ContainSubstring("var MyIt = ginkgo.It"))
- Ω(byteContent).ShouldNot(ContainSubstring("var It = ginkgo.It"))
- Ω(byteContent).Should(ContainSubstring("var Ω = gomega.Ω"))
- })
- })
- Describe("ginkgo generate", func() {
- var pkgPath string
- BeforeEach(func() {
- pkgPath = tmpPath("foo_bar")
- os.Mkdir(pkgPath, 0777)
- })
- Context("with no arguments", func() {
- It("should generate a test file named after the package", func() {
- session := startGinkgo(pkgPath, "generate")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_bar_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("FooBar", func() {`))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
- session = startGinkgo(pkgPath, "generate")
- Eventually(session).Should(gexec.Exit(1))
- output = session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_bar_test.go already exists"))
- })
- })
- Context("with an argument of the form: foo", func() {
- It("should generate a test file named after the argument", func() {
- session := startGinkgo(pkgPath, "generate", "baz_buzz")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
- })
- })
- Context("with an argument of the form: foo.go", func() {
- It("should generate a test file named after the argument", func() {
- session := startGinkgo(pkgPath, "generate", "baz_buzz.go")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
- })
- })
- Context("with an argument of the form: foo_test", func() {
- It("should generate a test file named after the argument", func() {
- session := startGinkgo(pkgPath, "generate", "baz_buzz_test")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
- })
- })
- Context("with an argument of the form: foo-test", func() {
- It("should generate a test file named after the argument", func() {
- session := startGinkgo(pkgPath, "generate", "baz-buzz-test")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
- })
- })
- Context("with an argument of the form: foo_test.go", func() {
- It("should generate a test file named after the argument", func() {
- session := startGinkgo(pkgPath, "generate", "baz_buzz_test.go")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
- })
- })
- Context("with multiple arguments", func() {
- It("should generate a test file named after the argument", func() {
- session := startGinkgo(pkgPath, "generate", "baz", "buzz")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("baz_test.go"))
- Ω(output).Should(ContainSubstring("buzz_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("Baz", func() {`))
- content, err = ioutil.ReadFile(filepath.Join(pkgPath, "buzz_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring(`var _ = Describe("Buzz", func() {`))
- })
- })
- Context("with nodot", func() {
- It("should not import ginkgo or gomega", func() {
- session := startGinkgo(pkgPath, "generate", "--nodot")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_bar_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
- Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
- })
- })
- Context("with agouti", func() {
- It("should generate an agouti test file", func() {
- session := startGinkgo(pkgPath, "generate", "--agouti")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(ContainSubstring("foo_bar_test.go"))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package foo_bar_test"))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
- Ω(content).Should(ContainSubstring("\t" + `. "github.com/sclevine/agouti/matchers"`))
- Ω(content).Should(ContainSubstring("\t" + `"github.com/sclevine/agouti"`))
- Ω(content).Should(ContainSubstring("page, err = agoutiDriver.NewPage()"))
- })
- })
- })
- Describe("ginkgo bootstrap/generate", func() {
- var pkgPath string
- BeforeEach(func() {
- pkgPath = tmpPath("some crazy-thing")
- os.Mkdir(pkgPath, 0777)
- })
- Context("when the working directory is empty", func() {
- It("generates correctly named bootstrap and generate files with a package name derived from the directory", func() {
- session := startGinkgo(pkgPath, "bootstrap")
- Eventually(session).Should(gexec.Exit(0))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
- Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
- session = startGinkgo(pkgPath, "generate")
- Eventually(session).Should(gexec.Exit(0))
- content, err = ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
- Ω(content).Should(ContainSubstring("SomeCrazyThing"))
- })
- })
- Context("when the working directory contains a file with a package name", func() {
- BeforeEach(func() {
- Ω(ioutil.WriteFile(filepath.Join(pkgPath, "foo.go"), []byte("package main\n\nfunc main() {}"), 0777)).Should(Succeed())
- })
- It("generates correctly named bootstrap and generate files with the package name", func() {
- session := startGinkgo(pkgPath, "bootstrap")
- Eventually(session).Should(gexec.Exit(0))
- content, err := ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_suite_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package main_test"))
- Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
- session = startGinkgo(pkgPath, "generate")
- Eventually(session).Should(gexec.Exit(0))
- content, err = ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_test.go"))
- Ω(err).ShouldNot(HaveOccurred())
- Ω(content).Should(ContainSubstring("package main_test"))
- Ω(content).Should(ContainSubstring("SomeCrazyThing"))
- })
- })
- })
- Describe("ginkgo blur", func() {
- It("should unfocus tests", func() {
- pathToTest := tmpPath("focused")
- fixture := fixturePath("focused_fixture")
- copyIn(fixture, pathToTest, false)
- session := startGinkgo(pathToTest, "--noColor")
- Eventually(session).Should(gexec.Exit(types.GINKGO_FOCUS_EXIT_CODE))
- output := session.Out.Contents()
- Ω(string(output)).Should(ContainSubstring("8 Passed"))
- Ω(string(output)).Should(ContainSubstring("5 Skipped"))
- session = startGinkgo(pathToTest, "blur")
- Eventually(session).Should(gexec.Exit(0))
- output = session.Out.Contents()
- Ω(string(output)).ShouldNot(ContainSubstring("expected 'package'"))
- session = startGinkgo(pathToTest, "--noColor")
- Eventually(session).Should(gexec.Exit(0))
- output = session.Out.Contents()
- Ω(string(output)).Should(ContainSubstring("13 Passed"))
- Ω(string(output)).Should(ContainSubstring("0 Skipped"))
- Expect(sameFile(filepath.Join(pathToTest, "README.md"), filepath.Join(fixture, "README.md"))).To(BeTrue())
- })
- It("should ignore the 'vendor' folder", func() {
- pathToTest := tmpPath("focused_fixture_with_vendor")
- copyIn(fixturePath("focused_fixture_with_vendor"), pathToTest, true)
- session := startGinkgo(pathToTest, "blur")
- Eventually(session).Should(gexec.Exit(0))
- session = startGinkgo(pathToTest, "--noColor")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Expect(string(output)).To(ContainSubstring("13 Passed"))
- Expect(string(output)).To(ContainSubstring("0 Skipped"))
- vendorPath := fixturePath("focused_fixture_with_vendor/vendor")
- otherVendorPath := filepath.Join(pathToTest, "vendor")
- Expect(sameFolder(vendorPath, otherVendorPath)).To(BeTrue())
- })
- })
- Describe("ginkgo version", func() {
- It("should print out the version info", func() {
- session := startGinkgo("", "version")
- Eventually(session).Should(gexec.Exit(0))
- output := session.Out.Contents()
- Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
- })
- })
- Describe("ginkgo help", func() {
- It("should print out usage information", func() {
- session := startGinkgo("", "help")
- Eventually(session).Should(gexec.Exit(0))
- output := string(session.Out.Contents())
- Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
- Ω(output).Should(ContainSubstring("ginkgo watch"))
- Ω(output).Should(ContainSubstring("-succinct"))
- Ω(output).Should(ContainSubstring("-nodes"))
- Ω(output).Should(ContainSubstring("ginkgo generate"))
- Ω(output).Should(ContainSubstring("ginkgo help <COMMAND>"))
- })
- })
- })
|