session_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package gexec_test
  2. import (
  3. "os/exec"
  4. "syscall"
  5. "time"
  6. . "github.com/onsi/gomega/gbytes"
  7. . "github.com/onsi/gomega/gexec"
  8. . "github.com/onsi/ginkgo"
  9. . "github.com/onsi/gomega"
  10. )
  11. var _ = Describe("Session", func() {
  12. var command *exec.Cmd
  13. var session *Session
  14. var outWriter, errWriter *Buffer
  15. BeforeEach(func() {
  16. outWriter = nil
  17. errWriter = nil
  18. })
  19. JustBeforeEach(func() {
  20. command = exec.Command(fireflyPath)
  21. var err error
  22. session, err = Start(command, outWriter, errWriter)
  23. Ω(err).ShouldNot(HaveOccurred())
  24. })
  25. Context("running a command", func() {
  26. It("should start the process", func() {
  27. Ω(command.Process).ShouldNot(BeNil())
  28. })
  29. It("should wrap the process's stdout and stderr with gbytes buffers", func(done Done) {
  30. Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
  31. Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
  32. defer session.Out.CancelDetects()
  33. select {
  34. case <-session.Out.Detect("Can we maybe vote on the whole murdering people issue"):
  35. Eventually(session).Should(Exit(0))
  36. case <-session.Out.Detect("I swear by my pretty floral bonnet, I will end you."):
  37. Eventually(session).Should(Exit(1))
  38. case <-session.Out.Detect("My work's illegal, but at least it's honest."):
  39. Eventually(session).Should(Exit(2))
  40. }
  41. close(done)
  42. })
  43. It("should satisfy the gbytes.BufferProvider interface, passing Stdout", func() {
  44. Eventually(session).Should(Say("We've done the impossible, and that makes us mighty"))
  45. Eventually(session).Should(Exit())
  46. })
  47. })
  48. Describe("providing the exit code", func() {
  49. It("should provide the app's exit code", func() {
  50. Ω(session.ExitCode()).Should(Equal(-1))
  51. Eventually(session).Should(Exit())
  52. Ω(session.ExitCode()).Should(BeNumerically(">=", 0))
  53. Ω(session.ExitCode()).Should(BeNumerically("<", 3))
  54. })
  55. })
  56. Describe("wait", func() {
  57. It("should wait till the command exits", func() {
  58. Ω(session.ExitCode()).Should(Equal(-1))
  59. Ω(session.Wait().ExitCode()).Should(BeNumerically(">=", 0))
  60. Ω(session.Wait().ExitCode()).Should(BeNumerically("<", 3))
  61. })
  62. })
  63. Describe("exited", func() {
  64. It("should close when the command exits", func() {
  65. Eventually(session.Exited).Should(BeClosed())
  66. Ω(session.ExitCode()).ShouldNot(Equal(-1))
  67. })
  68. })
  69. Describe("kill", func() {
  70. It("should kill the command and wait for it to exit", func() {
  71. session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
  72. Ω(err).ShouldNot(HaveOccurred())
  73. session.Kill()
  74. Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
  75. Eventually(session).Should(Exit(128 + 9))
  76. })
  77. })
  78. Describe("interrupt", func() {
  79. It("should interrupt the command", func() {
  80. session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
  81. Ω(err).ShouldNot(HaveOccurred())
  82. session.Interrupt()
  83. Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
  84. Eventually(session).Should(Exit(128 + 2))
  85. })
  86. })
  87. Describe("terminate", func() {
  88. It("should terminate the command", func() {
  89. session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
  90. Ω(err).ShouldNot(HaveOccurred())
  91. session.Terminate()
  92. Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
  93. Eventually(session).Should(Exit(128 + 15))
  94. })
  95. })
  96. Describe("signal", func() {
  97. It("should send the signal to the command", func() {
  98. session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
  99. Ω(err).ShouldNot(HaveOccurred())
  100. session.Signal(syscall.SIGABRT)
  101. Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
  102. Eventually(session).Should(Exit(128 + 6))
  103. })
  104. })
  105. Context("when the command exits", func() {
  106. It("should close the buffers", func() {
  107. Eventually(session).Should(Exit())
  108. Ω(session.Out.Closed()).Should(BeTrue())
  109. Ω(session.Err.Closed()).Should(BeTrue())
  110. Ω(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
  111. })
  112. var So = It
  113. So("this means that eventually should short circuit", func() {
  114. t := time.Now()
  115. failures := InterceptGomegaFailures(func() {
  116. Eventually(session).Should(Say("blah blah blah blah blah"))
  117. })
  118. Ω(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))
  119. Ω(failures).Should(HaveLen(1))
  120. })
  121. })
  122. Context("when wrapping out and err", func() {
  123. BeforeEach(func() {
  124. outWriter = NewBuffer()
  125. errWriter = NewBuffer()
  126. })
  127. It("should route to both the provided writers and the gbytes buffers", func() {
  128. Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
  129. Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
  130. Ω(outWriter.Contents()).Should(ContainSubstring("We've done the impossible, and that makes us mighty"))
  131. Ω(errWriter.Contents()).Should(ContainSubstring("Ah, curse your sudden but inevitable betrayal!"))
  132. Eventually(session).Should(Exit())
  133. Ω(outWriter.Contents()).Should(Equal(session.Out.Contents()))
  134. Ω(errWriter.Contents()).Should(Equal(session.Err.Contents()))
  135. })
  136. })
  137. Describe("when the command fails to start", func() {
  138. It("should return an error", func() {
  139. _, err := Start(exec.Command("agklsjdfas"), nil, nil)
  140. Ω(err).Should(HaveOccurred())
  141. })
  142. })
  143. })