Skip to content

Commit 9bd4407

Browse files
jcouballperlun
authored andcommitted
Do not allow changes to ENV to leak from test to test (#403)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent a4fbb6b commit 9bd4407

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

tests/test_helper.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,19 @@ def append_file(name, contents)
7979
f.puts contents
8080
end
8181
end
82-
82+
83+
# Runs a block inside an environment with customized ENV variables.
84+
# It restores the ENV after execution.
85+
#
86+
# @param [Proc] block block to be executed within the customized environment
87+
#
88+
def with_custom_env_variables(&block)
89+
saved_env = {}
90+
begin
91+
Git::Lib::ENV_VARIABLE_NAMES.each { |k| saved_env[k] = ENV[k] }
92+
return block.call
93+
ensure
94+
Git::Lib::ENV_VARIABLE_NAMES.each { |k| ENV[k] = saved_env[k] }
95+
end
96+
end
8397
end

tests/units/test_config.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,29 @@ def test_set_config
2929
end
3030

3131
def test_env_config
32-
assert_equal(Git::Base.config.git_ssh, nil)
33-
34-
ENV['GIT_SSH'] = '/env/git/ssh'
32+
with_custom_env_variables do
33+
begin
34+
assert_equal(Git::Base.config.git_ssh, nil)
35+
ENV['GIT_SSH'] = '/env/git/ssh'
3536

36-
assert_equal(Git::Base.config.git_ssh, '/env/git/ssh')
37+
assert_equal(Git::Base.config.git_ssh, '/env/git/ssh')
3738

38-
Git.configure do |config|
39-
config.binary_path = '/usr/bin/git'
40-
config.git_ssh = '/path/to/ssh/script'
41-
end
42-
43-
assert_equal(Git::Base.config.git_ssh, '/path/to/ssh/script')
39+
Git.configure do |config|
40+
config.binary_path = '/usr/bin/git'
41+
config.git_ssh = '/path/to/ssh/script'
42+
end
43+
44+
assert_equal(Git::Base.config.git_ssh, '/path/to/ssh/script')
4445

45-
@git.log
46-
ensure
47-
ENV['GIT_SSH'] = nil
46+
@git.log
47+
ensure
48+
ENV['GIT_SSH'] = nil
4849

49-
Git.configure do |config|
50-
config.binary_path = nil
51-
config.git_ssh = nil
50+
Git.configure do |config|
51+
config.binary_path = nil
52+
config.git_ssh = nil
53+
end
54+
end
5255
end
5356
end
54-
5557
end

tests/units/test_lib.rb

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,44 @@ def test_log_commits
7575
a = @lib.full_log_commits :count => 20
7676
assert_equal(20, a.size)
7777
end
78-
78+
7979
def test_environment_reset
80-
ENV['GIT_DIR'] = '/my/git/dir'
81-
ENV['GIT_WORK_TREE'] = '/my/work/tree'
82-
ENV['GIT_INDEX_FILE'] = 'my_index'
80+
with_custom_env_variables do
81+
ENV['GIT_DIR'] = '/my/git/dir'
82+
ENV['GIT_WORK_TREE'] = '/my/work/tree'
83+
ENV['GIT_INDEX_FILE'] = 'my_index'
8384

84-
@lib.log_commits :count => 10
85+
@lib.log_commits :count => 10
8586

86-
assert_equal(ENV['GIT_DIR'], '/my/git/dir')
87-
assert_equal(ENV['GIT_WORK_TREE'], '/my/work/tree')
88-
assert_equal(ENV['GIT_INDEX_FILE'],'my_index')
87+
assert_equal(ENV['GIT_DIR'], '/my/git/dir')
88+
assert_equal(ENV['GIT_WORK_TREE'], '/my/work/tree')
89+
assert_equal(ENV['GIT_INDEX_FILE'],'my_index')
90+
end
8991
end
9092

9193
def test_git_ssh_from_environment_is_passed_to_binary
92-
ENV['GIT_SSH'] = 'my/git-ssh-wrapper'
93-
94-
Dir.mktmpdir do |dir|
95-
output_path = File.join(dir, 'git_ssh_value')
96-
binary_path = File.join(dir, 'git')
97-
Git::Base.config.binary_path = binary_path
98-
File.open(binary_path, 'w') { |f|
99-
f << "echo $GIT_SSH > #{output_path}"
100-
}
101-
FileUtils.chmod(0700, binary_path)
102-
@lib.checkout('something')
103-
assert_equal("my/git-ssh-wrapper\n", File.read(output_path))
104-
end
105-
ensure
106-
Git.configure do |config|
107-
config.binary_path = nil
108-
config.git_ssh = nil
109-
end
94+
with_custom_env_variables do
95+
begin
96+
ENV['GIT_SSH'] = 'my/git-ssh-wrapper'
11097

111-
ENV['GIT_SSH'] = nil
98+
Dir.mktmpdir do |dir|
99+
output_path = File.join(dir, 'git_ssh_value')
100+
binary_path = File.join(dir, 'git')
101+
Git::Base.config.binary_path = binary_path
102+
File.open(binary_path, 'w') { |f|
103+
f << "echo $GIT_SSH > #{output_path}"
104+
}
105+
FileUtils.chmod(0700, binary_path)
106+
@lib.checkout('something')
107+
assert_equal("my/git-ssh-wrapper\n", File.read(output_path))
108+
end
109+
ensure
110+
Git.configure do |config|
111+
config.binary_path = nil
112+
config.git_ssh = nil
113+
end
114+
end
115+
end
112116
end
113117

114118
def test_revparse
File renamed without changes.

0 commit comments

Comments
 (0)