1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
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.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.Err.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>"))
})
})
})
|