summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-12 11:25:16 +0000
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-12 11:25:16 +0000
commitda1ba13714f0a1025e4e60ef50d477a2603c1a3a (patch)
tree04b8046c765f7d1cec10db66cf45945211422459
parent8d1d4e3164a095ed488fd07c2eb75fa084378ab7 (diff)
irb.rb: preserve ARGV on binding.irb
This is not perfectly good solution (at least we don't want to have ARGV as default value of `argv` argument), but unfortunately IRB.setup and IRB.parse_opts are public methods and we can't make breaking change to those methods. We may deprecate using them and then make them private in the future, but the removal should not be in Ruby 2.5. So I kept their interface for now. [Bug #14162] [close GH-1770] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61149 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/irb.rb2
-rw-r--r--lib/irb/init.rb30
-rw-r--r--test/irb/test_init.rb25
3 files changed, 41 insertions, 16 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index b07bc80c95..3a5fdbc698 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -704,7 +704,7 @@ end
class Binding
# :nodoc:
def irb
- IRB.setup(eval("__FILE__"))
+ IRB.setup(eval("__FILE__"), argv: [])
workspace = IRB::WorkSpace.new(self)
STDOUT.print(workspace.code_around_binding)
IRB::Irb.new(workspace).run(IRB.conf)
diff --git a/lib/irb/init.rb b/lib/irb/init.rb
index 700466beda..c9f927fdd8 100644
--- a/lib/irb/init.rb
+++ b/lib/irb/init.rb
@@ -13,10 +13,10 @@
module IRB # :nodoc:
# initialize config
- def IRB.setup(ap_path)
+ def IRB.setup(ap_path, argv: ARGV)
IRB.init_config(ap_path)
IRB.init_error
- IRB.parse_opts
+ IRB.parse_opts(argv: argv)
IRB.run_config
IRB.load_modules
@@ -121,9 +121,9 @@ module IRB # :nodoc:
end
# option analyzing
- def IRB.parse_opts
+ def IRB.parse_opts(argv: ARGV)
load_path = []
- while opt = ARGV.shift
+ while opt = argv.shift
case opt
when "-f"
@CONF[:RC] = false
@@ -133,7 +133,7 @@ module IRB # :nodoc:
when "-w"
$VERBOSE = true
when /^-W(.+)?/
- opt = $1 || ARGV.shift
+ opt = $1 || argv.shift
case opt
when "0"
$VERBOSE = nil
@@ -143,19 +143,19 @@ module IRB # :nodoc:
$VERBOSE = true
end
when /^-r(.+)?/
- opt = $1 || ARGV.shift
+ opt = $1 || argv.shift
@CONF[:LOAD_MODULES].push opt if opt
when /^-I(.+)?/
- opt = $1 || ARGV.shift
+ opt = $1 || argv.shift
load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
when '-U'
set_encoding("UTF-8", "UTF-8")
when /^-E(.+)?/, /^--encoding(?:=(.+))?/
- opt = $1 || ARGV.shift
+ opt = $1 || argv.shift
set_encoding(*opt.split(':', 2))
when "--inspect"
- if /^-/ !~ ARGV.first
- @CONF[:INSPECT_MODE] = ARGV.shift
+ if /^-/ !~ argv.first
+ @CONF[:INSPECT_MODE] = argv.shift
else
@CONF[:INSPECT_MODE] = true
end
@@ -174,7 +174,7 @@ module IRB # :nodoc:
when "--noverbose"
@CONF[:VERBOSE] = false
when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/
- opt = $1 || ARGV.shift
+ opt = $1 || argv.shift
prompt_mode = opt.upcase.tr("-", "_").intern
@CONF[:PROMPT_MODE] = prompt_mode
when "--noprompt"
@@ -186,13 +186,13 @@ module IRB # :nodoc:
when "--tracer"
@CONF[:USE_TRACER] = true
when /^--back-trace-limit(?:=(.+))?/
- @CONF[:BACK_TRACE_LIMIT] = ($1 || ARGV.shift).to_i
+ @CONF[:BACK_TRACE_LIMIT] = ($1 || argv.shift).to_i
when /^--context-mode(?:=(.+))?/
- @CONF[:CONTEXT_MODE] = ($1 || ARGV.shift).to_i
+ @CONF[:CONTEXT_MODE] = ($1 || argv.shift).to_i
when "--single-irb"
@CONF[:SINGLE_IRB] = true
when /^--irb_debug(?:=(.+))?/
- @CONF[:DEBUG_LEVEL] = ($1 || ARGV.shift).to_i
+ @CONF[:DEBUG_LEVEL] = ($1 || argv.shift).to_i
when "-v", "--version"
print IRB.version, "\n"
exit 0
@@ -201,7 +201,7 @@ module IRB # :nodoc:
IRB.print_usage
exit 0
when "--"
- if opt = ARGV.shift
+ if opt = argv.shift
@CONF[:SCRIPT] = opt
$0 = opt
end
diff --git a/test/irb/test_init.rb b/test/irb/test_init.rb
new file mode 100644
index 0000000000..f2022253f9
--- /dev/null
+++ b/test/irb/test_init.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: false
+require "test/unit"
+require "irb"
+
+module TestIRB
+ class TestInit < Test::Unit::TestCase
+ def test_setup_with_argv_preserves_global_argv
+ argv = ["foo", "bar"]
+ with_argv(argv) do
+ IRB.setup(eval("__FILE__"), argv: [])
+ assert_equal argv, ARGV
+ end
+ end
+
+ private
+
+ def with_argv(argv)
+ orig = ARGV.dup
+ ARGV.replace(argv)
+ yield
+ ensure
+ ARGV.replace(orig)
+ end
+ end
+end