Skip to content

Commit 5a2a6a0

Browse files
committed
Delay formatter setup until reporter needs to notify something
1 parent ef5f3cd commit 5a2a6a0

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

lib/rspec/core/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ def reporter
930930
@reporter_buffer || @reporter ||=
931931
begin
932932
@reporter_buffer = DeprecationReporterBuffer.new
933-
formatter_loader.setup_default output_stream, deprecation_stream
933+
formatter_loader.prepare_default output_stream, deprecation_stream
934934
@reporter_buffer.play_onto(formatter_loader.reporter)
935935
@reporter_buffer = nil
936936
formatter_loader.reporter

lib/rspec/core/formatters.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ def initialize(reporter)
115115
# @return [String] the default formatter to setup, defaults to `progress`
116116
attr_accessor :default_formatter
117117

118+
# @private
119+
def prepare_default(output_stream, deprecation_stream)
120+
reporter.prepare_default(self, output_stream, deprecation_stream)
121+
end
122+
118123
# @private
119124
def setup_default(output_stream, deprecation_stream)
120125
add default_formatter, output_stream if @formatters.empty?

lib/rspec/core/reporter.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def initialize(configuration)
1919
@pending_examples = []
2020
@duration = @start = @load_time = nil
2121
@non_example_exception_count = 0
22+
@setup_default = lambda {}
23+
@setup = false
2224
end
2325

2426
# @private
@@ -52,6 +54,13 @@ def register_listener(listener, *notifications)
5254
true
5355
end
5456

57+
# @private
58+
def prepare_default(loader, output_stream, deprecation_stream)
59+
@setup_default = lambda do
60+
loader.setup_default output_stream, deprecation_stream
61+
end
62+
end
63+
5564
# @private
5665
def registered_listeners(notification)
5766
@listeners[notification].to_a
@@ -200,6 +209,7 @@ def stop
200209

201210
# @private
202211
def notify(event, notification)
212+
ensure_listeners_ready
203213
registered_listeners(event).each do |formatter|
204214
formatter.__send__(event, notification)
205215
end
@@ -225,6 +235,13 @@ def fail_fast_limit_met?
225235

226236
private
227237

238+
def ensure_listeners_ready
239+
return if @setup
240+
241+
@setup_default.call
242+
@setup = true
243+
end
244+
228245
def close
229246
notify :close, Notifications::NullNotification
230247
end

spec/rspec/core/configuration_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,38 @@ def metadata_hash(*args)
14591459
end
14601460
end
14611461

1462+
describe '#reporter' do
1463+
before do
1464+
config.output_stream = StringIO.new
1465+
config.deprecation_stream = StringIO.new
1466+
end
1467+
1468+
it 'doesnt immediately trigger formatter setup' do
1469+
config.reporter
1470+
1471+
expect(config.formatters).to be_empty
1472+
end
1473+
1474+
it 'allows registering listeners without doubling up formatters' do
1475+
config.reporter.register_listener double(:message => nil), :message
1476+
1477+
expect {
1478+
config.formatter = :documentation
1479+
}.to change { config.formatters.size }.from(0).to(1)
1480+
1481+
expect {
1482+
config.reporter.notify :message, double(:message => 'Triggers formatter setup')
1483+
}.to change { config.formatters.size }.from(1).to(2)
1484+
end
1485+
1486+
it 'still configures a default formatter when none specified' do
1487+
config.reporter.register_listener double(:message => nil), :message
1488+
expect {
1489+
config.reporter.notify :message, double(:message => 'Triggers formatter setup')
1490+
}.to change { config.formatters.size }.from(0).to(2)
1491+
end
1492+
end
1493+
14621494
describe "#default_formatter" do
14631495
it 'defaults to `progress`' do
14641496
expect(config.default_formatter).to eq('progress')
@@ -1480,8 +1512,13 @@ def metadata_hash(*args)
14801512
end
14811513

14821514
context 'when no other formatter has been set' do
1515+
before do
1516+
config.output_stream = StringIO.new
1517+
end
1518+
14831519
it 'gets used' do
14841520
config.default_formatter = 'doc'
1521+
config.reporter.notify :message, double(:message => 'Triggers formatter setup')
14851522

14861523
expect(used_formatters).not_to include(an_instance_of Formatters::ProgressFormatter)
14871524
expect(used_formatters).to include(an_instance_of Formatters::DocumentationFormatter)

0 commit comments

Comments
 (0)