blob: 678ea2514a6020c1cc989acf8d9ad865ea606152 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package failer
2
3import (
4 "fmt"
5 "sync"
6
7 "github.com/onsi/ginkgo/types"
8)
9
10type Failer struct {
11 lock *sync.Mutex
12 failure types.SpecFailure
13 state types.SpecState
14}
15
16func New() *Failer {
17 return &Failer{
18 lock: &sync.Mutex{},
19 state: types.SpecStatePassed,
20 }
21}
22
23func (f *Failer) Panic(location types.CodeLocation, forwardedPanic interface{}) {
24 f.lock.Lock()
25 defer f.lock.Unlock()
26
27 if f.state == types.SpecStatePassed {
28 f.state = types.SpecStatePanicked
29 f.failure = types.SpecFailure{
30 Message: "Test Panicked",
31 Location: location,
32 ForwardedPanic: fmt.Sprintf("%v", forwardedPanic),
33 }
34 }
35}
36
37func (f *Failer) Timeout(location types.CodeLocation) {
38 f.lock.Lock()
39 defer f.lock.Unlock()
40
41 if f.state == types.SpecStatePassed {
42 f.state = types.SpecStateTimedOut
43 f.failure = types.SpecFailure{
44 Message: "Timed out",
45 Location: location,
46 }
47 }
48}
49
50func (f *Failer) Fail(message string, location types.CodeLocation) {
51 f.lock.Lock()
52 defer f.lock.Unlock()
53
54 if f.state == types.SpecStatePassed {
55 f.state = types.SpecStateFailed
56 f.failure = types.SpecFailure{
57 Message: message,
58 Location: location,
59 }
60 }
61}
62
63func (f *Failer) Drain(componentType types.SpecComponentType, componentIndex int, componentCodeLocation types.CodeLocation) (types.SpecFailure, types.SpecState) {
64 f.lock.Lock()
65 defer f.lock.Unlock()
66
67 failure := f.failure
68 outcome := f.state
69 if outcome != types.SpecStatePassed {
70 failure.ComponentType = componentType
71 failure.ComponentIndex = componentIndex
72 failure.ComponentCodeLocation = componentCodeLocation
73 }
74
75 f.state = types.SpecStatePassed
76 f.failure = types.SpecFailure{}
77
78 return failure, outcome
79}
80
81func (f *Failer) Skip(message string, location types.CodeLocation) {
82 f.lock.Lock()
83 defer f.lock.Unlock()
84
85 if f.state == types.SpecStatePassed {
86 f.state = types.SpecStateSkipped
87 f.failure = types.SpecFailure{
88 Message: message,
89 Location: location,
90 }
91 }
92}