Skip to content

Commit b060e47

Browse files
committed
test: verify that command line envionment variables are set as expected
1 parent 1a5092a commit b060e47

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

lib/git/lib.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ def env_overrides
15471547
'GIT_DIR' => @git_dir,
15481548
'GIT_WORK_TREE' => @git_work_dir,
15491549
'GIT_INDEX_FILE' => @git_index_file,
1550-
'GIT_SSH' => Git::Base.config.git_ssh
1550+
'GIT_SSH' => Git::Base.config.git_ssh,
15511551
}
15521552
end
15531553

tests/test_helper.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def append_file(name, contents)
131131
#
132132
# @return [void]
133133
#
134-
def assert_command_line_eq(expected_command_line, method: :command, mocked_output: nil)
134+
def assert_command_line_eq(expected_command_line, method: :command, mocked_output: nil, include_env: false)
135135
actual_command_line = nil
136136

137137
command_output = ''
@@ -140,7 +140,11 @@ def assert_command_line_eq(expected_command_line, method: :command, mocked_outpu
140140
git = Git.init('test_project')
141141

142142
git.lib.define_singleton_method(method) do |*cmd, **opts, &block|
143-
actual_command_line = [*cmd, opts]
143+
if include_env
144+
actual_command_line = [env_overrides, *cmd, opts]
145+
else
146+
actual_command_line = [*cmd, opts]
147+
end
144148
mocked_output
145149
end
146150

@@ -149,6 +153,8 @@ def assert_command_line_eq(expected_command_line, method: :command, mocked_outpu
149153
end
150154
end
151155

156+
expected_command_line = expected_command_line.call if expected_command_line.is_a?(Proc)
157+
152158
assert_equal(expected_command_line, actual_command_line)
153159

154160
command_output
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
# frozen_string_literal: true
3+
4+
require 'test_helper'
5+
6+
class TestCommandLineEnvOverrides < Test::Unit::TestCase
7+
test 'it should set the expected environment variables' do
8+
expected_command_line = nil
9+
expected_command_line_proc = ->{ expected_command_line }
10+
assert_command_line_eq(expected_command_line_proc, include_env: true) do |git|
11+
expected_env = {
12+
'GIT_DIR' => git.lib.git_dir,
13+
'GIT_INDEX_FILE' => git.lib.git_index_file,
14+
'GIT_SSH' => nil,
15+
'GIT_WORK_TREE' => git.lib.git_work_dir
16+
}
17+
expected_command_line = [expected_env, 'checkout', {}]
18+
19+
git.checkout
20+
end
21+
end
22+
23+
test 'it should set the GIT_SSH environment variable from Git::Base.config.git_ssh' do
24+
expected_command_line = nil
25+
expected_command_line_proc = ->{ expected_command_line }
26+
27+
saved_git_ssh = Git::Base.config.git_ssh
28+
begin
29+
Git::Base.config.git_ssh = 'ssh -i /path/to/key'
30+
31+
assert_command_line_eq(expected_command_line_proc, include_env: true) do |git|
32+
# Set the expected command line
33+
34+
expected_env = {
35+
'GIT_DIR' => git.lib.git_dir,
36+
'GIT_INDEX_FILE' => git.lib.git_index_file,
37+
'GIT_SSH' => 'ssh -i /path/to/key',
38+
'GIT_WORK_TREE' => git.lib.git_work_dir
39+
}
40+
41+
expected_command_line = [expected_env, 'checkout', {}]
42+
git.checkout
43+
end
44+
ensure
45+
Git::Base.config.git_ssh = saved_git_ssh
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)