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

Commit 5ae27f2

Browse files
committed
Add reporter#notify_non_example_exception.
1 parent fd4e761 commit 5ae27f2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/rspec/core/reporter.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ def deprecation(hash)
151151
notify :deprecation, Notifications::DeprecationNotification.from_hash(hash)
152152
end
153153

154+
# @private
155+
# Provides a way to notify of an exception that is not tied to any
156+
# particular exception (such as an exception encountered in a :suite hook).
157+
# Exceptions will be formatted the same way they normally are.
158+
def notify_non_example_exception(exception, context_description)
159+
example = Example.new(AnonymousExampleGroup, context_description, {})
160+
presenter = Formatters::ExceptionPresenter.new(exception, example, :indentation => 0)
161+
message presenter.fully_formatted(nil)
162+
end
163+
154164
# @private
155165
def finish
156166
close_after do

spec/rspec/core/reporter_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,27 @@ module RSpec::Core
280280
reporter.finish
281281
end
282282
end
283+
284+
describe "#notify_non_example_exception" do
285+
it "sends a `message` notification that contains the formatted exception details" do
286+
formatter_out = StringIO.new
287+
formatter = Formatters::ProgressFormatter.new(formatter_out)
288+
reporter.register_listener formatter, :message
289+
290+
line = __LINE__ + 1
291+
exception = 1 / 0 rescue $!
292+
reporter.notify_non_example_exception(exception, "NonExample Context")
293+
294+
expect(formatter_out.string).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
295+
|
296+
|NonExample Context
297+
|Failure/Error: exception = 1 / 0 rescue $!
298+
|
299+
|ZeroDivisionError:
300+
| divided by 0
301+
|# #{Metadata.relative_path(__FILE__)}:#{line}
302+
EOS
303+
end
304+
end
283305
end
284306
end

0 commit comments

Comments
 (0)