subcommand_test.go 16 KB

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