Skip to content

Commit

Permalink
[ruby/irb] Added setup and teardown to TestIRB::TestInit
Browse files Browse the repository at this point in the history
Not to be affected by existing rc files in all tests.

ruby/irb@bf434892b4
  • Loading branch information
nobu authored and matzbot committed Apr 26, 2021
1 parent 8fdc45c commit 5219b4d
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions test/irb/test_init.rb
Expand Up @@ -5,6 +5,19 @@

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)
end

def test_setup_with_argv_preserves_global_argv
argv = ["foo", "bar"]
with_argv(argv) do
Expand All @@ -20,12 +33,8 @@ def test_setup_with_minimum_argv_does_not_change_dollar0
end

def test_rc_file
backup_irbrc = ENV.delete("IRBRC") # This is for RVM...
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
backup_home = ENV["HOME"]
Dir.mktmpdir("test_irb_init_#{$$}") do |tmpdir|
ENV["HOME"] = tmpdir

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"))
Expand All @@ -34,19 +43,11 @@ def test_rc_file
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
end
ensure
ENV["HOME"] = backup_home
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
ENV["IRBRC"] = backup_irbrc
end

def test_rc_file_in_subdir
backup_irbrc = ENV.delete("IRBRC") # This is for RVM...
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
backup_home = ENV["HOME"]
Dir.mktmpdir("test_irb_init_#{$$}") do |tmpdir|
ENV["HOME"] = tmpdir

tmpdir = @tmpdir
Dir.chdir(tmpdir) do
FileUtils.mkdir_p("#{tmpdir}/mydir")
Dir.chdir("#{tmpdir}/mydir") do
IRB.conf[:RC_NAME_GENERATOR] = nil
Expand All @@ -58,10 +59,6 @@ def test_rc_file_in_subdir
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
end
end
ensure
ENV["HOME"] = backup_home
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
ENV["IRBRC"] = backup_irbrc
end

def test_recovery_sigint
Expand Down

0 comments on commit 5219b4d

Please sign in to comment.