Skip to content

Commit 2e0bb19

Browse files
authored
Merge pull request rspec#2243 from rspec/delay_formatter_setup
Delay formatter setup until reporter needs to notify something
2 parents 271aa38 + 048ca59 commit 2e0bb19

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Bug Fixes:
44

55
* Include example id in the JSON formatter output. (#2369, Xavier Shay)
6+
* Delay formatter loading until the last minute to allow accessing the reporter
7+
without triggering formatter setup. (Jon Rowe, #2243)
68

79
### 3.6.0.beta2 / 2016-12-12
810
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.6.0.beta1...v3.6.0.beta2)

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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,53 @@ 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 'does not immediately trigger formatter setup' do
1469+
config.reporter
1470+
1471+
expect(config.formatters).to be_empty
1472+
end
1473+
1474+
it 'buffers deprecations until the reporter is ready' do
1475+
allow(config.formatter_loader).to receive(:prepare_default).and_wrap_original do |original, *args|
1476+
config.reporter.deprecation :message => 'Test deprecation'
1477+
original.call(*args)
1478+
end
1479+
expect {
1480+
config.reporter.notify :deprecation_summary, Notifications::NullNotification
1481+
}.to change { config.deprecation_stream.string }.to include 'Test deprecation'
1482+
end
1483+
1484+
it 'allows registering listeners without doubling up formatters' do
1485+
config.reporter.register_listener double(:message => nil), :message
1486+
1487+
expect {
1488+
config.formatter = :documentation
1489+
}.to change { config.formatters.size }.from(0).to(1)
1490+
1491+
# notify triggers the formatter setup, there are two due to the already configured
1492+
# documentation formatter and deprecation formatter
1493+
expect {
1494+
config.reporter.notify :message, double(:message => 'Triggers formatter setup')
1495+
}.to change { config.formatters.size }.from(1).to(2)
1496+
end
1497+
1498+
it 'still configures a default formatter when none specified' do
1499+
config.reporter.register_listener double(:message => nil), :message
1500+
1501+
# notify triggers the formatter setup, there are two due to the default
1502+
# (progress) and deprecation formatter
1503+
expect {
1504+
config.reporter.notify :message, double(:message => 'Triggers formatter setup')
1505+
}.to change { config.formatters.size }.from(0).to(2)
1506+
end
1507+
end
1508+
14621509
describe "#default_formatter" do
14631510
it 'defaults to `progress`' do
14641511
expect(config.default_formatter).to eq('progress')
@@ -1480,8 +1527,13 @@ def metadata_hash(*args)
14801527
end
14811528

14821529
context 'when no other formatter has been set' do
1530+
before do
1531+
config.output_stream = StringIO.new
1532+
end
1533+
14831534
it 'gets used' do
14841535
config.default_formatter = 'doc'
1536+
config.reporter.notify :message, double(:message => 'Triggers formatter setup')
14851537

14861538
expect(used_formatters).not_to include(an_instance_of Formatters::ProgressFormatter)
14871539
expect(used_formatters).to include(an_instance_of Formatters::DocumentationFormatter)

spec/rspec/core/formatters/base_text_formatter_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
expect(formatter_output.string).to match("1 example, 1 failure, 1 pending")
3636
end
3737

38+
it "with 1s outputs singular (only pending)" do
39+
send_notification :dump_summary, summary_notification(1, examples(1), examples(0), examples(1), 0)
40+
expect(formatter_output.string).to match("1 example, 0 failures, 1 pending")
41+
end
42+
3843
it "with 2s outputs pluralized (including pending)" do
3944
send_notification :dump_summary, summary_notification(2, examples(2), examples(2), examples(2), 0)
4045
expect(formatter_output.string).to match("2 examples, 2 failures, 2 pending")

0 commit comments

Comments
 (0)