From 5c4657f8832bcbb9e7c3c50b6fe69212a86de153 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sun, 11 Feb 2024 05:17:37 +0000 Subject: [ruby/irb] Polish the exit! command and its tests (https://github.com/ruby/irb/pull/867) * Remove IRB.irb_exit! method It's not necessary to introduce a new method just for the exit! command at this moment. * Rename ExitForcedAction to ForceExit * Move force exit tests to a dedicated file * Fix nested history saving with exit! command Because we switched to use `Kernel#exit` instead of `exit!`, the outer session's ensure block in `Irb#run` will be run, which will save the history. This means the separate check to save history when force exiting is no longer necessary. * execute_lines helper should also capture IRB setup's output This prevents setup warnings from being printed to test output while allowing those output to be tested. * Update readme https://github.com/ruby/irb/commit/899d10ade1 --- lib/irb.rb | 15 +++------------ lib/irb/cmd/exit_forced_action.rb | 22 ---------------------- lib/irb/cmd/force_exit.rb | 22 ++++++++++++++++++++++ lib/irb/extend-command.rb | 2 +- 4 files changed, 26 insertions(+), 35 deletions(-) delete mode 100644 lib/irb/cmd/exit_forced_action.rb create mode 100644 lib/irb/cmd/force_exit.rb (limited to 'lib') diff --git a/lib/irb.rb b/lib/irb.rb index ad6ec78aa4..3830867e6a 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -889,10 +889,6 @@ module IRB throw :IRB_EXIT, false end - def IRB.irb_exit!(*) - throw :IRB_EXIT, true - end - # Aborts then interrupts irb. # # Will raise an Abort exception, or the given +exception+. @@ -972,8 +968,7 @@ module IRB conf[:IRB_RC].call(context) if conf[:IRB_RC] conf[:MAIN_CONTEXT] = context - supports_history_saving = conf[:SAVE_HISTORY] && context.io.support_history_saving? - save_history = !in_nested_session && supports_history_saving + save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving? if save_history context.io.load_history @@ -993,12 +988,8 @@ module IRB trap("SIGINT", prev_trap) 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 - end + context.io.save_history if save_history + Kernel.exit(0) if forced_exit end end diff --git a/lib/irb/cmd/exit_forced_action.rb b/lib/irb/cmd/exit_forced_action.rb deleted file mode 100644 index e5df75b682..0000000000 --- a/lib/irb/cmd/exit_forced_action.rb +++ /dev/null @@ -1,22 +0,0 @@ -# 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 diff --git a/lib/irb/cmd/force_exit.rb b/lib/irb/cmd/force_exit.rb new file mode 100644 index 0000000000..2b9f296865 --- /dev/null +++ b/lib/irb/cmd/force_exit.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative "nop" + +module IRB + # :stopdoc: + + module ExtendCommand + class ForceExit < Nop + category "IRB" + description "Exit the current process." + + def execute(*) + throw :IRB_EXIT, true + rescue UncaughtThrowError + Kernel.exit(0) + end + end + end + + # :startdoc: +end diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb index 2db2b80578..d303bf76da 100644 --- a/lib/irb/extend-command.rb +++ b/lib/irb/extend-command.rb @@ -37,7 +37,7 @@ module IRB # :nodoc: [:irb_quit, OVERRIDE_PRIVATE_ONLY], ], [ - :irb_exit!, :ExitForcedAction, "cmd/exit_forced_action", + :irb_exit!, :ForceExit, "cmd/force_exit", [:exit!, OVERRIDE_PRIVATE_ONLY], ], -- cgit v1.2.3