1package integration_test
2
3import (
4	"io/ioutil"
5	"os"
6	"path/filepath"
7	"strings"
8
9	. "github.com/onsi/ginkgo"
10	"github.com/onsi/ginkgo/types"
11	. "github.com/onsi/gomega"
12	"github.com/onsi/gomega/gexec"
13)
14
15var _ = Describe("Subcommand", func() {
16	Describe("ginkgo bootstrap", func() {
17		var pkgPath string
18		BeforeEach(func() {
19			pkgPath = tmpPath("foo")
20			os.Mkdir(pkgPath, 0777)
21		})
22
23		It("should generate a bootstrap file, as long as one does not exist", func() {
24			session := startGinkgo(pkgPath, "bootstrap")
25			Eventually(session).Should(gexec.Exit(0))
26			output := session.Out.Contents()
27
28			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
29
30			content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
31			Ω(err).ShouldNot(HaveOccurred())
32			Ω(content).Should(ContainSubstring("package foo_test"))
33			Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
34			Ω(content).Should(ContainSubstring("RegisterFailHandler"))
35			Ω(content).Should(ContainSubstring("RunSpecs"))
36
37			Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
38			Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
39
40			session = startGinkgo(pkgPath, "bootstrap")
41			Eventually(session).Should(gexec.Exit(1))
42			output = session.Out.Contents()
43			Ω(output).Should(ContainSubstring("foo_suite_test.go already exists"))
44		})
45
46		It("should import nodot declarations when told to", func() {
47			session := startGinkgo(pkgPath, "bootstrap", "--nodot")
48			Eventually(session).Should(gexec.Exit(0))
49			output := session.Out.Contents()
50
51			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
52
53			content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
54			Ω(err).ShouldNot(HaveOccurred())
55			Ω(content).Should(ContainSubstring("package foo_test"))
56			Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
57			Ω(content).Should(ContainSubstring("RegisterFailHandler"))
58			Ω(content).Should(ContainSubstring("RunSpecs"))
59
60			Ω(content).Should(ContainSubstring("var It = ginkgo.It"))
61			Ω(content).Should(ContainSubstring("var Ω = gomega.Ω"))
62
63			Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/ginkgo"`))
64			Ω(content).Should(ContainSubstring("\t" + `"github.com/onsi/gomega"`))
65		})
66
67		It("should generate an agouti bootstrap file when told to", func() {
68			session := startGinkgo(pkgPath, "bootstrap", "--agouti")
69			Eventually(session).Should(gexec.Exit(0))
70			output := session.Out.Contents()
71
72			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
73
74			content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
75			Ω(err).ShouldNot(HaveOccurred())
76			Ω(content).Should(ContainSubstring("package foo_test"))
77			Ω(content).Should(ContainSubstring("func TestFoo(t *testing.T) {"))
78			Ω(content).Should(ContainSubstring("RegisterFailHandler"))
79			Ω(content).Should(ContainSubstring("RunSpecs"))
80
81			Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
82			Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
83			Ω(content).Should(ContainSubstring("\t" + `"github.com/sclevine/agouti"`))
84		})
85
86		It("should generate a bootstrap file using a template when told to", func() {
87			templateFile := filepath.Join(pkgPath, ".bootstrap")
88			ioutil.WriteFile(templateFile, []byte(`package {{.Package}}
89
90			import (
91				{{.GinkgoImport}}
92				{{.GomegaImport}}
93
94				"testing"
95				"binary"
96			)
97
98			func Test{{.FormattedName}}(t *testing.T) {
99				// This is a {{.Package}} test
100			}`), 0666)
101			session := startGinkgo(pkgPath, "bootstrap", "--template", templateFile)
102			Eventually(session).Should(gexec.Exit(0))
103			output := session.Out.Contents()
104
105			Ω(output).Should(ContainSubstring("foo_suite_test.go"))
106
107			content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
108			Ω(err).ShouldNot(HaveOccurred())
109			Ω(content).Should(ContainSubstring("package foo_test"))
110			Ω(content).Should(ContainSubstring(`. "github.com/onsi/ginkgo"`))
111			Ω(content).Should(ContainSubstring(`. "github.com/onsi/gomega"`))
112			Ω(content).Should(ContainSubstring(`"binary"`))
113			Ω(content).Should(ContainSubstring("// This is a foo_test test"))
114		})
115	})
116
117	Describe("nodot", func() {
118		It("should update the declarations in the bootstrap file", func() {
119			pkgPath := tmpPath("foo")
120			os.Mkdir(pkgPath, 0777)
121
122			session := startGinkgo(pkgPath, "bootstrap", "--nodot")
123			Eventually(session).Should(gexec.Exit(0))
124
125			byteContent, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
126			Ω(err).ShouldNot(HaveOccurred())
127
128			content := string(byteContent)
129			content = strings.Replace(content, "var It =", "var MyIt =", -1)
130			content = strings.Replace(content, "var Ω = gomega.Ω\n", "", -1)
131
132			err = ioutil.WriteFile(filepath.Join(pkgPath, "foo_suite_test.go"), []byte(content), os.ModePerm)
133			Ω(err).ShouldNot(HaveOccurred())
134
135			session = startGinkgo(pkgPath, "nodot")
136			Eventually(session).Should(gexec.Exit(0))
137
138			byteContent, err = ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
139			Ω(err).ShouldNot(HaveOccurred())
140
141			Ω(byteContent).Should(ContainSubstring("var MyIt = ginkgo.It"))
142			Ω(byteContent).ShouldNot(ContainSubstring("var It = ginkgo.It"))
143			Ω(byteContent).Should(ContainSubstring("var Ω = gomega.Ω"))
144		})
145	})
146
147	Describe("ginkgo generate", func() {
148		var pkgPath string
149
150		BeforeEach(func() {
151			pkgPath = tmpPath("foo_bar")
152			os.Mkdir(pkgPath, 0777)
153		})
154
155		Context("with no arguments", func() {
156			It("should generate a test file named after the package", func() {
157				session := startGinkgo(pkgPath, "generate")
158				Eventually(session).Should(gexec.Exit(0))
159				output := session.Out.Contents()
160
161				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
162
163				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
164				Ω(err).ShouldNot(HaveOccurred())
165				Ω(content).Should(ContainSubstring("package foo_bar_test"))
166				Ω(content).Should(ContainSubstring(`var _ = Describe("FooBar", func() {`))
167				Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
168				Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
169
170				session = startGinkgo(pkgPath, "generate")
171				Eventually(session).Should(gexec.Exit(1))
172				output = session.Out.Contents()
173
174				Ω(output).Should(ContainSubstring("foo_bar_test.go already exists"))
175			})
176		})
177
178		Context("with an argument of the form: foo", func() {
179			It("should generate a test file named after the argument", func() {
180				session := startGinkgo(pkgPath, "generate", "baz_buzz")
181				Eventually(session).Should(gexec.Exit(0))
182				output := session.Out.Contents()
183
184				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
185
186				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
187				Ω(err).ShouldNot(HaveOccurred())
188				Ω(content).Should(ContainSubstring("package foo_bar_test"))
189				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
190			})
191		})
192
193		Context("with an argument of the form: foo.go", func() {
194			It("should generate a test file named after the argument", func() {
195				session := startGinkgo(pkgPath, "generate", "baz_buzz.go")
196				Eventually(session).Should(gexec.Exit(0))
197				output := session.Out.Contents()
198
199				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
200
201				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
202				Ω(err).ShouldNot(HaveOccurred())
203				Ω(content).Should(ContainSubstring("package foo_bar_test"))
204				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
205
206			})
207		})
208
209		Context("with an argument of the form: foo_test", func() {
210			It("should generate a test file named after the argument", func() {
211				session := startGinkgo(pkgPath, "generate", "baz_buzz_test")
212				Eventually(session).Should(gexec.Exit(0))
213				output := session.Out.Contents()
214
215				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
216
217				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
218				Ω(err).ShouldNot(HaveOccurred())
219				Ω(content).Should(ContainSubstring("package foo_bar_test"))
220				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
221			})
222		})
223
224		Context("with an argument of the form: foo_test.go", func() {
225			It("should generate a test file named after the argument", func() {
226				session := startGinkgo(pkgPath, "generate", "baz_buzz_test.go")
227				Eventually(session).Should(gexec.Exit(0))
228				output := session.Out.Contents()
229
230				Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
231
232				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
233				Ω(err).ShouldNot(HaveOccurred())
234				Ω(content).Should(ContainSubstring("package foo_bar_test"))
235				Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
236			})
237		})
238
239		Context("with multiple arguments", func() {
240			It("should generate a test file named after the argument", func() {
241				session := startGinkgo(pkgPath, "generate", "baz", "buzz")
242				Eventually(session).Should(gexec.Exit(0))
243				output := session.Out.Contents()
244
245				Ω(output).Should(ContainSubstring("baz_test.go"))
246				Ω(output).Should(ContainSubstring("buzz_test.go"))
247
248				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_test.go"))
249				Ω(err).ShouldNot(HaveOccurred())
250				Ω(content).Should(ContainSubstring("package foo_bar_test"))
251				Ω(content).Should(ContainSubstring(`var _ = Describe("Baz", func() {`))
252
253				content, err = ioutil.ReadFile(filepath.Join(pkgPath, "buzz_test.go"))
254				Ω(err).ShouldNot(HaveOccurred())
255				Ω(content).Should(ContainSubstring("package foo_bar_test"))
256				Ω(content).Should(ContainSubstring(`var _ = Describe("Buzz", func() {`))
257			})
258		})
259
260		Context("with nodot", func() {
261			It("should not import ginkgo or gomega", func() {
262				session := startGinkgo(pkgPath, "generate", "--nodot")
263				Eventually(session).Should(gexec.Exit(0))
264				output := session.Out.Contents()
265
266				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
267
268				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
269				Ω(err).ShouldNot(HaveOccurred())
270				Ω(content).Should(ContainSubstring("package foo_bar_test"))
271				Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
272				Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
273			})
274		})
275
276		Context("with agouti", func() {
277			It("should generate an agouti test file", func() {
278				session := startGinkgo(pkgPath, "generate", "--agouti")
279				Eventually(session).Should(gexec.Exit(0))
280				output := session.Out.Contents()
281
282				Ω(output).Should(ContainSubstring("foo_bar_test.go"))
283
284				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
285				Ω(err).ShouldNot(HaveOccurred())
286				Ω(content).Should(ContainSubstring("package foo_bar_test"))
287				Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
288				Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
289				Ω(content).Should(ContainSubstring("\t" + `. "github.com/sclevine/agouti/matchers"`))
290				Ω(content).Should(ContainSubstring("\t" + `"github.com/sclevine/agouti"`))
291				Ω(content).Should(ContainSubstring("page, err = agoutiDriver.NewPage()"))
292			})
293		})
294	})
295
296	Describe("ginkgo bootstrap/generate", func() {
297		var pkgPath string
298		BeforeEach(func() {
299			pkgPath = tmpPath("some crazy-thing")
300			os.Mkdir(pkgPath, 0777)
301		})
302
303		Context("when the working directory is empty", func() {
304			It("generates correctly named bootstrap and generate files with a package name derived from the directory", func() {
305				session := startGinkgo(pkgPath, "bootstrap")
306				Eventually(session).Should(gexec.Exit(0))
307
308				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_suite_test.go"))
309				Ω(err).ShouldNot(HaveOccurred())
310				Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
311				Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
312
313				session = startGinkgo(pkgPath, "generate")
314				Eventually(session).Should(gexec.Exit(0))
315
316				content, err = ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_test.go"))
317				Ω(err).ShouldNot(HaveOccurred())
318				Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
319				Ω(content).Should(ContainSubstring("SomeCrazyThing"))
320			})
321		})
322
323		Context("when the working directory contains a file with a package name", func() {
324			BeforeEach(func() {
325				Ω(ioutil.WriteFile(filepath.Join(pkgPath, "foo.go"), []byte("package main\n\nfunc main() {}"), 0777)).Should(Succeed())
326			})
327
328			It("generates correctly named bootstrap and generate files with the package name", func() {
329				session := startGinkgo(pkgPath, "bootstrap")
330				Eventually(session).Should(gexec.Exit(0))
331
332				content, err := ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_suite_test.go"))
333				Ω(err).ShouldNot(HaveOccurred())
334				Ω(content).Should(ContainSubstring("package main_test"))
335				Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
336
337				session = startGinkgo(pkgPath, "generate")
338				Eventually(session).Should(gexec.Exit(0))
339
340				content, err = ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_test.go"))
341				Ω(err).ShouldNot(HaveOccurred())
342				Ω(content).Should(ContainSubstring("package main_test"))
343				Ω(content).Should(ContainSubstring("SomeCrazyThing"))
344			})
345		})
346	})
347
348	Describe("ginkgo blur", func() {
349		It("should unfocus tests", func() {
350			pathToTest := tmpPath("focused")
351			fixture := fixturePath("focused_fixture")
352			copyIn(fixture, pathToTest, false)
353
354			session := startGinkgo(pathToTest, "--noColor")
355			Eventually(session).Should(gexec.Exit(types.GINKGO_FOCUS_EXIT_CODE))
356			output := session.Out.Contents()
357
358			Ω(string(output)).Should(ContainSubstring("8 Passed"))
359			Ω(string(output)).Should(ContainSubstring("5 Skipped"))
360
361			session = startGinkgo(pathToTest, "blur")
362			Eventually(session).Should(gexec.Exit(0))
363			output = session.Out.Contents()
364			Ω(string(output)).ShouldNot(ContainSubstring("expected 'package'"))
365
366			session = startGinkgo(pathToTest, "--noColor")
367			Eventually(session).Should(gexec.Exit(0))
368			output = session.Out.Contents()
369			Ω(string(output)).Should(ContainSubstring("13 Passed"))
370			Ω(string(output)).Should(ContainSubstring("0 Skipped"))
371
372			Expect(sameFile(filepath.Join(pathToTest, "README.md"), filepath.Join(fixture, "README.md"))).To(BeTrue())
373		})
374
375		It("should ignore the 'vendor' folder", func() {
376			pathToTest := tmpPath("focused_fixture_with_vendor")
377			copyIn(fixturePath("focused_fixture_with_vendor"), pathToTest, true)
378
379			session := startGinkgo(pathToTest, "blur")
380			Eventually(session).Should(gexec.Exit(0))
381
382			session = startGinkgo(pathToTest, "--noColor")
383			Eventually(session).Should(gexec.Exit(0))
384			output := session.Out.Contents()
385			Expect(string(output)).To(ContainSubstring("13 Passed"))
386			Expect(string(output)).To(ContainSubstring("0 Skipped"))
387
388			vendorPath := fixturePath("focused_fixture_with_vendor/vendor")
389			otherVendorPath := filepath.Join(pathToTest, "vendor")
390
391			Expect(sameFolder(vendorPath, otherVendorPath)).To(BeTrue())
392		})
393	})
394
395	Describe("ginkgo version", func() {
396		It("should print out the version info", func() {
397			session := startGinkgo("", "version")
398			Eventually(session).Should(gexec.Exit(0))
399			output := session.Out.Contents()
400
401			Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
402		})
403	})
404
405	Describe("ginkgo help", func() {
406		It("should print out usage information", func() {
407			session := startGinkgo("", "help")
408			Eventually(session).Should(gexec.Exit(0))
409			output := string(session.Err.Contents())
410
411			Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
412			Ω(output).Should(ContainSubstring("ginkgo watch"))
413			Ω(output).Should(ContainSubstring("-succinct"))
414			Ω(output).Should(ContainSubstring("-nodes"))
415			Ω(output).Should(ContainSubstring("ginkgo generate"))
416			Ω(output).Should(ContainSubstring("ginkgo help <COMMAND>"))
417		})
418	})
419})
420