Skip to content

Commit

Permalink
Refactor tests for read_tree, write_tree, and commit_tree (#679)
Browse files Browse the repository at this point in the history
Signed-off-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
jcouball committed Dec 26, 2023
1 parent 0bb965d commit e64c2f6
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 112 deletions.
1 change: 1 addition & 0 deletions git.gemspec
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bump', '~> 0.10'
s.add_development_dependency 'create_github_release', '~> 0.2'
s.add_development_dependency 'minitar', '~> 0.9'
s.add_development_dependency 'mocha', '~> 2.1'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'test-unit', '~> 3.3'

Expand Down
4 changes: 2 additions & 2 deletions lib/git/lib.rb
Expand Up @@ -1043,7 +1043,7 @@ def commit_tree(tree, opts = {})
arr_opts = []
arr_opts << tree
arr_opts << '-p' << opts[:parent] if opts[:parent]
arr_opts += [opts[:parents]].map { |p| ['-p', p] }.flatten if opts[:parents]
arr_opts += Array(opts[:parents]).map { |p| ['-p', p] }.flatten if opts[:parents]
command('commit-tree', *arr_opts, redirect: "< #{escape t.path}")
end

Expand Down Expand Up @@ -1113,7 +1113,7 @@ def current_command_version
# @example
# lib.current_command_version #=> [2, 42, 0]
#
# lib.compare_version_to(2, 41, 0) #=> 1
# lib.compare_version_to(2, 41, 0) #=> 1
# lib.compare_version_to(2, 42, 0) #=> 0
# lib.compare_version_to(2, 43, 0) #=> -1
#
Expand Down
17 changes: 15 additions & 2 deletions tests/test_helper.rb
Expand Up @@ -2,6 +2,7 @@
require 'fileutils'
require 'minitar'
require 'test/unit'
require 'mocha/test_unit'
require 'tmpdir'

require "git"
Expand Down Expand Up @@ -148,6 +149,7 @@ def with_custom_env_variables(&block)
# @param expected_command_line [Array<String>] The expected arguments to be sent to Git::Lib#command
# @param git_cmd [Symbol] the method to be called on the Git::Base object
# @param git_cmd_args [Array<Object>] The arguments to be sent to the git_cmd method
# @param git_output [String] The output to be returned by the Git::Lib#command method
#
# @yield [git] An initialization block
# The initialization block is called after a test project is created with Git.init.
Expand All @@ -157,9 +159,11 @@ def with_custom_env_variables(&block)
#
# @return [void]
#
def assert_command_line(expected_command_line, git_cmd, git_cmd_args)
def assert_command_line(expected_command_line, git_cmd, git_cmd_args, git_output = nil)
actual_command_line = nil

command_output = ''

in_temp_dir do |path|
git = Git.init('test_project')

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

git.send(git_cmd, *git_cmd_args)
command_output = git.send(git_cmd, *git_cmd_args)
end
end

assert_equal(expected_command_line, actual_command_line)

command_output
end

def assert_child_process_success(&block)
yield
assert_equal 0, $CHILD_STATUS.exitstatus, "Child process failed with exitstatus #{$CHILD_STATUS.exitstatus}"
end

def windows_platform?
# Check if on Windows via RUBY_PLATFORM (CRuby) and RUBY_DESCRIPTION (JRuby)
win_platform_regex = /mingw|mswin/
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
end
end

0 comments on commit e64c2f6

Please sign in to comment.