2018-01-29 19:44:18 +00:00
|
|
|
package gexec_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
. "github.com/onsi/gomega/gexec"
|
|
|
|
|
2018-01-29 19:44:18 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NeverExits struct{}
|
|
|
|
|
|
|
|
func (e NeverExits) ExitCode() int {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("ExitMatcher", func() {
|
|
|
|
var command *exec.Cmd
|
|
|
|
var session *Session
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
command = exec.Command(fireflyPath, "0")
|
|
|
|
session, err = Start(command, nil, nil)
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Describe("when passed something that is an Exiter", func() {
|
|
|
|
It("should act normally", func() {
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(NeverExits{}).Should(Exit())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Expected process to exit. It did not."))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("when passed something that is not an Exiter", func() {
|
|
|
|
It("should error", func() {
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect("aardvark").Should(Exit())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Exit must be passed a gexec.Exiter"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("with no exit code", func() {
|
|
|
|
It("should say the right things when it fails", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).ShouldNot(Exit())
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).Should(Exit())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Expected process to exit. It did not."))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
Eventually(session).Should(Exit())
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).Should(Exit())
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
failures = InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).ShouldNot(Exit())
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Expected process not to exit. It did."))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("with an exit code", func() {
|
|
|
|
It("should say the right things when it fails", func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).ShouldNot(Exit(0))
|
|
|
|
Expect(session).ShouldNot(Exit(1))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).Should(Exit(0))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("Expected process to exit. It did not."))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
Eventually(session).Should(Exit(0))
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).Should(Exit(0))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
failures = InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).Should(Exit(1))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("to match exit code:"))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).ShouldNot(Exit(1))
|
2018-01-29 19:44:18 +00:00
|
|
|
|
|
|
|
failures = InterceptGomegaFailures(func() {
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(session).ShouldNot(Exit(0))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(failures[0]).Should(ContainSubstring("not to match exit code:"))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("bailing out early", func() {
|
|
|
|
It("should bail out early once the process exits", func() {
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
failures := InterceptGomegaFailures(func() {
|
|
|
|
Eventually(session).Should(Exit(1))
|
|
|
|
})
|
2018-09-05 15:36:14 +00:00
|
|
|
Expect(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))
|
|
|
|
Expect(failures).Should(HaveLen(1))
|
2018-01-29 19:44:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|