|
| 1 | +package formatters |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/cucumber/godog/formatters" |
| 7 | + messages "github.com/cucumber/messages/go/v21" |
| 8 | +) |
| 9 | + |
| 10 | +// WrapOnFlush wrap a `formatters.Formatter` in a `formatters.FlushFormatter`, which only |
| 11 | +// executes when `Flush` is called |
| 12 | +func WrapOnFlush(fmt formatters.Formatter) formatters.FlushFormatter { |
| 13 | + return &onFlushFormatter{ |
| 14 | + fmt: fmt, |
| 15 | + fns: make([]func(), 0), |
| 16 | + mu: &sync.Mutex{}, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +type onFlushFormatter struct { |
| 21 | + fmt formatters.Formatter |
| 22 | + fns []func() |
| 23 | + mu *sync.Mutex |
| 24 | +} |
| 25 | + |
| 26 | +func (o *onFlushFormatter) Pickle(pickle *messages.Pickle) { |
| 27 | + o.fns = append(o.fns, func() { |
| 28 | + o.fmt.Pickle(pickle) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +func (o *onFlushFormatter) Passed(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition) { |
| 33 | + o.fns = append(o.fns, func() { |
| 34 | + o.fmt.Passed(pickle, step, definition) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +// Ambiguous implements formatters.Formatter. |
| 39 | +func (o *onFlushFormatter) Ambiguous(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition, err error) { |
| 40 | + o.fns = append(o.fns, func() { |
| 41 | + o.fmt.Ambiguous(pickle, step, definition, err) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +// Defined implements formatters.Formatter. |
| 46 | +func (o *onFlushFormatter) Defined(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition) { |
| 47 | + o.fns = append(o.fns, func() { |
| 48 | + o.fmt.Defined(pickle, step, definition) |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +// Failed implements formatters.Formatter. |
| 53 | +func (o *onFlushFormatter) Failed(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition, err error) { |
| 54 | + o.fns = append(o.fns, func() { |
| 55 | + o.fmt.Failed(pickle, step, definition, err) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +// Feature implements formatters.Formatter. |
| 60 | +func (o *onFlushFormatter) Feature(pickle *messages.GherkinDocument, p string, c []byte) { |
| 61 | + o.fns = append(o.fns, func() { |
| 62 | + o.fmt.Feature(pickle, p, c) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +// Pending implements formatters.Formatter. |
| 67 | +func (o *onFlushFormatter) Pending(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition) { |
| 68 | + o.fns = append(o.fns, func() { |
| 69 | + o.fmt.Pending(pickle, step, definition) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +// Skipped implements formatters.Formatter. |
| 74 | +func (o *onFlushFormatter) Skipped(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition) { |
| 75 | + o.fns = append(o.fns, func() { |
| 76 | + o.fmt.Skipped(pickle, step, definition) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +// Summary implements formatters.Formatter. |
| 81 | +func (o *onFlushFormatter) Summary() { |
| 82 | + o.fns = append(o.fns, func() { |
| 83 | + o.fmt.Summary() |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +// TestRunStarted implements formatters.Formatter. |
| 88 | +func (o *onFlushFormatter) TestRunStarted() { |
| 89 | + o.fns = append(o.fns, func() { |
| 90 | + o.fmt.TestRunStarted() |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +// Undefined implements formatters.Formatter. |
| 95 | +func (o *onFlushFormatter) Undefined(pickle *messages.Pickle, step *messages.PickleStep, definition *formatters.StepDefinition) { |
| 96 | + o.fns = append(o.fns, func() { |
| 97 | + o.fmt.Undefined(pickle, step, definition) |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +// Flush the logs. |
| 102 | +func (o *onFlushFormatter) Flush() { |
| 103 | + o.mu.Lock() |
| 104 | + defer o.mu.Unlock() |
| 105 | + for _, fn := range o.fns { |
| 106 | + fn() |
| 107 | + } |
| 108 | +} |
0 commit comments