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

Commit b3a1d81

Browse files
committed
Merge pull request #1980 from rspec/fallback_message_formatter
Add fallback formatter for #message
2 parents 355b438 + 60b2dc7 commit b3a1d81

File tree

6 files changed

+102
-21
lines changed

6 files changed

+102
-21
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Enhancements:
4747
`aggregate_failures` feature to allow multiple failures in an example
4848
and list them all, rather than aborting on the first failure. (Myron
4949
Marston, #1946)
50+
* When no formatter implements #message add a fallback to prevent those
51+
messages being lost. (Jon Rowe, #1980)
5052

5153
Bug Fixes:
5254

lib/rspec/core/formatters.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@
6666
# @see RSpec::Core::Formatters::BaseTextFormatter
6767
# @see RSpec::Core::Reporter
6868
module RSpec::Core::Formatters
69-
autoload :DocumentationFormatter, 'rspec/core/formatters/documentation_formatter'
70-
autoload :HtmlFormatter, 'rspec/core/formatters/html_formatter'
71-
autoload :ProgressFormatter, 'rspec/core/formatters/progress_formatter'
72-
autoload :ProfileFormatter, 'rspec/core/formatters/profile_formatter'
73-
autoload :JsonFormatter, 'rspec/core/formatters/json_formatter'
74-
autoload :BisectFormatter, 'rspec/core/formatters/bisect_formatter'
69+
autoload :DocumentationFormatter, 'rspec/core/formatters/documentation_formatter'
70+
autoload :HtmlFormatter, 'rspec/core/formatters/html_formatter'
71+
autoload :FallbackMessageFormatter, 'rspec/core/formatters/fallback_message_formatter'
72+
autoload :ProgressFormatter, 'rspec/core/formatters/progress_formatter'
73+
autoload :ProfileFormatter, 'rspec/core/formatters/profile_formatter'
74+
autoload :JsonFormatter, 'rspec/core/formatters/json_formatter'
75+
autoload :BisectFormatter, 'rspec/core/formatters/bisect_formatter'
7576

7677
# Register the formatter class
7778
# @param formatter_class [Class] formatter class to register
@@ -121,6 +122,10 @@ def setup_default(output_stream, deprecation_stream)
121122
add DeprecationFormatter, deprecation_stream, output_stream
122123
end
123124

125+
unless existing_formatter_implements?(:message)
126+
add FallbackMessageFormatter, output_stream
127+
end
128+
124129
return unless RSpec.configuration.profile_examples? && !existing_formatter_implements?(:dump_profile)
125130

126131
add RSpec::Core::Formatters::ProfileFormatter, output_stream
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module RSpec
2+
module Core
3+
module Formatters
4+
# @api private
5+
# Formatter for providing message output as a fallback when no other
6+
# profiler implements #message
7+
class FallbackMessageFormatter
8+
Formatters.register self, :message
9+
10+
def initialize(output)
11+
@output = output
12+
end
13+
14+
# @private
15+
attr_reader :output
16+
17+
# @api public
18+
#
19+
# Used by the reporter to send messages to the output stream.
20+
#
21+
# @param notification [MessageNotification] containing message
22+
def message(notification)
23+
output.puts notification.message
24+
end
25+
end
26+
end
27+
end
28+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'rspec/core/reporter'
2+
require 'rspec/core/formatters/fallback_message_formatter'
3+
4+
module RSpec::Core::Formatters
5+
RSpec.describe FallbackMessageFormatter do
6+
include FormatterSupport
7+
8+
describe "#message" do
9+
it 'writes the message to the output' do
10+
expect {
11+
send_notification :message, message_notification('Custom Message')
12+
}.to change { formatter_output.string }.
13+
from(excluding 'Custom Message').
14+
to(including 'Custom Message')
15+
end
16+
end
17+
end
18+
end

spec/rspec/core/formatters_spec.rb

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,55 @@ module RSpec::Core::Formatters
130130
end
131131
end
132132

133-
describe "#setup_default", "with profiling enabled" do
133+
describe "#setup_default" do
134134
let(:setup_default) { loader.setup_default output, output }
135135

136-
before do
137-
allow(RSpec.configuration).to receive(:profile_examples?) { true }
136+
context "with a formatter that implements #message" do
137+
it 'doesnt add a fallback formatter' do
138+
allow(reporter).to receive(:registered_listeners).with(:message) { [:json] }
139+
setup_default
140+
expect(loader.formatters).to exclude(
141+
an_instance_of ::RSpec::Core::Formatters::FallbackMessageFormatter
142+
)
143+
end
138144
end
139145

140-
context "without an existing profile formatter" do
141-
it "will add the profile formatter" do
142-
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [] }
143-
setup_default
144-
expect(loader.formatters.last).to be_a ::RSpec::Core::Formatters::ProfileFormatter
146+
context "without a formatter that implements #message" do
147+
it 'adds a fallback for message output' do
148+
allow(reporter).to receive(:registered_listeners).with(:message) { [] }
149+
expect {
150+
setup_default
151+
}.to change { loader.formatters }.
152+
from( excluding an_instance_of ::RSpec::Core::Formatters::FallbackMessageFormatter ).
153+
to( including an_instance_of ::RSpec::Core::Formatters::FallbackMessageFormatter )
145154
end
146155
end
147156

148-
context "when a formatter that implement #dump_profile is added" do
149-
it "wont add the profile formatter" do
150-
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [:json] }
151-
setup_default
152-
expect(
153-
loader.formatters.map(&:class)
154-
).to_not include ::RSpec::Core::Formatters::ProfileFormatter
157+
context "with profiling enabled" do
158+
before do
159+
allow(reporter).to receive(:registered_listeners).with(:message) { [:json] }
160+
allow(RSpec.configuration).to receive(:profile_examples?) { true }
161+
end
162+
163+
context "without an existing profile formatter" do
164+
it "will add the profile formatter" do
165+
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [] }
166+
expect {
167+
setup_default
168+
}.to change { loader.formatters }.
169+
from( excluding an_instance_of ::RSpec::Core::Formatters::ProfileFormatter ).
170+
to( including an_instance_of ::RSpec::Core::Formatters::ProfileFormatter )
171+
end
172+
end
173+
174+
context "when a formatter that implement #dump_profile is added" do
175+
it "wont add the profile formatter" do
176+
allow(reporter).to receive(:registered_listeners).with(:dump_profile) { [:json] }
177+
setup_default
178+
expect(
179+
loader.formatters.map(&:class)
180+
).to_not include ::RSpec::Core::Formatters::ProfileFormatter
181+
end
155182
end
156183
end
157184
end

spec/support/matchers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,5 @@ def failure_reason(example)
121121

122122
RSpec::Matchers.define_negated_matcher :avoid_outputting, :output
123123
RSpec::Matchers.define_negated_matcher :exclude, :include
124+
RSpec::Matchers.define_negated_matcher :excluding, :include
124125
RSpec::Matchers.define_negated_matcher :avoid_changing, :change

0 commit comments

Comments
 (0)