Skip to content

Commit

Permalink
Don't show 'Maybe IRB bug!' in show_source and ls command
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Nov 27, 2024
1 parent 4217a46 commit c82b568
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
34 changes: 23 additions & 11 deletions lib/irb/command/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module IRB

module Command
class Ls < Base
include RubyArgsExtractor
class EvaluationError < StandardError; end

category "Context"
description "Show methods, constants, and variables."
Expand All @@ -22,24 +22,35 @@ class Ls < Base
-g [query] Filter the output with a query.
HELP_MESSAGE

def evaluate(code)
@irb_context.workspace.binding.eval(code)
rescue Exception => e
puts "#{e.class}: #{e.message}"
raise EvaluationError
end

def execute(arg)
if match = arg.match(/\A(?<target>.+\s|)(-g|-G)\s+(?<grep>.+)$/)
if match[:target].empty?
use_main = true
else
obj = @irb_context.workspace.binding.eval(match[:target])
end
target = match[:target]
grep = Regexp.new(match[:grep])
elsif match = arg.match(/\A((?<target>.+),|)\s*grep:(?<grep>.+)/)
# Legacy style `ls obj, grep: /regexp/`
# Evaluation order should be eval(target) then eval(grep)
target = match[:target] || ''
grep_regexp_code = match[:grep]
else
args, kwargs = ruby_args(arg)
use_main = args.empty?
obj = args.first
grep = kwargs[:grep]
target = arg.strip
end

if use_main
if target.empty?
obj = irb_context.workspace.main
locals = irb_context.workspace.binding.local_variables
else
obj = evaluate(target)
end

if grep_regexp_code
grep = evaluate(grep_regexp_code)
end

o = Output.new(grep: grep)
Expand All @@ -52,6 +63,7 @@ def execute(arg)
o.dump("class variables", klass.class_variables)
o.dump("locals", locals) if locals
o.print_result
rescue EvaluationError
end

def dump_methods(o, klass, obj)
Expand Down
5 changes: 2 additions & 3 deletions lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ def method_target(owner_receiver, super_level, method, type)
end

def eval_receiver_or_owner(code)
context_binding = @irb_context.workspace.binding
eval(code, context_binding)
rescue NameError
@irb_context.workspace.binding.eval(code)
rescue Exception
raise EvaluationError
end

Expand Down
13 changes: 13 additions & 0 deletions test/irb/command/test_show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ def test_show_source_with_missing_constant
assert_match(%r[Couldn't locate a definition for Foo], out)
end

def test_show_source_with_eval_error
write_ruby <<~'RUBY'
binding.irb
RUBY

out = run_ruby_file do
type "show_source raise(Exception).itself"
type "exit"
end

assert_match(%r[Couldn't locate a definition for raise\(Exception\)\.itself], out)
end

def test_show_source_string
write_ruby <<~'RUBY'
binding.irb
Expand Down
13 changes: 13 additions & 0 deletions test/irb/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,19 @@ def test_ls_grep_empty
end
end

def test_ls_with_eval_error
[
"ls raise(Exception,'foo')\n",
"ls raise(Exception,'foo'), grep: /./\n",
"ls Integer, grep: raise(Exception,'foo')\n",
].each do |line|
out, err = execute_lines(line)
assert_empty err
assert_match(/Exception: foo/, out)
assert_not_match(/Maybe IRB bug!/, out)
end
end

def test_ls_with_no_singleton_class
out, err = execute_lines(
"ls 42",
Expand Down

0 comments on commit c82b568

Please sign in to comment.