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

Remove exit command workaround, handle IRB_EXIT in debug_readline #923

Merged
merged 3 commits into from
Apr 20, 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
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
Loading