2017-12-12 14:25:16 +03:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require "irb"
|
2019-11-19 14:58:11 +03:00
|
|
|
require "fileutils"
|
2017-12-12 14:25:16 +03:00
|
|
|
|
2022-11-04 01:13:07 +03:00
|
|
|
require_relative "helper"
|
|
|
|
|
2017-12-12 14:25:16 +03:00
|
|
|
module TestIRB
|
2023-06-13 14:02:22 +03:00
|
|
|
class InitTest < TestCase
|
2021-04-15 16:24:02 +03:00
|
|
|
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)
|
2022-09-16 20:25:15 +03:00
|
|
|
IRB.conf.delete(:SCRIPT)
|
2021-04-15 16:24:02 +03:00
|
|
|
end
|
|
|
|
|
2017-12-12 14:25:16 +03:00
|
|
|
def test_setup_with_argv_preserves_global_argv
|
|
|
|
argv = ["foo", "bar"]
|
|
|
|
with_argv(argv) do
|
2018-08-18 07:09:48 +03:00
|
|
|
IRB.setup(eval("__FILE__"), argv: %w[-f])
|
2017-12-12 14:25:16 +03:00
|
|
|
assert_equal argv, ARGV
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-18 07:09:48 +03:00
|
|
|
def test_setup_with_minimum_argv_does_not_change_dollar0
|
2017-12-12 16:56:48 +03:00
|
|
|
orig = $0.dup
|
2018-08-18 07:09:48 +03:00
|
|
|
IRB.setup(eval("__FILE__"), argv: %w[-f])
|
2017-12-12 16:56:48 +03:00
|
|
|
assert_equal orig, $0
|
|
|
|
end
|
|
|
|
|
2019-11-19 14:58:11 +03:00
|
|
|
def test_rc_file
|
2021-04-15 16:24:02 +03:00
|
|
|
tmpdir = @tmpdir
|
|
|
|
Dir.chdir(tmpdir) do
|
2022-03-31 06:51:04 +03:00
|
|
|
ENV["XDG_CONFIG_HOME"] = "#{tmpdir}/xdg"
|
2019-11-19 14:58:11 +03:00
|
|
|
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"))
|
2022-03-31 06:51:04 +03:00
|
|
|
assert_file.not_exist?(tmpdir+"/xdg")
|
2019-11-19 14:58:11 +03:00
|
|
|
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"))
|
2022-03-31 06:51:04 +03:00
|
|
|
assert_file.not_exist?(tmpdir+"/xdg")
|
2019-11-19 14:58:11 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rc_file_in_subdir
|
2021-04-15 16:24:02 +03:00
|
|
|
tmpdir = @tmpdir
|
|
|
|
Dir.chdir(tmpdir) do
|
2019-11-19 14:58:11 +03:00
|
|
|
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
|
|
|
|
|
2021-04-01 13:06:10 +03:00
|
|
|
def test_recovery_sigint
|
2021-11-05 14:03:11 +03:00
|
|
|
pend "This test gets stuck on Solaris for unknown reason; contribution is welcome" if RUBY_PLATFORM =~ /solaris/
|
2021-04-01 13:06:10 +03:00
|
|
|
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
|
|
|
|
|
2020-05-18 04:12:02 +03:00
|
|
|
def test_no_color_environment_variable
|
2021-05-24 06:13:46 +03:00
|
|
|
orig_no_color = ENV['NO_COLOR']
|
|
|
|
orig_use_colorize = IRB.conf[:USE_COLORIZE]
|
|
|
|
IRB.conf[:USE_COLORIZE] = true
|
2020-05-18 04:12:02 +03:00
|
|
|
|
|
|
|
assert IRB.conf[:USE_COLORIZE]
|
|
|
|
|
|
|
|
ENV['NO_COLOR'] = 'true'
|
2020-08-12 05:32:44 +03:00
|
|
|
IRB.setup(__FILE__)
|
2020-05-18 04:12:02 +03:00
|
|
|
refute IRB.conf[:USE_COLORIZE]
|
|
|
|
|
2022-10-28 12:30:20 +03:00
|
|
|
ENV['NO_COLOR'] = ''
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert IRB.conf[:USE_COLORIZE]
|
|
|
|
|
2020-05-18 04:12:02 +03:00
|
|
|
ENV['NO_COLOR'] = nil
|
2020-08-12 05:32:44 +03:00
|
|
|
IRB.setup(__FILE__)
|
2020-05-18 04:12:02 +03:00
|
|
|
assert IRB.conf[:USE_COLORIZE]
|
|
|
|
ensure
|
2021-05-24 06:13:46 +03:00
|
|
|
ENV['NO_COLOR'] = orig_no_color
|
|
|
|
IRB.conf[:USE_COLORIZE] = orig_use_colorize
|
2020-05-18 04:12:02 +03:00
|
|
|
end
|
|
|
|
|
2022-12-06 03:53:28 +03:00
|
|
|
def test_use_autocomplete_environment_variable
|
|
|
|
orig_use_autocomplete_env = ENV['IRB_USE_AUTOCOMPLETE']
|
|
|
|
orig_use_autocomplete_conf = IRB.conf[:USE_AUTOCOMPLETE]
|
|
|
|
|
|
|
|
ENV['IRB_USE_AUTOCOMPLETE'] = nil
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert IRB.conf[:USE_AUTOCOMPLETE]
|
|
|
|
|
|
|
|
ENV['IRB_USE_AUTOCOMPLETE'] = ''
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert IRB.conf[:USE_AUTOCOMPLETE]
|
|
|
|
|
|
|
|
ENV['IRB_USE_AUTOCOMPLETE'] = 'false'
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
refute IRB.conf[:USE_AUTOCOMPLETE]
|
|
|
|
|
|
|
|
ENV['IRB_USE_AUTOCOMPLETE'] = 'true'
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert IRB.conf[:USE_AUTOCOMPLETE]
|
|
|
|
ensure
|
|
|
|
ENV["IRB_USE_AUTOCOMPLETE"] = orig_use_autocomplete_env
|
|
|
|
IRB.conf[:USE_AUTOCOMPLETE] = orig_use_autocomplete_conf
|
|
|
|
end
|
|
|
|
|
2023-11-21 03:04:36 +03:00
|
|
|
def test_completor_environment_variable
|
|
|
|
orig_use_autocomplete_env = ENV['IRB_COMPLETOR']
|
|
|
|
orig_use_autocomplete_conf = IRB.conf[:COMPLETOR]
|
|
|
|
|
|
|
|
ENV['IRB_COMPLETOR'] = nil
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert_equal(:regexp, IRB.conf[:COMPLETOR])
|
|
|
|
|
|
|
|
ENV['IRB_COMPLETOR'] = 'regexp'
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert_equal(:regexp, IRB.conf[:COMPLETOR])
|
|
|
|
|
|
|
|
ENV['IRB_COMPLETOR'] = 'type'
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
assert_equal(:type, IRB.conf[:COMPLETOR])
|
|
|
|
|
|
|
|
ENV['IRB_COMPLETOR'] = 'regexp'
|
|
|
|
IRB.setup(__FILE__, argv: ['--type-completor'])
|
|
|
|
assert_equal :type, IRB.conf[:COMPLETOR]
|
|
|
|
|
|
|
|
ENV['IRB_COMPLETOR'] = 'type'
|
|
|
|
IRB.setup(__FILE__, argv: ['--regexp-completor'])
|
|
|
|
assert_equal :regexp, IRB.conf[:COMPLETOR]
|
|
|
|
ensure
|
|
|
|
ENV['IRB_COMPLETOR'] = orig_use_autocomplete_env
|
|
|
|
IRB.conf[:COMPLETOR] = orig_use_autocomplete_conf
|
|
|
|
end
|
|
|
|
|
2023-11-09 16:15:22 +03:00
|
|
|
def test_completor_setup_with_argv
|
|
|
|
orig_completor_conf = IRB.conf[:COMPLETOR]
|
|
|
|
|
|
|
|
# Default is :regexp
|
|
|
|
IRB.setup(__FILE__, argv: [])
|
|
|
|
assert_equal :regexp, IRB.conf[:COMPLETOR]
|
|
|
|
|
|
|
|
IRB.setup(__FILE__, argv: ['--type-completor'])
|
|
|
|
assert_equal :type, IRB.conf[:COMPLETOR]
|
|
|
|
|
|
|
|
IRB.setup(__FILE__, argv: ['--regexp-completor'])
|
|
|
|
assert_equal :regexp, IRB.conf[:COMPLETOR]
|
|
|
|
ensure
|
|
|
|
IRB.conf[:COMPLETOR] = orig_completor_conf
|
|
|
|
end
|
|
|
|
|
2022-09-16 20:25:15 +03:00
|
|
|
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
|
|
|
|
|
2017-12-12 14:25:16 +03:00
|
|
|
private
|
|
|
|
|
|
|
|
def with_argv(argv)
|
|
|
|
orig = ARGV.dup
|
|
|
|
ARGV.replace(argv)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ARGV.replace(orig)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|