Skip to content

Commit

Permalink
Remove exit command workaround, handle IRB_EXIT in debug_readline (#923)
Browse files Browse the repository at this point in the history
* Remove exit and exti! command workaround when executed outside of IRB

Command was a method. It could be executed outside of IRB.
Workaround for it is no longer needed.

* Handle IRB_EXIT in debug mode

* Add exit and exit! command in rdbg mode
  • Loading branch information
tompng authored Apr 20, 2024
1 parent c6bbc42 commit 0b5dd6a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
26 changes: 16 additions & 10 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -962,20 +962,26 @@ def debug_readline(binding)
#
# Irb#eval_input will simply return the input, and we need to pass it to the
# debugger.
input = if IRB.conf[:SAVE_HISTORY] && context.io.support_history_saving?
# Previous IRB session's history has been saved when `Irb#run` is exited We need
# to make sure the saved history is not saved again by resetting the counter
context.io.reset_history_counter
input = nil
forced_exit = catch(:IRB_EXIT) do
if IRB.conf[:SAVE_HISTORY] && context.io.support_history_saving?
# Previous IRB session's history has been saved when `Irb#run` is exited We need
# to make sure the saved history is not saved again by resetting the counter
context.io.reset_history_counter

begin
eval_input
ensure
context.io.save_history
begin
input = eval_input
ensure
context.io.save_history
end
else
input = eval_input
end
else
eval_input
false
end

Kernel.exit if forced_exit

if input&.include?("\n")
@line_no += input.count("\n") - 1
end
Expand Down
4 changes: 1 addition & 3 deletions lib/irb/command/exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class Exit < Base
category "IRB"
description "Exit the current irb session."

def execute(*)
def execute(_arg)
IRB.irb_exit
rescue UncaughtThrowError
Kernel.exit
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/irb/command/force_exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class ForceExit < Base
category "IRB"
description "Exit the current process."

def execute(*)
def execute(_arg)
throw :IRB_EXIT, true
rescue UncaughtThrowError
Kernel.exit!
end
end
end
Expand Down
12 changes: 0 additions & 12 deletions test/irb/command/test_force_exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,5 @@ def foo

assert_match(/irb\(main\):001> 123/, output)
end

def test_forced_exit_out_of_irb_session
write_ruby <<~'ruby'
at_exit { puts 'un' + 'reachable' }
binding.irb
exit! # this will call exit! method overrided by command
ruby
output = run_ruby_file do
type "exit"
end
assert_not_include(output, 'unreachable')
end
end
end
28 changes: 23 additions & 5 deletions test/irb/test_debugger_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,46 @@ def test_catch
def test_exit
write_ruby <<~'RUBY'
binding.irb
puts "hello"
puts "he" + "llo"
RUBY

output = run_ruby_file do
type "next"
type "debug"
type "exit"
end

assert_match(/irb\(main\):001> next/, output)
assert_match(/irb:rdbg\(main\):002>/, output)
assert_match(/hello/, output)
end

def test_force_exit
write_ruby <<~'RUBY'
binding.irb
puts "he" + "llo"
RUBY

output = run_ruby_file do
type "debug"
type "exit!"
end

assert_match(/irb:rdbg\(main\):002>/, output)
assert_not_match(/hello/, output)
end

def test_quit
write_ruby <<~'RUBY'
binding.irb
puts "he" + "llo"
RUBY

output = run_ruby_file do
type "next"
type "debug"
type "quit!"
end

assert_match(/irb\(main\):001> next/, output)
assert_match(/irb:rdbg\(main\):002>/, output)
assert_not_match(/hello/, output)
end

def test_prompt_line_number_continues
Expand Down

0 comments on commit 0b5dd6a

Please sign in to comment.