Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit fd8aaea

Browse files
authored
Merge pull request #2916 from niceking/bypass-output-check-when-output-undefined
Skip output check if output not defined on formatter
2 parents e36aa2a + d562c27 commit fd8aaea

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/rspec/core/formatters.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,16 @@ def register(formatter, notifications)
194194

195195
def duplicate_formatter_exists?(new_formatter)
196196
@formatters.any? do |formatter|
197-
formatter.class == new_formatter.class && formatter.output == new_formatter.output
197+
formatter.class == new_formatter.class &&
198+
has_matching_output?(formatter, new_formatter)
198199
end
199200
end
200201

202+
def has_matching_output?(formatter, new_formatter)
203+
return true unless formatter.respond_to?(:output) && new_formatter.respond_to?(:output)
204+
formatter.output == new_formatter.output
205+
end
206+
201207
def existing_formatter_implements?(notification)
202208
@reporter.registered_listeners(notification).any?
203209
end

spec/rspec/core/formatters_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,27 @@ module RSpec::Core::Formatters
144144
loader.add :documentation, path
145145
}.to change { loader.formatters.length }
146146
end
147+
148+
plain_old_formatter = Class.new do
149+
RSpec::Core::Formatters.register self, :example_started
150+
151+
def initialize(output)
152+
end
153+
end
154+
155+
it "handles formatters which do not subclass our formatters" do
156+
expect {
157+
loader.add plain_old_formatter, output
158+
}.to change { loader.formatters.length }
159+
160+
# deliberate duplicate to ensure we can check for them correctly
161+
expect {
162+
loader.add plain_old_formatter, output
163+
}.to_not change { loader.formatters.length }
164+
end
147165
end
148166

149-
context "When a custom formatter exists" do
167+
context "when a custom formatter exists" do
150168
specific_formatter = RSpec::Core::Formatters::JsonFormatter
151169
generic_formatter = specific_formatter.superclass
152170

0 commit comments

Comments
 (0)