Class: RSpec::Signal::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/signal/formatter.rb

Overview

The RSpec formatter. Collects failures as they happen, then writes the artifacts once the run is over.

When selected explicitly it is the only formatter, suppressing RSpec's verbose failure renderer. When auto-installed it restores the default formatter so requiring the gem does not change normal human output.

Constant Summary collapse

PROGRESS_WIDTH =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Formatter

Returns a new instance of Formatter.



22
23
24
25
26
27
28
29
# File 'lib/rspec/signal/formatter.rb', line 22

def initialize(output)
  @output = output
  @failures = []
  @errors = []
  @summary = {}
  @seed = nil
  @seed_used = false
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



20
21
22
# File 'lib/rspec/signal/formatter.rb', line 20

def output
  @output
end

Instance Method Details

#close(_notification) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rspec/signal/formatter.rb', line 79

def close(_notification)
  return unless config.enabled?

  finish_progress
  if ParallelRun.worker?
    ParallelRun.write_worker(report, config)
  else
    result = writer.write(report)
    print_summary(result)
  end
rescue StandardError => e
  record_error(e)
ensure
  warn_about_errors
end

#configObject



31
32
33
# File 'lib/rspec/signal/formatter.rb', line 31

def config
  RSpec::Signal.configuration
end

#dump_summary(notification) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rspec/signal/formatter.rb', line 60

def dump_summary(notification)
  @summary = {
    example_count: notification.example_count,
    failure_count: notification.failure_count,
    pending_count: notification.pending_count,
    duration: notification.duration,
    errors_outside_examples: notification.errors_outside_of_examples_count
  }
rescue StandardError => e
  record_error(e)
end

#example_failed(notification) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rspec/signal/formatter.rb', line 50

def example_failed(notification)
  return unless config.enabled?

  @failures << builder.call(notification, position: @failures.size + 1)
rescue StandardError => e
  record_error(e)
ensure
  advance_progress
end

#example_passed(_notification) ⇒ Object



42
43
44
# File 'lib/rspec/signal/formatter.rb', line 42

def example_passed(_notification)
  advance_progress
end

#example_pending(_notification) ⇒ Object



46
47
48
# File 'lib/rspec/signal/formatter.rb', line 46

def example_pending(_notification)
  advance_progress
end

#reportReport

Returns exposed for testing and for tools that embed the gem.

Returns:

  • (Report)

    exposed for testing and for tools that embed the gem.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rspec/signal/formatter.rb', line 96

def report
  Report.new(
    failures: @failures,
    example_count: @summary.fetch(:example_count, 0),
    failure_count: @summary.fetch(:failure_count, @failures.size),
    pending_count: @summary.fetch(:pending_count, 0),
    duration: @summary[:duration],
    seed: @seed,
    seed_used: @seed_used,
    environment: RSpec::Signal.environment,
    errors_outside_examples: @summary.fetch(:errors_outside_examples, 0),
    relate_failures: config.relate_failures
  )
end

#seed(notification) ⇒ Object



72
73
74
75
76
77
# File 'lib/rspec/signal/formatter.rb', line 72

def seed(notification)
  @seed = notification.seed
  @seed_used = notification.seed_used?
rescue StandardError => e
  record_error(e)
end

#start(notification) ⇒ Object



35
36
37
38
39
40
# File 'lib/rspec/signal/formatter.rb', line 35

def start(notification)
  # Adding a formatter suppresses RSpec's default one. When rspec-signal
  # installed itself, the user never asked for that, so put it back.
  RSpec::Signal.restore_default_formatter! if RSpec::Signal.auto_installed? && !RSpec::Signal.quiet_mode?
  start_progress(notification.count)
end