github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/utils/matchers.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 6 "github.com/onsi/gomega/format" 7 "github.com/onsi/gomega/gexec" 8 ) 9 10 // ExitWithError matches when assertion is > argument. Default 0 11 // Modeled after the gomega Exit() matcher 12 func ExitWithError(optionalExitCode ...int) *exitMatcher { 13 exitCode := 0 14 if len(optionalExitCode) > 0 { 15 exitCode = optionalExitCode[0] 16 } 17 return &exitMatcher{exitCode: exitCode} 18 } 19 20 type exitMatcher struct { 21 exitCode int 22 actualExitCode int 23 } 24 25 func (m *exitMatcher) Match(actual interface{}) (success bool, err error) { 26 exiter, ok := actual.(gexec.Exiter) 27 if !ok { 28 return false, fmt.Errorf("ExitWithError must be passed a gexec.Exiter (Missing method ExitCode() int) Got:\n#{format.Object(actual, 1)}") 29 } 30 31 m.actualExitCode = exiter.ExitCode() 32 if m.actualExitCode == -1 { 33 return false, nil 34 } 35 return m.actualExitCode > m.exitCode, nil 36 } 37 38 func (m *exitMatcher) FailureMessage(actual interface{}) (message string) { 39 if m.actualExitCode == -1 { 40 return "Expected process to exit. It did not." 41 } 42 return format.Message(m.actualExitCode, "to be greater than exit code:", m.exitCode) 43 } 44 45 func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string) { 46 if m.actualExitCode == -1 { 47 return "you really shouldn't be able to see this!" 48 } else { 49 if m.exitCode == -1 { 50 return "Expected process not to exit. It did." 51 } 52 return format.Message(m.actualExitCode, "is less than or equal to exit code:", m.exitCode) 53 } 54 } 55 func (m *exitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool { 56 session, ok := actual.(*gexec.Session) 57 if ok { 58 return session.ExitCode() == -1 59 } 60 return true 61 }