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

Allow adding a subclass of an already added formatter #2019

Merged
merged 2 commits into from
Jul 10, 2015

Conversation

argos83
Copy link

@argos83 argos83 commented Jul 3, 2015

duplicate_formatter_exists? in formatters.rb prevents the user from adding the same formatter for the same output twice by checking the formatter instance's class. However the check is done in a way that it doesn't only avoid the same class to be added but also subclasses.

If a user implements a more specific formatter. E.g. a SpecializedJsonFormatter which extends from RSpec::Core::Formatters::JsonFormatter and adds some extra functionality, and he/she wants to register both then, depending on the registration order, the SpecializedJsonFormatter may not be added.

For instance:

RSpec.configure do |config|
  ....
  formatters.each { |formatter| config.add_formatter(formatter) }
  ....

Will work for:

formatters = [SpecializedJsonFormatter, RSpec::Core::Formatters::JsonFormatter]

But not for:

formatters = [RSpec::Core::Formatters::JsonFormatter, SpecializedJsonFormatter]

I believe both cases used to work with RSpec 3.2.0

it "adds a subclass of that formatter for the same output target" do
expect {
loader.add Custom::AMoreSpecificFormatter, output
}.to change { loader.formatters.length }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer this if the classes were declared as local variables / lets inline rather than using ones prepared in another file, especially as for this spec it's completely unnecessary to have an implementation.

e.g.

a_general_formatter = Struct.new(:output)
a_more_specific_formatter = Class.new(a_general_formatter)

before { loader.add a_general_formatter, output }

it "adds a subclass of that formatter for the same output target" do
  expect {
    loader.add a_more_specific_formatter, output
  }.to change { loader.formatters.length }
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried that, but it doesn't work. None of the formatters are added.

and if you do instead

generic_formatter = BaseFormatter
specific_formatter = Class.new(generic_formatter)

Only the generic formatter is added (looks like classes don't get added unless they've registered).

The following works though (and doesn't require the custom formatter classes)

specific_formatter = RSpec::Core::Formatters::JsonFormatter
generic_formatter = specific_formatter.superclass

I'll add those changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, you do need to register otherwise you're considered a legacy formatter, still the new way is better, thanks!

@JonRowe
Copy link
Member

JonRowe commented Jul 8, 2015

Good catch @argos83, I'd like to refactor the specs a little before this is merged but it's looking good otherwise, thanks.

JonRowe added a commit that referenced this pull request Jul 10, 2015
Allow adding a subclass of an already added formatter
@JonRowe JonRowe merged commit 87c832f into rspec:master Jul 10, 2015
JonRowe added a commit that referenced this pull request Jul 10, 2015
JonRowe added a commit that referenced this pull request Jul 10, 2015
Allow adding a subclass of an already added formatter
JonRowe added a commit that referenced this pull request Jul 10, 2015
@JonRowe
Copy link
Member

JonRowe commented Jul 10, 2015

Picked to 3-3-maintenance

MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
…class

Allow adding a subclass of an already added formatter
MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
…ding_formatter_subclass

Allow adding a subclass of an already added formatter

---
This commit was imported from rspec/rspec-core@37728c7.
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
[skip ci]

---
This commit was imported from rspec/rspec-core@ba0955f.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants