Skip to content

Refactor tests for Git::Lib methods #read_tree, #write_tree, and #commit_tree #679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions git.gemspec
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading