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



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

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
# File 'lib/rspec/signal/formatter.rb', line 31

def config = RSpec::Signal.configuration

#dump_summary(notification) ⇒ Object



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

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



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

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



40
41
42
# File 'lib/rspec/signal/formatter.rb', line 40

def example_passed(_notification)
  advance_progress
end

#example_pending(_notification) ⇒ Object



44
45
46
# File 'lib/rspec/signal/formatter.rb', line 44

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.



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

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



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

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

#start(notification) ⇒ Object



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

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