initial commit
[govpp.git] / vendor / github.com / onsi / gomega / gexec / exit_matcher_test.go
1 package gexec_test
2
3 import (
4         . "github.com/onsi/gomega/gexec"
5         "os/exec"
6         "time"
7
8         . "github.com/onsi/ginkgo"
9         . "github.com/onsi/gomega"
10 )
11
12 type NeverExits struct{}
13
14 func (e NeverExits) ExitCode() int {
15         return -1
16 }
17
18 var _ = Describe("ExitMatcher", func() {
19         var command *exec.Cmd
20         var session *Session
21
22         BeforeEach(func() {
23                 var err error
24                 command = exec.Command(fireflyPath, "0")
25                 session, err = Start(command, nil, nil)
26                 Ω(err).ShouldNot(HaveOccurred())
27         })
28
29         Describe("when passed something that is an Exiter", func() {
30                 It("should act normally", func() {
31                         failures := InterceptGomegaFailures(func() {
32                                 Ω(NeverExits{}).Should(Exit())
33                         })
34
35                         Ω(failures[0]).Should(ContainSubstring("Expected process to exit.  It did not."))
36                 })
37         })
38
39         Describe("when passed something that is not an Exiter", func() {
40                 It("should error", func() {
41                         failures := InterceptGomegaFailures(func() {
42                                 Ω("aardvark").Should(Exit())
43                         })
44
45                         Ω(failures[0]).Should(ContainSubstring("Exit must be passed a gexec.Exiter"))
46                 })
47         })
48
49         Context("with no exit code", func() {
50                 It("should say the right things when it fails", func() {
51                         Ω(session).ShouldNot(Exit())
52
53                         failures := InterceptGomegaFailures(func() {
54                                 Ω(session).Should(Exit())
55                         })
56
57                         Ω(failures[0]).Should(ContainSubstring("Expected process to exit.  It did not."))
58
59                         Eventually(session).Should(Exit())
60
61                         Ω(session).Should(Exit())
62
63                         failures = InterceptGomegaFailures(func() {
64                                 Ω(session).ShouldNot(Exit())
65                         })
66
67                         Ω(failures[0]).Should(ContainSubstring("Expected process not to exit.  It did."))
68                 })
69         })
70
71         Context("with an exit code", func() {
72                 It("should say the right things when it fails", func() {
73                         Ω(session).ShouldNot(Exit(0))
74                         Ω(session).ShouldNot(Exit(1))
75
76                         failures := InterceptGomegaFailures(func() {
77                                 Ω(session).Should(Exit(0))
78                         })
79
80                         Ω(failures[0]).Should(ContainSubstring("Expected process to exit.  It did not."))
81
82                         Eventually(session).Should(Exit(0))
83
84                         Ω(session).Should(Exit(0))
85
86                         failures = InterceptGomegaFailures(func() {
87                                 Ω(session).Should(Exit(1))
88                         })
89
90                         Ω(failures[0]).Should(ContainSubstring("to match exit code:"))
91
92                         Ω(session).ShouldNot(Exit(1))
93
94                         failures = InterceptGomegaFailures(func() {
95                                 Ω(session).ShouldNot(Exit(0))
96                         })
97
98                         Ω(failures[0]).Should(ContainSubstring("not to match exit code:"))
99                 })
100         })
101
102         Describe("bailing out early", func() {
103                 It("should bail out early once the process exits", func() {
104                         t := time.Now()
105
106                         failures := InterceptGomegaFailures(func() {
107                                 Eventually(session).Should(Exit(1))
108                         })
109                         Ω(time.Since(t)).Should(BeNumerically("<=", 500*time.Millisecond))
110                         Ω(failures).Should(HaveLen(1))
111                 })
112         })
113 })