Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear ENV["XDG_CONFIG_HOME"] to avoid loading user-defined irbrc in test #982

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/irb/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def ruby_core?
!Pathname(__dir__).join("../../", "irb.gemspec").exist?
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
@default_encoding = [Encoding.default_external, Encoding.default_internal]
@stdio_encodings = [STDIN, STDOUT, STDERR].map {|io| [io.external_encoding, io.internal_encoding] }
Expand Down
7 changes: 2 additions & 5 deletions test/irb/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ def setup
Dir.mkdir(@tmpdir)
end
Dir.chdir(@tmpdir)
@home_backup = ENV["HOME"]
ENV["HOME"] = @tmpdir
@xdg_config_home_backup = ENV.delete("XDG_CONFIG_HOME")
setup_envs(home: @tmpdir)
save_encodings
IRB.instance_variable_get(:@CONF).clear
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
@is_win = (RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/)
end

def teardown
ENV["XDG_CONFIG_HOME"] = @xdg_config_home_backup
ENV["HOME"] = @home_backup
teardown_envs
Dir.chdir(@pwd)
FileUtils.rm_rf(@tmpdir)
restore_encodings
Expand Down
9 changes: 2 additions & 7 deletions test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ class HistoryTest < TestCase
def setup
@original_verbose, $VERBOSE = $VERBOSE, nil
@tmpdir = Dir.mktmpdir("test_irb_history_")
@backup_home = ENV["HOME"]
@backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
@backup_irbrc = ENV.delete("IRBRC")
setup_envs(home: @tmpdir)
@backup_default_external = Encoding.default_external
ENV["HOME"] = @tmpdir
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
end

def teardown
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
ENV["HOME"] = @backup_home
ENV["XDG_CONFIG_HOME"] = @backup_xdg_config_home
ENV["IRBRC"] = @backup_irbrc
teardown_envs
Encoding.default_external = @backup_default_external
$VERBOSE = @original_verbose
FileUtils.rm_rf(@tmpdir)
Expand Down
8 changes: 3 additions & 5 deletions test/irb/test_init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,15 @@ def with_argv(argv)

class ConfigValidationTest < TestCase
def setup
@original_home = ENV["HOME"]
@original_irbrc = ENV["IRBRC"]
# To prevent the test from using the user's .irbrc file
ENV["HOME"] = @home = Dir.mktmpdir
@home = Dir.mktmpdir
setup_envs(home: @home)
super
end

def teardown
super
ENV["IRBRC"] = @original_irbrc
ENV["HOME"] = @original_home
teardown_envs
File.unlink(@irbrc)
Dir.rmdir(@home)
IRB.instance_variable_set(:@existing_rc_name_generators, nil)
Expand Down
Loading