github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/failer.go (about) 1 package internal 2 3 import ( 4 "fmt" 5 "sync" 6 7 "github.com/onsi/ginkgo/types" 8 ) 9 10 type Failer struct { 11 lock *sync.Mutex 12 failure types.Failure 13 state types.SpecState 14 } 15 16 func NewFailer() *Failer { 17 return &Failer{ 18 lock: &sync.Mutex{}, 19 state: types.SpecStatePassed, 20 } 21 } 22 23 func (f *Failer) GetState() types.SpecState { 24 f.lock.Lock() 25 defer f.lock.Unlock() 26 return f.state 27 } 28 29 func (f *Failer) GetFailure() types.Failure { 30 f.lock.Lock() 31 defer f.lock.Unlock() 32 return f.failure 33 } 34 35 func (f *Failer) Panic(location types.CodeLocation, forwardedPanic interface{}) { 36 f.lock.Lock() 37 defer f.lock.Unlock() 38 39 if f.state == types.SpecStatePassed { 40 f.state = types.SpecStatePanicked 41 f.failure = types.Failure{ 42 Message: "Test Panicked", 43 Location: location, 44 ForwardedPanic: fmt.Sprintf("%v", forwardedPanic), 45 } 46 } 47 } 48 49 func (f *Failer) Fail(message string, location types.CodeLocation) { 50 f.lock.Lock() 51 defer f.lock.Unlock() 52 53 if f.state == types.SpecStatePassed { 54 f.state = types.SpecStateFailed 55 f.failure = types.Failure{ 56 Message: message, 57 Location: location, 58 } 59 } 60 } 61 62 func (f *Failer) Skip(message string, location types.CodeLocation) { 63 f.lock.Lock() 64 defer f.lock.Unlock() 65 66 if f.state == types.SpecStatePassed { 67 f.state = types.SpecStateSkipped 68 f.failure = types.Failure{ 69 Message: message, 70 Location: location, 71 } 72 } 73 } 74 75 func (f *Failer) AbortSuite(message string, location types.CodeLocation) { 76 f.lock.Lock() 77 defer f.lock.Unlock() 78 79 if f.state == types.SpecStatePassed { 80 f.state = types.SpecStateAborted 81 f.failure = types.Failure{ 82 Message: message, 83 Location: location, 84 } 85 } 86 } 87 88 func (f *Failer) Drain() (types.SpecState, types.Failure) { 89 f.lock.Lock() 90 defer f.lock.Unlock() 91 92 failure := f.failure 93 outcome := f.state 94 95 f.state = types.SpecStatePassed 96 f.failure = types.Failure{} 97 98 return outcome, failure 99 }