Skip to content

Commit

Permalink
Prevent malformed exception messages from exiting RSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Sep 3, 2020
1 parent 019efb6 commit 65b32d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Empty file added lib/rspec/core/fork_runner.rb
Empty file.
10 changes: 8 additions & 2 deletions lib/rspec/core/formatters/exception_presenter.rb
Expand Up @@ -51,7 +51,7 @@ def formatted_cause(exception)
cause << '--- Caused by: ---'
cause << "#{exception_class_name(last_cause)}:" unless exception_class_name(last_cause) =~ /RSpec/

encoded_string(last_cause.message.to_s).split("\n").each do |line|
encoded_string(exception_message_string(last_cause)).split("\n").each do |line|
cause << " #{line}"
end

Expand Down Expand Up @@ -174,11 +174,17 @@ def failure_slash_error_lines
lines
end

def exception_message_string(exception)
exception.message.to_s
rescue Exception => other
"A #{exception.class} for which `exception.message.to_s` raises #{other.class}."
end

def exception_lines
@exception_lines ||= begin
lines = []
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
encoded_string(exception.message.to_s).split("\n").each do |line|
encoded_string(exception_message_string(exception)).split("\n").each do |line|
lines << (line.empty? ? line : " #{line}")
end
lines
Expand Down
20 changes: 20 additions & 0 deletions spec/rspec/core/formatters/exception_presenter_spec.rb
Expand Up @@ -260,6 +260,26 @@ def initialize(message, backtrace = [], cause = nil)
EOS
end

it 'will work then the message to_s raises a looped exception' do
raising_to_s_klass =
Class.new do
def to_s
raise StandardError, self
end
end

incorrect_message_exception = FakeException.new(raising_to_s_klass.new, [])

the_presenter = Formatters::ExceptionPresenter.new(incorrect_message_exception, example)

expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) Example
| Failure/Error: Unable to find matching line from backtrace
| A #{FakeException} for which `exception.message.to_s` raises StandardError.
EOS
end

it "adds extra failure lines from the example metadata" do
extra_example = example.clone
failure_line = 'http://www.example.com/job_details/123'
Expand Down

0 comments on commit 65b32d1

Please sign in to comment.