Skip to content

Commit

Permalink
Merge pull request #2285 from hyuraku/repair-exception_text-for-Ruby3.2
Browse files Browse the repository at this point in the history
repair ExceptionHandler#exception_text for Ruby 3.2
  • Loading branch information
andrehjr committed Sep 3, 2023
2 parents 52799c4 + 3b106f1 commit 5a7293f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
9 changes: 7 additions & 2 deletions lib/pry/exception_handler.rb
Expand Up @@ -30,8 +30,13 @@ def standard_error_text_for(exception)
end

def exception_text(exception)
"#{exception.class}: #{exception.message}\n" \
"from #{exception.backtrace.first}\n"
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
"#{exception.class}: #{exception.message}\n" \
"from #{exception.backtrace.first}\n"
else
"#{exception.class}: #{exception.detailed_message}\n" \
"from #{exception.backtrace.first}\n"
end
end

def cause_text(cause)
Expand Down
22 changes: 17 additions & 5 deletions spec/commands/ls_spec.rb
Expand Up @@ -93,14 +93,26 @@
end

it "should show public methods with -p" do
expect(pry_eval("ls -p Class.new{ def goo; end }.new")).to match(/methods: goo/)
message = "ls -p Class.new{ def goo; end }.new"
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
expect(pry_eval(message)).to match(/methods: goo/)
else
expect(pry_eval(message)).to match(/#<Class:.*>#methods: goo/m)
end
end

it "should show protected/private methods with -p" do
expect(pry_eval("ls -pM Class.new{ def goo; end; protected :goo }"))
.to match(/methods: goo/)
expect(pry_eval("ls -p Class.new{ def goo; end; private :goo }.new"))
.to match(/methods: goo/)
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
expect(pry_eval("ls -pM Class.new{ def goo; end; protected :goo }"))
.to match(/methods: goo/)
expect(pry_eval("ls -p Class.new{ def goo; end; private :goo }.new"))
.to match(/methods: goo/)
else
expect(pry_eval("ls -pM Class.new{ def goo; end; protected :goo }"))
.to match(/#<Class:.*>#methods: goo/)
expect(pry_eval("ls -p Class.new{ def goo; end; private :goo }.new"))
.to match(/#<Class:.*>#methods: goo/)
end
end

it "should work for objects with an overridden method method" do
Expand Down
31 changes: 23 additions & 8 deletions spec/exception_handler_spec.rb
Expand Up @@ -25,8 +25,14 @@

it "prints standard error message" do
described_class.handle_exception(output, exception, pry_instance)
expect(output.string)
.to eq("StandardError: oops\nfrom /bin/pry:23:in `<main>'\n")
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
expect(output.string)
.to include("StandardError: oops\nfrom /bin/pry:23:in `<main>'\n")
else
message = "StandardError: oops (StandardError)\nfrom /bin/pry:23:in `<main>'\n"
expect(output.string)
.to include(message)
end
end
end

Expand Down Expand Up @@ -54,12 +60,21 @@

it "prints standard error message" do
described_class.handle_exception(output, exception, pry_instance)
expect(output.string).to match(
/RuntimeError:\souter\soops\n
from\s.+\n
Caused\sby\sRuntimeError:\snested\soops\n
from.+/x
)
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
expect(output.string).to match(
/RuntimeError:\souter\soops\n
from\s.+\n
Caused\sby\sRuntimeError:\snested\soops\n
from.+/x
)
else
expect(output.string).to match(
/RuntimeError:\souter\soops\s\(RuntimeError\)\n
from\s.+\n
Caused\sby\sRuntimeError:\snested\soops\n
from\s.+/x
)
end
end
end
end
Expand Down

0 comments on commit 5a7293f

Please sign in to comment.