[ruby/irb] Introduce exit! command
(https://github.com/ruby/irb/pull/851) * Added failing test for when writing history on exit * Save history on exit * Exit early when calling Kernel.exit * use status 0 for kernel.exit * Added test for nested sessions * Update lib/irb.rb --------- https://github.com/ruby/irb/commit/c0a5f31679 Co-authored-by: Stan Lo <stan001212@gmail.com>
This commit is contained in:
parent
f960fbc102
commit
429eeb09f2
19
lib/irb.rb
19
lib/irb.rb
@ -886,7 +886,11 @@ module IRB
|
|||||||
|
|
||||||
# Quits irb
|
# Quits irb
|
||||||
def IRB.irb_exit(*)
|
def IRB.irb_exit(*)
|
||||||
throw :IRB_EXIT
|
throw :IRB_EXIT, false
|
||||||
|
end
|
||||||
|
|
||||||
|
def IRB.irb_exit!(*)
|
||||||
|
throw :IRB_EXIT, true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Aborts then interrupts irb.
|
# Aborts then interrupts irb.
|
||||||
@ -968,7 +972,8 @@ module IRB
|
|||||||
conf[:IRB_RC].call(context) if conf[:IRB_RC]
|
conf[:IRB_RC].call(context) if conf[:IRB_RC]
|
||||||
conf[:MAIN_CONTEXT] = context
|
conf[:MAIN_CONTEXT] = context
|
||||||
|
|
||||||
save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving?
|
supports_history_saving = conf[:SAVE_HISTORY] && context.io.support_history_saving?
|
||||||
|
save_history = !in_nested_session && supports_history_saving
|
||||||
|
|
||||||
if save_history
|
if save_history
|
||||||
context.io.load_history
|
context.io.load_history
|
||||||
@ -979,15 +984,23 @@ module IRB
|
|||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
catch(:IRB_EXIT) do
|
forced_exit = false
|
||||||
|
|
||||||
|
forced_exit = catch(:IRB_EXIT) do
|
||||||
eval_input
|
eval_input
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
trap("SIGINT", prev_trap)
|
trap("SIGINT", prev_trap)
|
||||||
conf[:AT_EXIT].each{|hook| hook.call}
|
conf[:AT_EXIT].each{|hook| hook.call}
|
||||||
|
|
||||||
|
if forced_exit
|
||||||
|
context.io.save_history if supports_history_saving
|
||||||
|
Kernel.exit(0)
|
||||||
|
else
|
||||||
context.io.save_history if save_history
|
context.io.save_history if save_history
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Evaluates input for this session.
|
# Evaluates input for this session.
|
||||||
def eval_input
|
def eval_input
|
||||||
|
22
lib/irb/cmd/exit_forced_action.rb
Normal file
22
lib/irb/cmd/exit_forced_action.rb
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "nop"
|
||||||
|
|
||||||
|
module IRB
|
||||||
|
# :stopdoc:
|
||||||
|
|
||||||
|
module ExtendCommand
|
||||||
|
class ExitForcedAction < Nop
|
||||||
|
category "IRB"
|
||||||
|
description "Exit the current process."
|
||||||
|
|
||||||
|
def execute(*)
|
||||||
|
IRB.irb_exit!
|
||||||
|
rescue UncaughtThrowError
|
||||||
|
Kernel.exit(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# :startdoc:
|
||||||
|
end
|
@ -36,6 +36,11 @@ module IRB # :nodoc:
|
|||||||
[:quit, OVERRIDE_PRIVATE_ONLY],
|
[:quit, OVERRIDE_PRIVATE_ONLY],
|
||||||
[:irb_quit, OVERRIDE_PRIVATE_ONLY],
|
[:irb_quit, OVERRIDE_PRIVATE_ONLY],
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
:irb_exit!, :ExitForcedAction, "cmd/exit_forced_action",
|
||||||
|
[:exit!, OVERRIDE_PRIVATE_ONLY],
|
||||||
|
],
|
||||||
|
|
||||||
[
|
[
|
||||||
:irb_current_working_workspace, :CurrentWorkingWorkspace, "cmd/chws",
|
:irb_current_working_workspace, :CurrentWorkingWorkspace, "cmd/chws",
|
||||||
[:cwws, NO_OVERRIDE],
|
[:cwws, NO_OVERRIDE],
|
||||||
|
@ -255,6 +255,47 @@ module TestIRB
|
|||||||
assert_match(/irb\(main\):001> next/, output)
|
assert_match(/irb\(main\):001> next/, output)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_forced_exit_finishes_process_when_nested_sessions
|
||||||
|
write_ruby <<~'ruby'
|
||||||
|
puts "First line"
|
||||||
|
puts "Second line"
|
||||||
|
binding.irb
|
||||||
|
puts "Third line"
|
||||||
|
binding.irb
|
||||||
|
puts "Fourth line"
|
||||||
|
ruby
|
||||||
|
|
||||||
|
output = run_ruby_file do
|
||||||
|
type "123"
|
||||||
|
type "456"
|
||||||
|
type "exit!"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_match(/First line\r\n/, output)
|
||||||
|
assert_match(/Second line\r\n/, output)
|
||||||
|
assert_match(/irb\(main\):001> 123/, output)
|
||||||
|
assert_match(/irb\(main\):002> 456/, output)
|
||||||
|
refute_match(/Third line\r\n/, output)
|
||||||
|
refute_match(/Fourth line\r\n/, output)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_forced_exit
|
||||||
|
write_ruby <<~'ruby'
|
||||||
|
puts "Hello"
|
||||||
|
binding.irb
|
||||||
|
ruby
|
||||||
|
|
||||||
|
output = run_ruby_file do
|
||||||
|
type "123"
|
||||||
|
type "456"
|
||||||
|
type "exit!"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_match(/Hello\r\n/, output)
|
||||||
|
assert_match(/irb\(main\):001> 123/, output)
|
||||||
|
assert_match(/irb\(main\):002> 456/, output)
|
||||||
|
end
|
||||||
|
|
||||||
def test_quit
|
def test_quit
|
||||||
write_ruby <<~'RUBY'
|
write_ruby <<~'RUBY'
|
||||||
binding.irb
|
binding.irb
|
||||||
|
@ -379,6 +379,24 @@ module TestIRB
|
|||||||
HISTORY
|
HISTORY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_history_saving_with_exit!
|
||||||
|
write_history ""
|
||||||
|
|
||||||
|
write_ruby <<~'RUBY'
|
||||||
|
binding.irb
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
run_ruby_file do
|
||||||
|
type "'starting session'"
|
||||||
|
type "exit!"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal <<~HISTORY, @history_file.open.read
|
||||||
|
'starting session'
|
||||||
|
exit!
|
||||||
|
HISTORY
|
||||||
|
end
|
||||||
|
|
||||||
def test_history_saving_with_nested_sessions_and_prior_history
|
def test_history_saving_with_nested_sessions_and_prior_history
|
||||||
write_history <<~HISTORY
|
write_history <<~HISTORY
|
||||||
old_history_1
|
old_history_1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user