8 . "github.com/onsi/gomega/gbytes"
9 . "github.com/onsi/gomega/gexec"
11 . "github.com/onsi/ginkgo"
12 . "github.com/onsi/gomega"
15 var _ = Describe("Session", func() {
19 var outWriter, errWriter *Buffer
26 JustBeforeEach(func() {
27 command = exec.Command(fireflyPath)
29 session, err = Start(command, outWriter, errWriter)
30 Ω(err).ShouldNot(HaveOccurred())
33 Context("running a command", func() {
34 It("should start the process", func() {
35 Ω(command.Process).ShouldNot(BeNil())
38 It("should wrap the process's stdout and stderr with gbytes buffers", func(done Done) {
39 Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
40 Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
41 defer session.Out.CancelDetects()
44 case <-session.Out.Detect("Can we maybe vote on the whole murdering people issue"):
45 Eventually(session).Should(Exit(0))
46 case <-session.Out.Detect("I swear by my pretty floral bonnet, I will end you."):
47 Eventually(session).Should(Exit(1))
48 case <-session.Out.Detect("My work's illegal, but at least it's honest."):
49 Eventually(session).Should(Exit(2))
55 It("should satisfy the gbytes.BufferProvider interface, passing Stdout", func() {
56 Eventually(session).Should(Say("We've done the impossible, and that makes us mighty"))
57 Eventually(session).Should(Exit())
61 Describe("providing the exit code", func() {
62 It("should provide the app's exit code", func() {
63 Ω(session.ExitCode()).Should(Equal(-1))
65 Eventually(session).Should(Exit())
66 Ω(session.ExitCode()).Should(BeNumerically(">=", 0))
67 Ω(session.ExitCode()).Should(BeNumerically("<", 3))
71 Describe("wait", func() {
72 It("should wait till the command exits", func() {
73 Ω(session.ExitCode()).Should(Equal(-1))
74 Ω(session.Wait().ExitCode()).Should(BeNumerically(">=", 0))
75 Ω(session.Wait().ExitCode()).Should(BeNumerically("<", 3))
79 Describe("exited", func() {
80 It("should close when the command exits", func() {
81 Eventually(session.Exited).Should(BeClosed())
82 Ω(session.ExitCode()).ShouldNot(Equal(-1))
86 Describe("kill", func() {
87 It("should kill the command and don't wait for it to exit", func() {
88 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
89 Ω(err).ShouldNot(HaveOccurred())
92 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
93 Eventually(session).Should(Exit(128 + 9))
97 Describe("interrupt", func() {
98 It("should interrupt the command", func() {
99 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
100 Ω(err).ShouldNot(HaveOccurred())
103 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
104 Eventually(session).Should(Exit(128 + 2))
108 Describe("terminate", func() {
109 It("should terminate the command", func() {
110 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
111 Ω(err).ShouldNot(HaveOccurred())
114 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
115 Eventually(session).Should(Exit(128 + 15))
119 Describe("signal", func() {
120 It("should send the signal to the command", func() {
121 session, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
122 Ω(err).ShouldNot(HaveOccurred())
124 session.Signal(syscall.SIGABRT)
125 Ω(session).ShouldNot(Exit(), "Should not exit immediately...")
126 Eventually(session).Should(Exit(128 + 6))
130 Context("tracking sessions", func() {
135 Describe("kill", func() {
136 It("should kill all the started sessions", func() {
137 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
138 Ω(err).ShouldNot(HaveOccurred())
140 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
141 Ω(err).ShouldNot(HaveOccurred())
143 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
144 Ω(err).ShouldNot(HaveOccurred())
148 Eventually(session1).Should(Exit(128 + 9))
149 Eventually(session2).Should(Exit(128 + 9))
150 Eventually(session3).Should(Exit(128 + 9))
153 It("should not wait for exit", func() {
154 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
155 Ω(err).ShouldNot(HaveOccurred())
158 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
160 Eventually(session1).Should(Exit(128 + 9))
163 It("should not track unstarted sessions", func() {
164 _, err := Start(exec.Command("does not exist", "10000000"), GinkgoWriter, GinkgoWriter)
165 Ω(err).Should(HaveOccurred())
167 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
168 Ω(err).ShouldNot(HaveOccurred())
170 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
171 Ω(err).ShouldNot(HaveOccurred())
175 Eventually(session2).Should(Exit(128 + 9))
176 Eventually(session3).Should(Exit(128 + 9))
181 Describe("killAndWait", func() {
182 It("should kill all the started sessions and wait for them to finish", func() {
183 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
184 Ω(err).ShouldNot(HaveOccurred())
186 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
187 Ω(err).ShouldNot(HaveOccurred())
189 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
190 Ω(err).ShouldNot(HaveOccurred())
193 Ω(session1).Should(Exit(128+9), "Should have exited")
194 Ω(session2).Should(Exit(128+9), "Should have exited")
195 Ω(session3).Should(Exit(128+9), "Should have exited")
199 Describe("terminate", func() {
200 It("should terminate all the started sessions", func() {
201 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
202 Ω(err).ShouldNot(HaveOccurred())
204 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
205 Ω(err).ShouldNot(HaveOccurred())
207 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
208 Ω(err).ShouldNot(HaveOccurred())
212 Eventually(session1).Should(Exit(128 + 15))
213 Eventually(session2).Should(Exit(128 + 15))
214 Eventually(session3).Should(Exit(128 + 15))
217 It("should not wait for exit", func() {
218 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
219 Ω(err).ShouldNot(HaveOccurred())
223 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
227 Describe("terminateAndWait", func() {
228 It("should terminate all the started sessions, and wait for them to exit", func() {
229 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
230 Ω(err).ShouldNot(HaveOccurred())
232 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
233 Ω(err).ShouldNot(HaveOccurred())
235 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
236 Ω(err).ShouldNot(HaveOccurred())
240 Ω(session1).Should(Exit(128+15), "Should have exited")
241 Ω(session2).Should(Exit(128+15), "Should have exited")
242 Ω(session3).Should(Exit(128+15), "Should have exited")
246 Describe("signal", func() {
247 It("should signal all the started sessions", func() {
248 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
249 Ω(err).ShouldNot(HaveOccurred())
251 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
252 Ω(err).ShouldNot(HaveOccurred())
254 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
255 Ω(err).ShouldNot(HaveOccurred())
257 Signal(syscall.SIGABRT)
259 Eventually(session1).Should(Exit(128 + 6))
260 Eventually(session2).Should(Exit(128 + 6))
261 Eventually(session3).Should(Exit(128 + 6))
264 It("should not wait", func() {
265 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
266 Ω(err).ShouldNot(HaveOccurred())
268 Signal(syscall.SIGABRT)
270 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
274 Describe("interrupt", func() {
275 It("should interrupt all the started sessions, and not wait", func() {
276 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
277 Ω(err).ShouldNot(HaveOccurred())
279 session2, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
280 Ω(err).ShouldNot(HaveOccurred())
282 session3, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
283 Ω(err).ShouldNot(HaveOccurred())
287 Eventually(session1).Should(Exit(128 + 2))
288 Eventually(session2).Should(Exit(128 + 2))
289 Eventually(session3).Should(Exit(128 + 2))
292 It("should not wait", func() {
293 session1, err := Start(exec.Command("sleep", "10000000"), GinkgoWriter, GinkgoWriter)
294 Ω(err).ShouldNot(HaveOccurred())
298 Ω(session1).ShouldNot(Exit(), "Should not exit immediately...")
303 Context("when the command exits", func() {
304 It("should close the buffers", func() {
305 Eventually(session).Should(Exit())
307 Ω(session.Out.Closed()).Should(BeTrue())
308 Ω(session.Err.Closed()).Should(BeTrue())
310 Ω(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
315 So("this means that eventually should short circuit", func() {
317 failures := InterceptGomegaFailures(func() {
318 Eventually(session).Should(Say("blah blah blah blah blah"))
320 Ω(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))
321 Ω(failures).Should(HaveLen(1))
325 Context("when wrapping out and err", func() {
327 outWriter = NewBuffer()
328 errWriter = NewBuffer()
331 It("should route to both the provided writers and the gbytes buffers", func() {
332 Eventually(session.Out).Should(Say("We've done the impossible, and that makes us mighty"))
333 Eventually(session.Err).Should(Say("Ah, curse your sudden but inevitable betrayal!"))
335 Ω(outWriter.Contents()).Should(ContainSubstring("We've done the impossible, and that makes us mighty"))
336 Ω(errWriter.Contents()).Should(ContainSubstring("Ah, curse your sudden but inevitable betrayal!"))
338 Eventually(session).Should(Exit())
340 Ω(outWriter.Contents()).Should(Equal(session.Out.Contents()))
341 Ω(errWriter.Contents()).Should(Equal(session.Err.Contents()))
345 Describe("when the command fails to start", func() {
346 It("should return an error", func() {
347 _, err := Start(exec.Command("agklsjdfas"), nil, nil)
348 Ω(err).Should(HaveOccurred())