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

Commit 1488497

Browse files
committed
Allow users to set a formatter instance instead of just a class.
This is useful for when you want to initialize your formatter with extra state. The simplest approach is for you to instantiate the formatter yourself instead of depending on RSpec to do it and trying to update the formatter with your desired state later.
1 parent 83f7105 commit 1488497

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/rspec/core/configuration.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,11 @@ def full_description
861861
# @overload add_formatter(formatter)
862862
# @overload add_formatter(formatter, output)
863863
#
864-
# @param formatter [Class, String] formatter to use. Can be any of the
864+
# @param formatter [Class, String, Object] formatter to use. Can be any of the
865865
# string values supported from the CLI (`p`/`progress`,
866-
# `d`/`doc`/`documentation`, `h`/`html`, or `j`/`json`) or any
866+
# `d`/`doc`/`documentation`, `h`/`html`, or `j`/`json`), any
867867
# class that implements the formatter protocol and has registered
868-
# itself with RSpec as a formatter.
868+
# itself with RSpec as a formatter, or a formatter instance.
869869
# @param output [String, IO] where the formatter will write its output.
870870
# Can be an IO object or a string path to a file. If not provided,
871871
# the configured `output_stream` (`$stdout`, by default) will be used.

lib/rspec/core/formatters.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def setup_default(output_stream, deprecation_stream)
140140

141141
# @private
142142
def add(formatter_to_use, *paths)
143+
# If a formatter instance was passed, we can register it directly,
144+
# with no need for any of the further processing that happens below.
145+
if Loader.formatters.key?(formatter_to_use.class)
146+
register formatter_to_use, notifications_for(formatter_to_use.class)
147+
return
148+
end
149+
143150
formatter_class = find_formatter(formatter_to_use)
144151

145152
args = paths.map { |p| p.respond_to?(:puts) ? p : open_stream(p) }

spec/rspec/core/formatters_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ module RSpec::Core::Formatters
3939
expect(loader.formatters.first).to be_an_instance_of(CustomFormatter)
4040
end
4141

42+
it "lets you pass a formatter instance, for when you need to instantiate it with some custom state" do
43+
instance = ProgressFormatter.new(StringIO.new)
44+
45+
expect {
46+
loader.add(instance)
47+
}.to change { loader.formatters }.from([]).to([instance])
48+
end
49+
4250
context "when a legacy formatter is added with RSpec::LegacyFormatters" do
4351
formatter_class = Struct.new(:output)
4452
let(:formatter) { double "formatter", :notifications => notifications, :output => output }

0 commit comments

Comments
 (0)