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