Skip to content

Commit bf43489

Browse files
committed
Added setup and teardown to TestIRB::TestInit
Not to be affected by existing rc files in all tests.
1 parent 2c94813 commit bf43489

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

test/irb/test_init.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
module TestIRB
77
class TestInit < Test::Unit::TestCase
8+
def setup
9+
# IRBRC is for RVM...
10+
@backup_env = %w[HOME XDG_CONFIG_HOME IRBRC].each_with_object({}) do |env, hash|
11+
hash[env] = ENV.delete(env)
12+
end
13+
ENV["HOME"] = @tmpdir = Dir.mktmpdir("test_irb_init_#{$$}")
14+
end
15+
16+
def teardown
17+
ENV.update(@backup_env)
18+
FileUtils.rm_rf(@tmpdir)
19+
end
20+
821
def test_setup_with_argv_preserves_global_argv
922
argv = ["foo", "bar"]
1023
with_argv(argv) do
@@ -20,12 +33,8 @@ def test_setup_with_minimum_argv_does_not_change_dollar0
2033
end
2134

2235
def test_rc_file
23-
backup_irbrc = ENV.delete("IRBRC") # This is for RVM...
24-
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
25-
backup_home = ENV["HOME"]
26-
Dir.mktmpdir("test_irb_init_#{$$}") do |tmpdir|
27-
ENV["HOME"] = tmpdir
28-
36+
tmpdir = @tmpdir
37+
Dir.chdir(tmpdir) do
2938
IRB.conf[:RC_NAME_GENERATOR] = nil
3039
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
3140
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
@@ -34,19 +43,11 @@ def test_rc_file
3443
assert_equal(tmpdir+"/.irb#{IRB::IRBRC_EXT}", IRB.rc_file)
3544
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
3645
end
37-
ensure
38-
ENV["HOME"] = backup_home
39-
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
40-
ENV["IRBRC"] = backup_irbrc
4146
end
4247

4348
def test_rc_file_in_subdir
44-
backup_irbrc = ENV.delete("IRBRC") # This is for RVM...
45-
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
46-
backup_home = ENV["HOME"]
47-
Dir.mktmpdir("test_irb_init_#{$$}") do |tmpdir|
48-
ENV["HOME"] = tmpdir
49-
49+
tmpdir = @tmpdir
50+
Dir.chdir(tmpdir) do
5051
FileUtils.mkdir_p("#{tmpdir}/mydir")
5152
Dir.chdir("#{tmpdir}/mydir") do
5253
IRB.conf[:RC_NAME_GENERATOR] = nil
@@ -58,10 +59,6 @@ def test_rc_file_in_subdir
5859
assert_equal(tmpdir+"/.irb_history", IRB.rc_file("_history"))
5960
end
6061
end
61-
ensure
62-
ENV["HOME"] = backup_home
63-
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
64-
ENV["IRBRC"] = backup_irbrc
6562
end
6663

6764
def test_recovery_sigint

0 commit comments

Comments
 (0)