watch_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package integration_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. "github.com/onsi/gomega/gbytes"
  10. "github.com/onsi/gomega/gexec"
  11. )
  12. var _ = Describe("Watch", func() {
  13. var rootPath string
  14. var pathA string
  15. var pathB string
  16. var pathC string
  17. var session *gexec.Session
  18. BeforeEach(func() {
  19. rootPath = tmpPath("root")
  20. pathA = filepath.Join(rootPath, "src", "github.com", "onsi", "A")
  21. pathB = filepath.Join(rootPath, "src", "github.com", "onsi", "B")
  22. pathC = filepath.Join(rootPath, "src", "github.com", "onsi", "C")
  23. err := os.MkdirAll(pathA, 0700)
  24. Ω(err).ShouldNot(HaveOccurred())
  25. err = os.MkdirAll(pathB, 0700)
  26. Ω(err).ShouldNot(HaveOccurred())
  27. err = os.MkdirAll(pathC, 0700)
  28. Ω(err).ShouldNot(HaveOccurred())
  29. copyIn(fixturePath(filepath.Join("watch_fixtures", "A")), pathA, false)
  30. copyIn(fixturePath(filepath.Join("watch_fixtures", "B")), pathB, false)
  31. copyIn(fixturePath(filepath.Join("watch_fixtures", "C")), pathC, false)
  32. })
  33. startGinkgoWithGopath := func(args ...string) *gexec.Session {
  34. cmd := ginkgoCommand(rootPath, args...)
  35. os.Setenv("GOPATH", rootPath+":"+os.Getenv("GOPATH"))
  36. session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
  37. Ω(err).ShouldNot(HaveOccurred())
  38. return session
  39. }
  40. modifyFile := func(path string) {
  41. time.Sleep(time.Second)
  42. content, err := ioutil.ReadFile(path)
  43. Ω(err).ShouldNot(HaveOccurred())
  44. content = append(content, []byte("//")...)
  45. err = ioutil.WriteFile(path, content, 0666)
  46. Ω(err).ShouldNot(HaveOccurred())
  47. }
  48. modifyCode := func(pkgToModify string) {
  49. modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+".go"))
  50. }
  51. modifyJSON := func(pkgToModify string) {
  52. modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+".json"))
  53. }
  54. modifyTest := func(pkgToModify string) {
  55. modifyFile(filepath.Join(rootPath, "src", "github.com", "onsi", pkgToModify, pkgToModify+"_test.go"))
  56. }
  57. AfterEach(func() {
  58. if session != nil {
  59. session.Kill().Wait()
  60. }
  61. })
  62. It("should be set up correctly", func() {
  63. session = startGinkgoWithGopath("-r")
  64. Eventually(session).Should(gexec.Exit(0))
  65. Ω(session.Out.Contents()).Should(ContainSubstring("A Suite"))
  66. Ω(session.Out.Contents()).Should(ContainSubstring("B Suite"))
  67. Ω(session.Out.Contents()).Should(ContainSubstring("C Suite"))
  68. Ω(session.Out.Contents()).Should(ContainSubstring("Ginkgo ran 3 suites"))
  69. })
  70. Context("when watching just one test suite", func() {
  71. It("should immediately run, and should rerun when the test suite changes", func() {
  72. session = startGinkgoWithGopath("watch", "-succinct", pathA)
  73. Eventually(session).Should(gbytes.Say("A Suite"))
  74. modifyCode("A")
  75. Eventually(session).Should(gbytes.Say("Detected changes in"))
  76. Eventually(session).Should(gbytes.Say("A Suite"))
  77. session.Kill().Wait()
  78. })
  79. })
  80. Context("when watching several test suites", func() {
  81. It("should not immediately run, but should rerun a test when its code changes", func() {
  82. session = startGinkgoWithGopath("watch", "-succinct", "-r")
  83. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  84. Consistently(session).ShouldNot(gbytes.Say("A Suite|B Suite|C Suite"))
  85. modifyCode("A")
  86. Eventually(session).Should(gbytes.Say("Detected changes in"))
  87. Eventually(session).Should(gbytes.Say("A Suite"))
  88. Consistently(session).ShouldNot(gbytes.Say("B Suite|C Suite"))
  89. session.Kill().Wait()
  90. })
  91. })
  92. Describe("watching dependencies", func() {
  93. Context("with a depth of 2", func() {
  94. It("should watch down to that depth", func() {
  95. session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2")
  96. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  97. Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`))
  98. Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
  99. Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))
  100. modifyCode("A")
  101. Eventually(session).Should(gbytes.Say("Detected changes in"))
  102. Eventually(session).Should(gbytes.Say("A Suite"))
  103. Consistently(session).ShouldNot(gbytes.Say("B Suite|C Suite"))
  104. modifyCode("B")
  105. Eventually(session).Should(gbytes.Say("Detected changes in"))
  106. Eventually(session).Should(gbytes.Say("B Suite"))
  107. Eventually(session).Should(gbytes.Say("A Suite"))
  108. Consistently(session).ShouldNot(gbytes.Say("C Suite"))
  109. modifyCode("C")
  110. Eventually(session).Should(gbytes.Say("Detected changes in"))
  111. Eventually(session).Should(gbytes.Say("C Suite"))
  112. Eventually(session).Should(gbytes.Say("B Suite"))
  113. Eventually(session).Should(gbytes.Say("A Suite"))
  114. })
  115. })
  116. Context("with a depth of 1", func() {
  117. It("should watch down to that depth", func() {
  118. session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=1")
  119. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  120. Eventually(session).Should(gbytes.Say(`A \[1 dependency\]`))
  121. Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
  122. Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))
  123. modifyCode("A")
  124. Eventually(session).Should(gbytes.Say("Detected changes in"))
  125. Eventually(session).Should(gbytes.Say("A Suite"))
  126. Consistently(session).ShouldNot(gbytes.Say("B Suite|C Suite"))
  127. modifyCode("B")
  128. Eventually(session).Should(gbytes.Say("Detected changes in"))
  129. Eventually(session).Should(gbytes.Say("B Suite"))
  130. Eventually(session).Should(gbytes.Say("A Suite"))
  131. Consistently(session).ShouldNot(gbytes.Say("C Suite"))
  132. modifyCode("C")
  133. Eventually(session).Should(gbytes.Say("Detected changes in"))
  134. Eventually(session).Should(gbytes.Say("C Suite"))
  135. Eventually(session).Should(gbytes.Say("B Suite"))
  136. Consistently(session).ShouldNot(gbytes.Say("A Suite"))
  137. })
  138. })
  139. Context("with a depth of 0", func() {
  140. It("should not watch any dependencies", func() {
  141. session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=0")
  142. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  143. Eventually(session).Should(gbytes.Say(`A \[0 dependencies\]`))
  144. Eventually(session).Should(gbytes.Say(`B \[0 dependencies\]`))
  145. Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))
  146. modifyCode("A")
  147. Eventually(session).Should(gbytes.Say("Detected changes in"))
  148. Eventually(session).Should(gbytes.Say("A Suite"))
  149. Consistently(session).ShouldNot(gbytes.Say("B Suite|C Suite"))
  150. modifyCode("B")
  151. Eventually(session).Should(gbytes.Say("Detected changes in"))
  152. Eventually(session).Should(gbytes.Say("B Suite"))
  153. Consistently(session).ShouldNot(gbytes.Say("A Suite|C Suite"))
  154. modifyCode("C")
  155. Eventually(session).Should(gbytes.Say("Detected changes in"))
  156. Eventually(session).Should(gbytes.Say("C Suite"))
  157. Consistently(session).ShouldNot(gbytes.Say("A Suite|B Suite"))
  158. })
  159. })
  160. It("should not trigger dependents when tests are changed", func() {
  161. session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2")
  162. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  163. Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`))
  164. Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
  165. Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))
  166. modifyTest("A")
  167. Eventually(session).Should(gbytes.Say("Detected changes in"))
  168. Eventually(session).Should(gbytes.Say("A Suite"))
  169. Consistently(session).ShouldNot(gbytes.Say("B Suite|C Suite"))
  170. modifyTest("B")
  171. Eventually(session).Should(gbytes.Say("Detected changes in"))
  172. Eventually(session).Should(gbytes.Say("B Suite"))
  173. Consistently(session).ShouldNot(gbytes.Say("A Suite|C Suite"))
  174. modifyTest("C")
  175. Eventually(session).Should(gbytes.Say("Detected changes in"))
  176. Eventually(session).Should(gbytes.Say("C Suite"))
  177. Consistently(session).ShouldNot(gbytes.Say("A Suite|B Suite"))
  178. })
  179. })
  180. Describe("adjusting the watch regular expression", func() {
  181. Describe("the default regular expression", func() {
  182. It("should only trigger when go files are changed", func() {
  183. session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2")
  184. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  185. Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`))
  186. Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
  187. Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))
  188. modifyJSON("C")
  189. Consistently(session).ShouldNot(gbytes.Say("Detected changes in"))
  190. Consistently(session).ShouldNot(gbytes.Say("A Suite|B Suite|C Suite"))
  191. })
  192. })
  193. Describe("modifying the regular expression", func() {
  194. It("should trigger if the regexp matches", func() {
  195. session = startGinkgoWithGopath("watch", "-succinct", "-r", "-depth=2", `-watchRegExp=\.json$`)
  196. Eventually(session).Should(gbytes.Say("Identified 3 test suites"))
  197. Eventually(session).Should(gbytes.Say(`A \[2 dependencies\]`))
  198. Eventually(session).Should(gbytes.Say(`B \[1 dependency\]`))
  199. Eventually(session).Should(gbytes.Say(`C \[0 dependencies\]`))
  200. modifyJSON("C")
  201. Eventually(session).Should(gbytes.Say("Detected changes in"))
  202. Eventually(session).Should(gbytes.Say("C Suite"))
  203. Eventually(session).Should(gbytes.Say("B Suite"))
  204. Eventually(session).Should(gbytes.Say("A Suite"))
  205. })
  206. })
  207. })
  208. Describe("when new test suite is added", func() {
  209. It("should start monitoring that test suite", func() {
  210. session = startGinkgoWithGopath("watch", "-succinct", "-r")
  211. Eventually(session).Should(gbytes.Say("Watching 3 suites"))
  212. pathD := filepath.Join(rootPath, "src", "github.com", "onsi", "D")
  213. err := os.MkdirAll(pathD, 0700)
  214. Ω(err).ShouldNot(HaveOccurred())
  215. copyIn(fixturePath(filepath.Join("watch_fixtures", "D")), pathD, false)
  216. Eventually(session).Should(gbytes.Say("Detected 1 new suite"))
  217. Eventually(session).Should(gbytes.Say(`D \[1 dependency\]`))
  218. Eventually(session).Should(gbytes.Say("D Suite"))
  219. modifyCode("D")
  220. Eventually(session).Should(gbytes.Say("Detected changes in"))
  221. Eventually(session).Should(gbytes.Say("D Suite"))
  222. modifyCode("C")
  223. Eventually(session).Should(gbytes.Say("Detected changes in"))
  224. Eventually(session).Should(gbytes.Say("C Suite"))
  225. Eventually(session).Should(gbytes.Say("D Suite"))
  226. })
  227. })
  228. })