Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatible shim for Exception#detailed_message. #58

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ def format(output, terminal, verbose)
format_exception(@exception, nil, output, terminal, verbose)
end

if Exception.method_defined?(:detailed_message)
def detailed_message(exception)
exception.detailed_message
end
else
def detailed_message(exception)
exception.message
end
end

def format_exception(exception, prefix, output, terminal, verbose)
lines = exception.message.lines.map(&:chomp)
lines = detailed_message(exception).lines.map(&:chomp)

output.puts " #{prefix}#{terminal[:exception_title]}#{exception.class}#{terminal.reset}: #{lines.shift}"

Expand Down
94 changes: 61 additions & 33 deletions test/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,70 @@
require 'console'
require 'console/capture'

class TestError < StandardError
def detailed_message(...)
"#{super}\nwith details"
end
end

describe Console::Event::Failure do
let(:output) {StringIO.new}
let(:terminal) {Console::Terminal.for(output)}
let(:error) {
RuntimeError.new("Test").tap {|e|
e.set_backtrace([
"(irb):2:in `rescue in irb_binding'",
"(irb):1:in `irb_binding'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/workspace.rb:114:in `eval'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/workspace.rb:114:in `evaluate'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/context.rb:450:in `evaluate'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:541:in `block (2 levels) in eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:704:in `signal_status'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:538:in `block in eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:166:in `block (2 levels) in each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:151:in `loop'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:151:in `block in each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:150:in `catch'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:150:in `each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:537:in `eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:472:in `block in run'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:471:in `catch'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:471:in `run'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:400:in `start'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/exe/irb:11:in `<top (required)>'",
"/path/to/root/.gem/ruby/2.5.7/bin/irb:23:in `load'",
"/path/to/root/.gem/ruby/2.5.7/bin/irb:23:in `<main>'"
])
}
}

it "formats exception removing root path" do
event = Console::Event::Failure.new(error, "/path/to/root")
event.format(output, terminal, true)
expect(output.string.lines[3..-1]).to have(
Sus::Have::Value.new(be =~ /^\s+\.gem/)
)
with 'runtime error' do
let(:error) do
RuntimeError.new("Test").tap do |error|
error.set_backtrace([
"(irb):2:in `rescue in irb_binding'",
"(irb):1:in `irb_binding'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/workspace.rb:114:in `eval'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/workspace.rb:114:in `evaluate'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/context.rb:450:in `evaluate'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:541:in `block (2 levels) in eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:704:in `signal_status'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:538:in `block in eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:166:in `block (2 levels) in each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:151:in `loop'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:151:in `block in each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:150:in `catch'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb/ruby-lex.rb:150:in `each_top_level_statement'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:537:in `eval_input'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:472:in `block in run'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:471:in `catch'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:471:in `run'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/lib/irb.rb:400:in `start'",
"/path/to/root/.gem/ruby/2.5.7/gems/irb-1.2.7/exe/irb:11:in `<top (required)>'",
"/path/to/root/.gem/ruby/2.5.7/bin/irb:23:in `load'",
"/path/to/root/.gem/ruby/2.5.7/bin/irb:23:in `<main>'"
])
end
end

it "formats exception removing root path" do
event = Console::Event::Failure.new(error, "/path/to/root")
event.format(output, terminal, true)
expect(output.string.lines[3..-1]).to have_value(be =~ /^\s+\.gem/)
end
end

with 'test error' do
let(:error) do
begin
raise TestError, "Test error!"
rescue TestError => error
error
end
end

it "includes detailed message" do
skip_unless_method_defined(:detailed_message, Exception)

expect(error.detailed_message).to be =~ /with details/

event = Console::Event::Failure.new(error)
event.format(output, terminal, true)
expect(output.string).to be =~ /Test error!/
expect(output.string).to be =~ /with details/
end
end
end
Loading