Skip to content

Commit e64c2f6

Browse files
authored
Refactor tests for read_tree, write_tree, and commit_tree (#679)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 0bb965d commit e64c2f6

File tree

4 files changed

+250
-112
lines changed

4 files changed

+250
-112
lines changed

git.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
3232
s.add_development_dependency 'bump', '~> 0.10'
3333
s.add_development_dependency 'create_github_release', '~> 0.2'
3434
s.add_development_dependency 'minitar', '~> 0.9'
35+
s.add_development_dependency 'mocha', '~> 2.1'
3536
s.add_development_dependency 'rake', '~> 13.0'
3637
s.add_development_dependency 'test-unit', '~> 3.3'
3738

lib/git/lib.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ def commit_tree(tree, opts = {})
10431043
arr_opts = []
10441044
arr_opts << tree
10451045
arr_opts << '-p' << opts[:parent] if opts[:parent]
1046-
arr_opts += [opts[:parents]].map { |p| ['-p', p] }.flatten if opts[:parents]
1046+
arr_opts += Array(opts[:parents]).map { |p| ['-p', p] }.flatten if opts[:parents]
10471047
command('commit-tree', *arr_opts, redirect: "< #{escape t.path}")
10481048
end
10491049

@@ -1113,7 +1113,7 @@ def current_command_version
11131113
# @example
11141114
# lib.current_command_version #=> [2, 42, 0]
11151115
#
1116-
# lib.compare_version_to(2, 41, 0) #=> 1
1116+
# lib.compare_version_to(2, 41, 0) #=> 1
11171117
# lib.compare_version_to(2, 42, 0) #=> 0
11181118
# lib.compare_version_to(2, 43, 0) #=> -1
11191119
#

tests/test_helper.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'fileutils'
33
require 'minitar'
44
require 'test/unit'
5+
require 'mocha/test_unit'
56
require 'tmpdir'
67

78
require "git"
@@ -148,6 +149,7 @@ def with_custom_env_variables(&block)
148149
# @param expected_command_line [Array<String>] The expected arguments to be sent to Git::Lib#command
149150
# @param git_cmd [Symbol] the method to be called on the Git::Base object
150151
# @param git_cmd_args [Array<Object>] The arguments to be sent to the git_cmd method
152+
# @param git_output [String] The output to be returned by the Git::Lib#command method
151153
#
152154
# @yield [git] An initialization block
153155
# The initialization block is called after a test project is created with Git.init.
@@ -157,9 +159,11 @@ def with_custom_env_variables(&block)
157159
#
158160
# @return [void]
159161
#
160-
def assert_command_line(expected_command_line, git_cmd, git_cmd_args)
162+
def assert_command_line(expected_command_line, git_cmd, git_cmd_args, git_output = nil)
161163
actual_command_line = nil
162164

165+
command_output = ''
166+
163167
in_temp_dir do |path|
164168
git = Git.init('test_project')
165169

@@ -169,17 +173,26 @@ def assert_command_line(expected_command_line, git_cmd, git_cmd_args)
169173
# Mock the Git::Lib#command method to capture the actual command line args
170174
git.lib.define_singleton_method(:command) do |cmd, *opts, &block|
171175
actual_command_line = [cmd, *opts.flatten]
176+
git_output
172177
end
173178

174-
git.send(git_cmd, *git_cmd_args)
179+
command_output = git.send(git_cmd, *git_cmd_args)
175180
end
176181
end
177182

178183
assert_equal(expected_command_line, actual_command_line)
184+
185+
command_output
179186
end
180187

181188
def assert_child_process_success(&block)
182189
yield
183190
assert_equal 0, $CHILD_STATUS.exitstatus, "Child process failed with exitstatus #{$CHILD_STATUS.exitstatus}"
184191
end
192+
193+
def windows_platform?
194+
# Check if on Windows via RUBY_PLATFORM (CRuby) and RUBY_DESCRIPTION (JRuby)
195+
win_platform_regex = /mingw|mswin/
196+
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
197+
end
185198
end

0 commit comments

Comments
 (0)