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
|
2024-03-05 19:07:39 +03:00
|
|
|
ENV["HOME"] = @tmpdir = File.realpath(Dir.mktmpdir("test_irb_init_#{$$}"))
|
2021-04-15 16:24:02 +03:00
|
|
|
end
|
|
|
|
|
2024-03-16 18:19:59 +03:00
|
|
|
def reset_rc_name_generators
|
|
|
|
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
|
|
|
|
end
|
|
|
|
|
2021-04-15 16:24:02 +03:00
|
|
|
def teardown
|
|
|
|
ENV.update(@backup_env)
|
|
|
|
FileUtils.rm_rf(@tmpdir)
|
2022-09-16 20:25:15 +03:00
|
|
|
IRB.conf.delete(:SCRIPT)
|
2024-03-16 18:19:59 +03:00
|
|
|
reset_rc_name_generators
|
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
|
|
|
|
|
2024-03-05 19:07:39 +03:00
|
|
|
def test_rc_files
|
|
|
|
tmpdir = @tmpdir
|
|
|
|
Dir.chdir(tmpdir) do
|
2024-03-16 18:19:59 +03:00
|
|
|
home = ENV['HOME'] = "#{tmpdir}/home"
|
|
|
|
xdg_config_home = ENV['XDG_CONFIG_HOME'] = "#{tmpdir}/xdg"
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_empty(IRB.irbrc_files)
|
|
|
|
assert_equal("#{home}/.irb_history", IRB.rc_file('_history'))
|
|
|
|
FileUtils.mkdir_p(home)
|
|
|
|
FileUtils.mkdir_p("#{xdg_config_home}/irb")
|
|
|
|
FileUtils.mkdir_p("#{home}/.config/irb")
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_empty(IRB.irbrc_files)
|
|
|
|
assert_equal("#{xdg_config_home}/irb/irb_history", IRB.rc_file('_history'))
|
|
|
|
home_irbrc = "#{home}/.irbrc"
|
|
|
|
config_irbrc = "#{home}/.config/irb/irbrc"
|
|
|
|
xdg_config_irbrc = "#{xdg_config_home}/irb/irbrc"
|
|
|
|
[home_irbrc, config_irbrc, xdg_config_irbrc].each do |file|
|
|
|
|
FileUtils.touch(file)
|
2024-03-05 19:07:39 +03:00
|
|
|
end
|
2024-03-16 18:19:59 +03:00
|
|
|
current_dir_irbrcs = %w[.irbrc irbrc _irbrc $irbrc].map { |file| "#{tmpdir}/#{file}" }
|
|
|
|
current_dir_irbrcs.each { |file| FileUtils.touch(file) }
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_equal([xdg_config_irbrc, home_irbrc, *current_dir_irbrcs], IRB.irbrc_files)
|
|
|
|
assert_equal(xdg_config_irbrc.sub(/rc$/, '_history'), IRB.rc_file('_history'))
|
|
|
|
ENV['XDG_CONFIG_HOME'] = nil
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_equal([home_irbrc, config_irbrc, *current_dir_irbrcs], IRB.irbrc_files)
|
|
|
|
assert_equal(home_irbrc.sub(/rc$/, '_history'), IRB.rc_file('_history'))
|
|
|
|
ENV['XDG_CONFIG_HOME'] = ''
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_equal([home_irbrc, config_irbrc] + current_dir_irbrcs, IRB.irbrc_files)
|
|
|
|
assert_equal(home_irbrc.sub(/rc$/, '_history'), IRB.rc_file('_history'))
|
|
|
|
ENV['XDG_CONFIG_HOME'] = xdg_config_home
|
|
|
|
ENV['IRBRC'] = "#{tmpdir}/.irbrc"
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_equal([ENV['IRBRC'], xdg_config_irbrc, home_irbrc] + (current_dir_irbrcs - [ENV['IRBRC']]), IRB.irbrc_files)
|
|
|
|
assert_equal(ENV['IRBRC'] + '_history', IRB.rc_file('_history'))
|
|
|
|
ENV['IRBRC'] = ENV['HOME'] = ENV['XDG_CONFIG_HOME'] = nil
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_equal(current_dir_irbrcs, IRB.irbrc_files)
|
|
|
|
assert_nil(IRB.rc_file('_history'))
|
2024-03-05 19:07:39 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-16 18:19:59 +03:00
|
|
|
def test_duplicated_rc_files
|
2024-03-05 19:07:39 +03:00
|
|
|
tmpdir = @tmpdir
|
|
|
|
Dir.chdir(tmpdir) do
|
2024-03-16 18:19:59 +03:00
|
|
|
ENV['XDG_CONFIG_HOME'] = "#{ENV['HOME']}/.config"
|
|
|
|
FileUtils.mkdir_p("#{ENV['XDG_CONFIG_HOME']}/irb")
|
|
|
|
env_irbrc = ENV['IRBRC'] = "#{tmpdir}/_irbrc"
|
|
|
|
xdg_config_irbrc = "#{ENV['XDG_CONFIG_HOME']}/irb/irbrc"
|
|
|
|
home_irbrc = "#{ENV['HOME']}/.irbrc"
|
|
|
|
current_dir_irbrc = "#{tmpdir}/irbrc"
|
|
|
|
[env_irbrc, xdg_config_irbrc, home_irbrc, current_dir_irbrc].each do |file|
|
|
|
|
FileUtils.touch(file)
|
|
|
|
end
|
|
|
|
reset_rc_name_generators
|
|
|
|
assert_equal([env_irbrc, xdg_config_irbrc, home_irbrc, current_dir_irbrc], IRB.irbrc_files)
|
2024-03-05 19:07:39 +03:00
|
|
|
end
|
2019-11-19 14:58:11 +03:00
|
|
|
end
|
|
|
|
|
2024-01-01 13:58:32 +03:00
|
|
|
def test_sigint_restore_default
|
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'] : []
|
2024-01-01 13:58:32 +03:00
|
|
|
# IRB should restore SIGINT handler
|
|
|
|
status = assert_in_out_err(bundle_exec + %w[-W0 -rirb -e Signal.trap("SIGINT","DEFAULT");binding.irb;loop{Process.kill("SIGINT",$$)} -- -f --], "exit\n", //, //)
|
|
|
|
Process.kill("SIGKILL", status.pid) if !status.exited? && !status.stopped? && !status.signaled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sigint_restore_block
|
|
|
|
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
|
|
|
|
# IRB should restore SIGINT handler
|
|
|
|
status = assert_in_out_err(bundle_exec + %w[-W0 -rirb -e x=false;Signal.trap("SIGINT"){x=true};binding.irb;loop{Process.kill("SIGINT",$$);if(x);break;end} -- -f --], "exit\n", //, //)
|
2021-04-01 13:06:10 +03:00
|
|
|
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
|
|
|
|
|
2024-02-06 19:46:41 +03:00
|
|
|
def test_option_tracer
|
|
|
|
argv = %w[--tracer]
|
|
|
|
IRB.setup(eval("__FILE__"), argv: argv)
|
|
|
|
assert_equal(true, IRB.conf[:USE_TRACER])
|
|
|
|
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
|
2023-12-18 17:36:55 +03:00
|
|
|
|
2024-05-10 14:40:54 +03:00
|
|
|
class ConfigValidationTest < TestCase
|
|
|
|
def setup
|
|
|
|
# To prevent the test from using the user's .irbrc file
|
2024-07-18 13:56:14 +03:00
|
|
|
@home = Dir.mktmpdir
|
|
|
|
setup_envs(home: @home)
|
2024-05-10 14:40:54 +03:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2024-07-18 13:56:14 +03:00
|
|
|
teardown_envs
|
2024-05-10 14:40:54 +03:00
|
|
|
File.unlink(@irbrc)
|
2024-05-12 07:47:10 +03:00
|
|
|
Dir.rmdir(@home)
|
2024-06-12 10:47:58 +03:00
|
|
|
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
|
2024-05-10 14:40:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_irb_name_converts_non_string_values_to_string
|
|
|
|
assert_no_irb_validation_error(<<~'RUBY')
|
|
|
|
IRB.conf[:IRB_NAME] = :foo
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert_equal "foo", IRB.conf[:IRB_NAME]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_irb_rc_name_only_takes_callable_objects
|
|
|
|
assert_irb_validation_error(<<~'RUBY', "IRB.conf[:IRB_RC] should be a callable object. Got :foo.")
|
|
|
|
IRB.conf[:IRB_RC] = :foo
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_back_trace_limit_only_accepts_integers
|
|
|
|
assert_irb_validation_error(<<~'RUBY', "IRB.conf[:BACK_TRACE_LIMIT] should be an integer. Got \"foo\".")
|
|
|
|
IRB.conf[:BACK_TRACE_LIMIT] = "foo"
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_prompt_only_accepts_hash
|
|
|
|
assert_irb_validation_error(<<~'RUBY', "IRB.conf[:PROMPT] should be a Hash. Got \"foo\".")
|
|
|
|
IRB.conf[:PROMPT] = "foo"
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval_history_only_accepts_integers
|
|
|
|
assert_irb_validation_error(<<~'RUBY', "IRB.conf[:EVAL_HISTORY] should be an integer. Got \"foo\".")
|
|
|
|
IRB.conf[:EVAL_HISTORY] = "foo"
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assert_irb_validation_error(rc_content, error_message)
|
|
|
|
write_rc rc_content
|
|
|
|
|
|
|
|
assert_raise_with_message(TypeError, error_message) do
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_irb_validation_error(rc_content)
|
|
|
|
write_rc rc_content
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
IRB.setup(__FILE__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_rc(content)
|
|
|
|
@irbrc = Tempfile.new('irbrc')
|
|
|
|
@irbrc.write(content)
|
|
|
|
@irbrc.close
|
|
|
|
ENV['IRBRC'] = @irbrc.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-18 17:36:55 +03:00
|
|
|
class InitIntegrationTest < IntegrationTestCase
|
2024-05-10 14:40:54 +03:00
|
|
|
def setup
|
|
|
|
super
|
2023-12-18 17:36:55 +03:00
|
|
|
|
|
|
|
write_ruby <<~'RUBY'
|
|
|
|
binding.irb
|
|
|
|
RUBY
|
2024-05-10 14:40:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_error_in_rc_file_is_warned
|
|
|
|
write_rc <<~'IRBRC'
|
|
|
|
require "file_that_does_not_exist"
|
|
|
|
IRBRC
|
2023-12-18 17:36:55 +03:00
|
|
|
|
|
|
|
output = run_ruby_file do
|
|
|
|
type "'foobar'"
|
|
|
|
type "exit"
|
|
|
|
end
|
|
|
|
|
|
|
|
# IRB session should still be started
|
|
|
|
assert_includes output, "foobar"
|
|
|
|
assert_includes output, 'cannot load such file -- file_that_does_not_exist (LoadError)'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_normal_errors_in_rc_file_is_warned
|
|
|
|
write_rc <<~'IRBRC'
|
|
|
|
raise "I'm an error"
|
|
|
|
IRBRC
|
|
|
|
|
|
|
|
output = run_ruby_file do
|
|
|
|
type "'foobar'"
|
|
|
|
type "exit"
|
|
|
|
end
|
|
|
|
|
|
|
|
# IRB session should still be started
|
|
|
|
assert_includes output, "foobar"
|
|
|
|
assert_includes output, 'I\'m an error (RuntimeError)'
|
|
|
|
end
|
|
|
|
end
|
2017-12-12 14:25:16 +03:00
|
|
|
end
|