You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9 KiB

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