From ae1be05dde0d3c00a809018efd79500fc208e038 Mon Sep 17 00:00:00 2001 From: James Couball Date: Tue, 29 Jan 2019 17:31:49 -0800 Subject: [PATCH] Do not allow changes to ENV to leak from test to test Signed-off-by: James Couball --- tests/test_helper.rb | 16 ++++- tests/units/test_config.rb | 36 ++++++------ tests/units/test_lib.rb | 58 ++++++++++--------- ..._thread_safty.rb => test_thread_safety.rb} | 0 4 files changed, 65 insertions(+), 45 deletions(-) rename tests/units/{test_thread_safty.rb => test_thread_safety.rb} (100%) diff --git a/tests/test_helper.rb b/tests/test_helper.rb index ef739d32..3f5a4665 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -79,5 +79,19 @@ def append_file(name, contents) f.puts contents end end - + + # Runs a block inside an environment with customized ENV variables. + # It restores the ENV after execution. + # + # @param [Proc] block block to be executed within the customized environment + # + def with_custom_env_variables(&block) + saved_env = {} + begin + Git::Lib::ENV_VARIABLE_NAMES.each { |k| saved_env[k] = ENV[k] } + return block.call + ensure + Git::Lib::ENV_VARIABLE_NAMES.each { |k| ENV[k] = saved_env[k] } + end + end end diff --git a/tests/units/test_config.rb b/tests/units/test_config.rb index f30278df..7721ba24 100644 --- a/tests/units/test_config.rb +++ b/tests/units/test_config.rb @@ -29,27 +29,29 @@ def test_set_config end def test_env_config - assert_equal(Git::Base.config.git_ssh, nil) - - ENV['GIT_SSH'] = '/env/git/ssh' + with_custom_env_variables do + begin + assert_equal(Git::Base.config.git_ssh, nil) + ENV['GIT_SSH'] = '/env/git/ssh' - assert_equal(Git::Base.config.git_ssh, '/env/git/ssh') + assert_equal(Git::Base.config.git_ssh, '/env/git/ssh') - Git.configure do |config| - config.binary_path = '/usr/bin/git' - config.git_ssh = '/path/to/ssh/script' - end - - assert_equal(Git::Base.config.git_ssh, '/path/to/ssh/script') + Git.configure do |config| + config.binary_path = '/usr/bin/git' + config.git_ssh = '/path/to/ssh/script' + end + + assert_equal(Git::Base.config.git_ssh, '/path/to/ssh/script') - @git.log - ensure - ENV['GIT_SSH'] = nil + @git.log + ensure + ENV['GIT_SSH'] = nil - Git.configure do |config| - config.binary_path = nil - config.git_ssh = nil + Git.configure do |config| + config.binary_path = nil + config.git_ssh = nil + end + end end end - end diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 25e42022..8796dc88 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -75,40 +75,44 @@ def test_log_commits a = @lib.full_log_commits :count => 20 assert_equal(20, a.size) end - + def test_environment_reset - ENV['GIT_DIR'] = '/my/git/dir' - ENV['GIT_WORK_TREE'] = '/my/work/tree' - ENV['GIT_INDEX_FILE'] = 'my_index' + with_custom_env_variables do + ENV['GIT_DIR'] = '/my/git/dir' + ENV['GIT_WORK_TREE'] = '/my/work/tree' + ENV['GIT_INDEX_FILE'] = 'my_index' - @lib.log_commits :count => 10 + @lib.log_commits :count => 10 - assert_equal(ENV['GIT_DIR'], '/my/git/dir') - assert_equal(ENV['GIT_WORK_TREE'], '/my/work/tree') - assert_equal(ENV['GIT_INDEX_FILE'],'my_index') + assert_equal(ENV['GIT_DIR'], '/my/git/dir') + assert_equal(ENV['GIT_WORK_TREE'], '/my/work/tree') + assert_equal(ENV['GIT_INDEX_FILE'],'my_index') + end end def test_git_ssh_from_environment_is_passed_to_binary - ENV['GIT_SSH'] = 'my/git-ssh-wrapper' - - Dir.mktmpdir do |dir| - output_path = File.join(dir, 'git_ssh_value') - binary_path = File.join(dir, 'git') - Git::Base.config.binary_path = binary_path - File.open(binary_path, 'w') { |f| - f << "echo $GIT_SSH > #{output_path}" - } - FileUtils.chmod(0700, binary_path) - @lib.checkout('something') - assert_equal("my/git-ssh-wrapper\n", File.read(output_path)) - end - ensure - Git.configure do |config| - config.binary_path = nil - config.git_ssh = nil - end + with_custom_env_variables do + begin + ENV['GIT_SSH'] = 'my/git-ssh-wrapper' - ENV['GIT_SSH'] = nil + Dir.mktmpdir do |dir| + output_path = File.join(dir, 'git_ssh_value') + binary_path = File.join(dir, 'git') + Git::Base.config.binary_path = binary_path + File.open(binary_path, 'w') { |f| + f << "echo $GIT_SSH > #{output_path}" + } + FileUtils.chmod(0700, binary_path) + @lib.checkout('something') + assert_equal("my/git-ssh-wrapper\n", File.read(output_path)) + end + ensure + Git.configure do |config| + config.binary_path = nil + config.git_ssh = nil + end + end + end end def test_revparse diff --git a/tests/units/test_thread_safty.rb b/tests/units/test_thread_safety.rb similarity index 100% rename from tests/units/test_thread_safty.rb rename to tests/units/test_thread_safety.rb