subcommand_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. })
  69. Describe("nodot", func() {
  70. It("should update the declarations in the bootstrap file", func() {
  71. pkgPath := tmpPath("foo")
  72. os.Mkdir(pkgPath, 0777)
  73. session := startGinkgo(pkgPath, "bootstrap", "--nodot")
  74. Eventually(session).Should(gexec.Exit(0))
  75. byteContent, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
  76. Ω(err).ShouldNot(HaveOccurred())
  77. content := string(byteContent)
  78. content = strings.Replace(content, "var It =", "var MyIt =", -1)
  79. content = strings.Replace(content, "var Ω = gomega.Ω\n", "", -1)
  80. err = ioutil.WriteFile(filepath.Join(pkgPath, "foo_suite_test.go"), []byte(content), os.ModePerm)
  81. Ω(err).ShouldNot(HaveOccurred())
  82. session = startGinkgo(pkgPath, "nodot")
  83. Eventually(session).Should(gexec.Exit(0))
  84. byteContent, err = ioutil.ReadFile(filepath.Join(pkgPath, "foo_suite_test.go"))
  85. Ω(err).ShouldNot(HaveOccurred())
  86. Ω(byteContent).Should(ContainSubstring("var MyIt = ginkgo.It"))
  87. Ω(byteContent).ShouldNot(ContainSubstring("var It = ginkgo.It"))
  88. Ω(byteContent).Should(ContainSubstring("var Ω = gomega.Ω"))
  89. })
  90. })
  91. Describe("ginkgo generate", func() {
  92. var pkgPath string
  93. BeforeEach(func() {
  94. pkgPath = tmpPath("foo_bar")
  95. os.Mkdir(pkgPath, 0777)
  96. })
  97. Context("with no arguments", func() {
  98. It("should generate a test file named after the package", func() {
  99. session := startGinkgo(pkgPath, "generate")
  100. Eventually(session).Should(gexec.Exit(0))
  101. output := session.Out.Contents()
  102. Ω(output).Should(ContainSubstring("foo_bar_test.go"))
  103. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
  104. Ω(err).ShouldNot(HaveOccurred())
  105. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  106. Ω(content).Should(ContainSubstring(`var _ = Describe("FooBar", func() {`))
  107. Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
  108. Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
  109. session = startGinkgo(pkgPath, "generate")
  110. Eventually(session).Should(gexec.Exit(1))
  111. output = session.Out.Contents()
  112. Ω(output).Should(ContainSubstring("foo_bar_test.go already exists"))
  113. })
  114. })
  115. Context("with an argument of the form: foo", func() {
  116. It("should generate a test file named after the argument", func() {
  117. session := startGinkgo(pkgPath, "generate", "baz_buzz")
  118. Eventually(session).Should(gexec.Exit(0))
  119. output := session.Out.Contents()
  120. Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
  121. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
  122. Ω(err).ShouldNot(HaveOccurred())
  123. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  124. Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
  125. })
  126. })
  127. Context("with an argument of the form: foo.go", func() {
  128. It("should generate a test file named after the argument", func() {
  129. session := startGinkgo(pkgPath, "generate", "baz_buzz.go")
  130. Eventually(session).Should(gexec.Exit(0))
  131. output := session.Out.Contents()
  132. Ω(output).Should(ContainSubstring("baz_buzz_test.go"))
  133. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_buzz_test.go"))
  134. Ω(err).ShouldNot(HaveOccurred())
  135. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  136. Ω(content).Should(ContainSubstring(`var _ = Describe("BazBuzz", func() {`))
  137. })
  138. })
  139. Context("with an argument of the form: foo_test", func() {
  140. It("should generate a test file named after the argument", func() {
  141. session := startGinkgo(pkgPath, "generate", "baz_buzz_test")
  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_test.go", func() {
  152. It("should generate a test file named after the argument", func() {
  153. session := startGinkgo(pkgPath, "generate", "baz_buzz_test.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 multiple arguments", func() {
  164. It("should generate a test file named after the argument", func() {
  165. session := startGinkgo(pkgPath, "generate", "baz", "buzz")
  166. Eventually(session).Should(gexec.Exit(0))
  167. output := session.Out.Contents()
  168. Ω(output).Should(ContainSubstring("baz_test.go"))
  169. Ω(output).Should(ContainSubstring("buzz_test.go"))
  170. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "baz_test.go"))
  171. Ω(err).ShouldNot(HaveOccurred())
  172. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  173. Ω(content).Should(ContainSubstring(`var _ = Describe("Baz", func() {`))
  174. content, err = ioutil.ReadFile(filepath.Join(pkgPath, "buzz_test.go"))
  175. Ω(err).ShouldNot(HaveOccurred())
  176. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  177. Ω(content).Should(ContainSubstring(`var _ = Describe("Buzz", func() {`))
  178. })
  179. })
  180. Context("with nodot", func() {
  181. It("should not import ginkgo or gomega", func() {
  182. session := startGinkgo(pkgPath, "generate", "--nodot")
  183. Eventually(session).Should(gexec.Exit(0))
  184. output := session.Out.Contents()
  185. Ω(output).Should(ContainSubstring("foo_bar_test.go"))
  186. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
  187. Ω(err).ShouldNot(HaveOccurred())
  188. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  189. Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
  190. Ω(content).ShouldNot(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
  191. })
  192. })
  193. Context("with agouti", func() {
  194. It("should generate an agouti test file", func() {
  195. session := startGinkgo(pkgPath, "generate", "--agouti")
  196. Eventually(session).Should(gexec.Exit(0))
  197. output := session.Out.Contents()
  198. Ω(output).Should(ContainSubstring("foo_bar_test.go"))
  199. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "foo_bar_test.go"))
  200. Ω(err).ShouldNot(HaveOccurred())
  201. Ω(content).Should(ContainSubstring("package foo_bar_test"))
  202. Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/ginkgo"`))
  203. Ω(content).Should(ContainSubstring("\t" + `. "github.com/onsi/gomega"`))
  204. Ω(content).Should(ContainSubstring("\t" + `. "github.com/sclevine/agouti/matchers"`))
  205. Ω(content).Should(ContainSubstring("\t" + `"github.com/sclevine/agouti"`))
  206. Ω(content).Should(ContainSubstring("page, err = agoutiDriver.NewPage()"))
  207. })
  208. })
  209. })
  210. Describe("ginkgo bootstrap/generate", func() {
  211. var pkgPath string
  212. BeforeEach(func() {
  213. pkgPath = tmpPath("some crazy-thing")
  214. os.Mkdir(pkgPath, 0777)
  215. })
  216. Context("when the working directory is empty", func() {
  217. It("generates correctly named bootstrap and generate files with a package name derived from the directory", func() {
  218. session := startGinkgo(pkgPath, "bootstrap")
  219. Eventually(session).Should(gexec.Exit(0))
  220. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_suite_test.go"))
  221. Ω(err).ShouldNot(HaveOccurred())
  222. Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
  223. Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
  224. session = startGinkgo(pkgPath, "generate")
  225. Eventually(session).Should(gexec.Exit(0))
  226. content, err = ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_test.go"))
  227. Ω(err).ShouldNot(HaveOccurred())
  228. Ω(content).Should(ContainSubstring("package some_crazy_thing_test"))
  229. Ω(content).Should(ContainSubstring("SomeCrazyThing"))
  230. })
  231. })
  232. Context("when the working directory contains a file with a package name", func() {
  233. BeforeEach(func() {
  234. Ω(ioutil.WriteFile(filepath.Join(pkgPath, "foo.go"), []byte("package main\n\nfunc main() {}"), 0777)).Should(Succeed())
  235. })
  236. It("generates correctly named bootstrap and generate files with the package name", func() {
  237. session := startGinkgo(pkgPath, "bootstrap")
  238. Eventually(session).Should(gexec.Exit(0))
  239. content, err := ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_suite_test.go"))
  240. Ω(err).ShouldNot(HaveOccurred())
  241. Ω(content).Should(ContainSubstring("package main_test"))
  242. Ω(content).Should(ContainSubstring("SomeCrazyThing Suite"))
  243. session = startGinkgo(pkgPath, "generate")
  244. Eventually(session).Should(gexec.Exit(0))
  245. content, err = ioutil.ReadFile(filepath.Join(pkgPath, "some_crazy_thing_test.go"))
  246. Ω(err).ShouldNot(HaveOccurred())
  247. Ω(content).Should(ContainSubstring("package main_test"))
  248. Ω(content).Should(ContainSubstring("SomeCrazyThing"))
  249. })
  250. })
  251. })
  252. Describe("ginkgo blur", func() {
  253. It("should unfocus tests", func() {
  254. pathToTest := tmpPath("focused")
  255. copyIn("focused_fixture", pathToTest)
  256. session := startGinkgo(pathToTest, "--noColor")
  257. Eventually(session).Should(gexec.Exit(types.GINKGO_FOCUS_EXIT_CODE))
  258. output := session.Out.Contents()
  259. Ω(output).Should(ContainSubstring("6 Passed"))
  260. Ω(output).Should(ContainSubstring("5 Skipped"))
  261. session = startGinkgo(pathToTest, "blur")
  262. Eventually(session).Should(gexec.Exit(0))
  263. session = startGinkgo(pathToTest, "--noColor")
  264. Eventually(session).Should(gexec.Exit(0))
  265. output = session.Out.Contents()
  266. Ω(output).Should(ContainSubstring("11 Passed"))
  267. Ω(output).Should(ContainSubstring("0 Skipped"))
  268. })
  269. })
  270. Describe("ginkgo version", func() {
  271. It("should print out the version info", func() {
  272. session := startGinkgo("", "version")
  273. Eventually(session).Should(gexec.Exit(0))
  274. output := session.Out.Contents()
  275. Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
  276. })
  277. })
  278. Describe("ginkgo help", func() {
  279. It("should print out usage information", func() {
  280. session := startGinkgo("", "help")
  281. Eventually(session).Should(gexec.Exit(0))
  282. output := string(session.Err.Contents())
  283. Ω(output).Should(MatchRegexp(`Ginkgo Version \d+\.\d+\.\d+`))
  284. Ω(output).Should(ContainSubstring("ginkgo watch"))
  285. Ω(output).Should(ContainSubstring("-succinct"))
  286. Ω(output).Should(ContainSubstring("-nodes"))
  287. Ω(output).Should(ContainSubstring("ginkgo generate"))
  288. })
  289. })
  290. })