Skip to content

Commit f2e58a9

Browse files
committed
prevent formater loader registering duplicate listeners
1 parent ccda143 commit f2e58a9

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Bug Fixes:
7070
where filtered to a particular location. (Ben Axnick, #1963)
7171
* Fix time formatting logic so that it displays 70 seconds as "1 minute,
7272
10 seconds" rather than "1 minute, 1 second". (Paul Brennan, #1984)
73+
* Fix regression where the formatter loader would allow duplicate formatters.
74+
(Jon Rowe, #1990)
7375

7476
### 3.2.3 / 2015-04-06
7577
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.2.2...v3.2.3)

lib/rspec/core/formatters.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ def add(formatter_to_use, *paths)
143143

144144
if !Loader.formatters[formatter_class].nil?
145145
formatter = formatter_class.new(*args)
146-
@reporter.register_listener formatter, *notifications_for(formatter_class)
146+
register formatter, notifications_for(formatter_class)
147147
elsif defined?(RSpec::LegacyFormatters)
148148
formatter = RSpec::LegacyFormatters.load_formatter formatter_class, *args
149-
@reporter.register_listener formatter, *formatter.notifications
149+
register formatter, formatter.notifications
150150
else
151151
call_site = "Formatter added at: #{::RSpec::CallerFilter.first_non_rspec_line}"
152152

@@ -161,10 +161,7 @@ def add(formatter_to_use, *paths)
161161
|
162162
|#{call_site}
163163
WARNING
164-
return
165164
end
166-
@formatters << formatter unless duplicate_formatter_exists?(formatter)
167-
formatter
168165
end
169166

170167
private
@@ -176,6 +173,13 @@ def find_formatter(formatter_to_use)
176173
"maybe you meant 'documentation' or 'progress'?.")
177174
end
178175

176+
def register(formatter, notifications)
177+
return if duplicate_formatter_exists?(formatter)
178+
@reporter.register_listener formatter, *notifications
179+
@formatters << formatter
180+
formatter
181+
end
182+
179183
def duplicate_formatter_exists?(new_formatter)
180184
@formatters.any? do |formatter|
181185
formatter.class === new_formatter && formatter.output == new_formatter.output

spec/rspec/core/formatters_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module RSpec::Core::Formatters
4141

4242
context "when a legacy formatter is added with RSpec::LegacyFormatters" do
4343
formatter_class = Struct.new(:output)
44-
let(:formatter) { double "formatter", :notifications => notifications }
44+
let(:formatter) { double "formatter", :notifications => notifications, :output => output }
4545
let(:notifications) { [:a, :b, :c] }
4646

4747
before do
@@ -57,6 +57,14 @@ module RSpec::Core::Formatters
5757
expect(reporter).to receive(:register_listener).with(formatter, *notifications)
5858
loader.add formatter_class, output
5959
end
60+
61+
it "will ignore duplicate legacy formatters" do
62+
loader.add formatter_class, output
63+
expect(reporter).to_not receive(:register_listener)
64+
expect {
65+
loader.add formatter_class, output
66+
}.not_to change { loader.formatters.length }
67+
end
6068
end
6169

6270
context "when a legacy formatter is added without RSpec::LegacyFormatters" do
@@ -117,6 +125,7 @@ module RSpec::Core::Formatters
117125
before { loader.add :documentation, output }
118126

119127
it "doesn't add the formatter for the same output target" do
128+
expect(reporter).to_not receive(:register_listener)
120129
expect {
121130
loader.add :documentation, output
122131
}.not_to change { loader.formatters.length }

0 commit comments

Comments
 (0)