[ruby/irb] Clear ENV["XDG_CONFIG_HOME"] to avoid loading

user-defined irbrc in TestIRB::ConfigValidationTest
(https://github.com/ruby/irb/pull/982)

https://github.com/ruby/irb/commit/632da0ff29
This commit is contained in:
tomoya ishida 2024-07-18 19:56:14 +09:00 committed by git
parent b61e3a6218
commit c304bf13b5
4 changed files with 20 additions and 17 deletions

View File

@ -49,6 +49,19 @@ module TestIRB
!Pathname(__dir__).join("../../", "irb.gemspec").exist? !Pathname(__dir__).join("../../", "irb.gemspec").exist?
end end
def setup_envs(home:)
@backup_home = ENV["HOME"]
ENV["HOME"] = home
@backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
@backup_irbrc = ENV.delete("IRBRC")
end
def teardown_envs
ENV["HOME"] = @backup_home
ENV["XDG_CONFIG_HOME"] = @backup_xdg_config_home
ENV["IRBRC"] = @backup_irbrc
end
def save_encodings def save_encodings
@default_encoding = [Encoding.default_external, Encoding.default_internal] @default_encoding = [Encoding.default_external, Encoding.default_internal]
@stdio_encodings = [STDIN, STDOUT, STDERR].map {|io| [io.external_encoding, io.internal_encoding] } @stdio_encodings = [STDIN, STDOUT, STDERR].map {|io| [io.external_encoding, io.internal_encoding] }

View File

@ -15,9 +15,7 @@ module TestIRB
Dir.mkdir(@tmpdir) Dir.mkdir(@tmpdir)
end end
Dir.chdir(@tmpdir) Dir.chdir(@tmpdir)
@home_backup = ENV["HOME"] setup_envs(home: @tmpdir)
ENV["HOME"] = @tmpdir
@xdg_config_home_backup = ENV.delete("XDG_CONFIG_HOME")
save_encodings save_encodings
IRB.instance_variable_get(:@CONF).clear IRB.instance_variable_get(:@CONF).clear
IRB.instance_variable_set(:@existing_rc_name_generators, nil) IRB.instance_variable_set(:@existing_rc_name_generators, nil)
@ -25,8 +23,7 @@ module TestIRB
end end
def teardown def teardown
ENV["XDG_CONFIG_HOME"] = @xdg_config_home_backup teardown_envs
ENV["HOME"] = @home_backup
Dir.chdir(@pwd) Dir.chdir(@pwd)
FileUtils.rm_rf(@tmpdir) FileUtils.rm_rf(@tmpdir)
restore_encodings restore_encodings

View File

@ -12,19 +12,14 @@ module TestIRB
def setup def setup
@original_verbose, $VERBOSE = $VERBOSE, nil @original_verbose, $VERBOSE = $VERBOSE, nil
@tmpdir = Dir.mktmpdir("test_irb_history_") @tmpdir = Dir.mktmpdir("test_irb_history_")
@backup_home = ENV["HOME"] setup_envs(home: @tmpdir)
@backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
@backup_irbrc = ENV.delete("IRBRC")
@backup_default_external = Encoding.default_external @backup_default_external = Encoding.default_external
ENV["HOME"] = @tmpdir
IRB.instance_variable_set(:@existing_rc_name_generators, nil) IRB.instance_variable_set(:@existing_rc_name_generators, nil)
end end
def teardown def teardown
IRB.instance_variable_set(:@existing_rc_name_generators, nil) IRB.instance_variable_set(:@existing_rc_name_generators, nil)
ENV["HOME"] = @backup_home teardown_envs
ENV["XDG_CONFIG_HOME"] = @backup_xdg_config_home
ENV["IRBRC"] = @backup_irbrc
Encoding.default_external = @backup_default_external Encoding.default_external = @backup_default_external
$VERBOSE = @original_verbose $VERBOSE = @original_verbose
FileUtils.rm_rf(@tmpdir) FileUtils.rm_rf(@tmpdir)

View File

@ -270,17 +270,15 @@ module TestIRB
class ConfigValidationTest < TestCase class ConfigValidationTest < TestCase
def setup def setup
@original_home = ENV["HOME"]
@original_irbrc = ENV["IRBRC"]
# To prevent the test from using the user's .irbrc file # To prevent the test from using the user's .irbrc file
ENV["HOME"] = @home = Dir.mktmpdir @home = Dir.mktmpdir
setup_envs(home: @home)
super super
end end
def teardown def teardown
super super
ENV["IRBRC"] = @original_irbrc teardown_envs
ENV["HOME"] = @original_home
File.unlink(@irbrc) File.unlink(@irbrc)
Dir.rmdir(@home) Dir.rmdir(@home)
IRB.instance_variable_set(:@existing_rc_name_generators, nil) IRB.instance_variable_set(:@existing_rc_name_generators, nil)