From a1f54200142572fcb762f45f7d067e01893cee9d Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Fri, 26 Jan 2024 01:14:08 +0300 Subject: [PATCH] fixup! Dynamically def method depending on features --- .../core/formatters/exception_presenter.rb | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/rspec/core/formatters/exception_presenter.rb b/lib/rspec/core/formatters/exception_presenter.rb index b38f61a81..c3fe64cab 100644 --- a/lib/rspec/core/formatters/exception_presenter.rb +++ b/lib/rspec/core/formatters/exception_presenter.rb @@ -41,32 +41,38 @@ def formatted_backtrace(exception=@exception) formatted_cause(exception) end - def formatted_cause(exception) - return [] unless RSpec::Support::RubyFeatures.supports_exception_cause? - - last_cause = final_exception(exception, [exception]) - cause = [] - - if exception.cause - cause << '------------------' - cause << '--- Caused by: ---' - cause << "#{exception_class_name(last_cause)}:" unless exception_class_name(last_cause) =~ /RSpec/ - - encoded_string(exception_message_string(last_cause)).split("\n").each do |line| - cause << " #{line}" - end + if RSpec::Support::RubyFeatures.supports_exception_cause? + def formatted_cause(exception) + last_cause = final_exception(exception, [exception]) + cause = [] + + if exception.cause + cause << '------------------' + cause << '--- Caused by: ---' + cause << "#{exception_class_name(last_cause)}:" unless exception_class_name(last_cause) =~ /RSpec/ + + encoded_string(exception_message_string(last_cause)).split("\n").each do |line| + cause << " #{line}" + end - unless last_cause.backtrace.nil? || last_cause.backtrace.empty? - lines = backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata) - lines = [lines[0]] unless RSpec.configuration.full_cause_backtrace + unless last_cause.backtrace.nil? || last_cause.backtrace.empty? + lines = backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata) + lines = [lines[0]] unless RSpec.configuration.full_cause_backtrace - lines.each do |line| - cause << (" #{line}") + lines.each do |line| + cause << (" #{line}") + end end end - end - cause + cause + end + else + # :nocov: + def formatted_cause(_) + [] + end + # :nocov: end def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCodes)