github.com/paketo-buildpacks/packit@v1.3.2-0.20211206231111-86b75c657449/internal/exit_handler_test.go (about) 1 package internal_test 2 3 import ( 4 "bytes" 5 "errors" 6 "testing" 7 8 "github.com/paketo-buildpacks/packit/internal" 9 "github.com/sclevine/spec" 10 11 . "github.com/onsi/gomega" 12 ) 13 14 func testExitHandler(t *testing.T, context spec.G, it spec.S) { 15 var ( 16 Expect = NewWithT(t).Expect 17 18 exitCode int 19 stderr *bytes.Buffer 20 stdout *bytes.Buffer 21 handler internal.ExitHandler 22 ) 23 24 it.Before(func() { 25 stderr = bytes.NewBuffer([]byte{}) 26 stdout = bytes.NewBuffer([]byte{}) 27 28 handler = internal.NewExitHandler( 29 internal.WithExitHandlerStderr(stderr), 30 internal.WithExitHandlerStdout(stdout), 31 internal.WithExitHandlerExitFunc(func(c int) { exitCode = c }), 32 ) 33 }) 34 35 it("prints the error message and exits with the right error code", func() { 36 handler.Error(errors.New("some-error-message")) 37 Expect(stderr).To(ContainSubstring("some-error-message")) 38 Expect(stdout.String()).To(BeEmpty()) 39 }) 40 41 context("when the error is nil", func() { 42 it("exits with code 0", func() { 43 handler.Error(nil) 44 Expect(exitCode).To(Equal(0)) 45 }) 46 }) 47 48 context("when the error is non-nil", func() { 49 it("exits with code 1", func() { 50 handler.Error(errors.New("failed")) 51 Expect(exitCode).To(Equal(1)) 52 }) 53 }) 54 55 context("when the error is exit.Fail", func() { 56 it("exits with code 1", func() { 57 handler.Error(internal.Fail) 58 Expect(exitCode).To(Equal(100)) 59 }) 60 }) 61 }