From 74995a1a772903c5247886da1105caa27a4afa2d Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 21 Mar 2024 20:49:50 +0100 Subject: [Feature #20275] Remove extra backtrace entries for rescue and ensure --- spec/ruby/core/exception/top_level_spec.rb | 29 ++++++++++++++++++----------- spec/ruby/language/ensure_spec.rb | 17 +++++++++++++++++ spec/ruby/language/rescue_spec.rb | 17 +++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) (limited to 'spec') diff --git a/spec/ruby/core/exception/top_level_spec.rb b/spec/ruby/core/exception/top_level_spec.rb index 6ef7539598..cc961d06d5 100644 --- a/spec/ruby/core/exception/top_level_spec.rb +++ b/spec/ruby/core/exception/top_level_spec.rb @@ -8,25 +8,32 @@ describe "An Exception reaching the top level" do it "the Exception#cause is printed to STDERR with backtraces" do code = <<-RUBY def raise_cause - raise "the cause" + raise "the cause" # 2 end def raise_wrapped - raise "wrapped" + raise "wrapped" # 5 end begin - raise_cause + raise_cause # 8 rescue - raise_wrapped + raise_wrapped # 10 end RUBY lines = ruby_exe(code, args: "2>&1", exit_status: 1).lines - lines.map! { |l| l.chomp[/:(in.+)/, 1] } - lines.size.should == 5 - lines[0].should =~ /\Ain [`'](?:Object#)?raise_wrapped': wrapped \(RuntimeError\)\z/ - lines[1].should =~ /\Ain [`'](?:rescue in )?
'\z/ - lines[2].should =~ /\Ain [`']
'\z/ - lines[3].should =~ /\Ain [`'](?:Object#)?raise_cause': the cause \(RuntimeError\)\z/ - lines[4].should =~ /\Ain [`']
'\z/ + + lines.map! { |l| l.chomp[/:(\d+:in.+)/, 1] } + lines[0].should =~ /\A5:in [`'](?:Object#)?raise_wrapped': wrapped \(RuntimeError\)\z/ + if lines[1].include? 'rescue in' + # CRuby < 3.4 has an extra 'rescue in' backtrace entry + lines[1].should =~ /\A10:in [`']rescue in
'\z/ + lines.delete_at 1 + lines[1].should =~ /\A7:in [`']
'\z/ + else + lines[1].should =~ /\A10:in [`']
'\z/ + end + lines[2].should =~ /\A2:in [`'](?:Object#)?raise_cause': the cause \(RuntimeError\)\z/ + lines[3].should =~ /\A8:in [`']
'\z/ + lines.size.should == 4 end describe "with a custom backtrace" do diff --git a/spec/ruby/language/ensure_spec.rb b/spec/ruby/language/ensure_spec.rb index e893904bcb..175222c86e 100644 --- a/spec/ruby/language/ensure_spec.rb +++ b/spec/ruby/language/ensure_spec.rb @@ -328,4 +328,21 @@ describe "An ensure block inside 'do end' block" do result.should == :begin end + + ruby_version_is "3.4" do + it "does not introduce extra backtrace entries" do + def foo + begin + raise "oops" + ensure + return caller(0, 2) + end + end + line = __LINE__ + foo.should == [ + "#{__FILE__}:#{line-3}:in 'foo'", + "#{__FILE__}:#{line+1}:in 'block (3 levels) in '" + ] + end + end end diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb index d6e7b6c802..a3ee4807ac 100644 --- a/spec/ruby/language/rescue_spec.rb +++ b/spec/ruby/language/rescue_spec.rb @@ -553,6 +553,23 @@ describe "The rescue keyword" do eval('1.+((1 rescue 1))').should == 2 end + ruby_version_is "3.4" do + it "does not introduce extra backtrace entries" do + def foo + begin + raise "oops" + rescue + return caller(0, 2) + end + end + line = __LINE__ + foo.should == [ + "#{__FILE__}:#{line-3}:in 'foo'", + "#{__FILE__}:#{line+1}:in 'block (3 levels) in '" + ] + end + end + describe "inline form" do it "can be inlined" do a = 1/0 rescue 1 -- cgit v1.2.3