1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/irb/test_init.rb
Jeremy Evans b07db96744 [ruby/irb] Support --noscript option to not use first non-option argument as script
Also add --script option to turn the option back on.

Previously there wasn't a way to get an interactive IRB session
and access arguments provided on the command line.

Additionally, handle `-` as script as stdin. In Unix-like tools, `-`
means to take standard input instead of a file.  This doesn't
result in exactly the same output for:

```
echo 'p ARGV' > args.rb; irb args.rb a b c
```

and

```
echo 'p ARGV' | irb - a b c
```

Due to how irb handles whether stdin is a tty.

However, this change allows use of `-` as a argument, instead of
giving an unrecognized switch error. This required some small
changes to context.rb (to handle `-` as standard input) and
input-method.rb (to have FileInputMethod accept IO arguments in
addition to strings).

Implements [Feature #15371]

https://github.com/ruby/irb/commit/4192683ba2
2022-09-17 02:25:26 +09:00

145 lines
4.3 KiB
Ruby

# frozen_string_literal: false
require "test/unit"
require "irb"
require "fileutils"
module TestIRB
class TestInit < Test::Unit::TestCase
def setup
# IRBRC is for RVM...
@backup_env = %w[HOME XDG_CONFIG_HOME IRBRC].each_with_object({}) do |env, hash|
hash[env] = ENV.delete(env)
end
ENV["HOME"] = @tmpdir = Dir.mktmpdir("test_irb_init_#{$$}")
end
def teardown
ENV.update(@backup_env)
FileUtils.rm_rf(@tmpdir)
IRB.conf.delete(:SCRIPT)
end
def test_setup_with_argv_preserves_global_argv
argv = ["foo", "bar"]
with_argv(argv) do
IRB.setup(eval("__FILE__"), argv: %w[-f])
assert_equal argv, ARGV
end
end
def test_setup_with_minimum_argv_does_not_change_dollar0
orig = $0.dup
IRB.setup(eval("__FILE__"), argv: %w[-f])
assert_equal orig, $0
end
def test_rc_file
tmpdir = @tmpdir
Dir.chdir(tmpdir) do
IRB.conf[:RC_NAME_GENERATOR] = nil
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
IRB.conf[:RC_NAME_GENERATOR] = nil
FileUtils.touch(tmpdir+"/.irb#{IRB::IRBRC_EXT}")
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
end
end
def test_rc_file_in_subdir
tmpdir = @tmpdir
Dir.chdir(tmpdir) do
FileUtils.mkdir_p("#{tmpdir}/mydir")
Dir.chdir("#{tmpdir}/mydir") do
IRB.conf[:RC_NAME_GENERATOR] = nil
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
IRB.conf[:RC_NAME_GENERATOR] = nil
FileUtils.touch(tmpdir+"/.irb#{IRB::IRBRC_EXT}")
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
end
end
end
def test_recovery_sigint
pend "This test gets stuck on Solaris for unknown reason; contribution is welcome" if RUBY_PLATFORM =~ /solaris/
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
status = assert_in_out_err(bundle_exec + %w[-W0 -rirb -e binding.irb;loop{Process.kill("SIGINT",$$)} -- -f --], "exit\n", //, //)
Process.kill("SIGKILL", status.pid) if !status.exited? && !status.stopped? && !status.signaled?
end
def test_no_color_environment_variable
orig_no_color = ENV['NO_COLOR']
orig_use_colorize = IRB.conf[:USE_COLORIZE]
IRB.conf[:USE_COLORIZE] = true
assert IRB.conf[:USE_COLORIZE]
ENV['NO_COLOR'] = 'true'
IRB.setup(__FILE__)
refute IRB.conf[:USE_COLORIZE]
ENV['NO_COLOR'] = nil
IRB.setup(__FILE__)
assert IRB.conf[:USE_COLORIZE]
ensure
ENV['NO_COLOR'] = orig_no_color
IRB.conf[:USE_COLORIZE] = orig_use_colorize
end
def test_noscript
argv = %w[--noscript -- -f]
IRB.setup(eval("__FILE__"), argv: argv)
assert_nil IRB.conf[:SCRIPT]
assert_equal(['-f'], argv)
argv = %w[--noscript -- a]
IRB.setup(eval("__FILE__"), argv: argv)
assert_nil IRB.conf[:SCRIPT]
assert_equal(['a'], argv)
argv = %w[--noscript a]
IRB.setup(eval("__FILE__"), argv: argv)
assert_nil IRB.conf[:SCRIPT]
assert_equal(['a'], argv)
argv = %w[--script --noscript a]
IRB.setup(eval("__FILE__"), argv: argv)
assert_nil IRB.conf[:SCRIPT]
assert_equal(['a'], argv)
argv = %w[--noscript --script a]
IRB.setup(eval("__FILE__"), argv: argv)
assert_equal('a', IRB.conf[:SCRIPT])
assert_equal([], argv)
end
def test_dash
argv = %w[-]
IRB.setup(eval("__FILE__"), argv: argv)
assert_equal('-', IRB.conf[:SCRIPT])
assert_equal([], argv)
argv = %w[-- -]
IRB.setup(eval("__FILE__"), argv: argv)
assert_equal('-', IRB.conf[:SCRIPT])
assert_equal([], argv)
argv = %w[-- - -f]
IRB.setup(eval("__FILE__"), argv: argv)
assert_equal('-', IRB.conf[:SCRIPT])
assert_equal(['-f'], argv)
end
private
def with_argv(argv)
orig = ARGV.dup
ARGV.replace(argv)
yield
ensure
ARGV.replace(orig)
end
end
end