Skip to content

Update test of 'git worktree add' with no commits #670

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 3 commits into from
Sep 18, 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
16 changes: 16 additions & 0 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,22 @@ def current_command_version
version_parts.fill(0, version_parts.length...3)
end

# Returns current_command_version <=> other_version
#
# @example
# lib.current_command_version #=> [2, 42, 0]
#
# lib.compare_version_to(2, 41, 0) #=> 1
# lib.compare_version_to(2, 42, 0) #=> 0
# lib.compare_version_to(2, 43, 0) #=> -1
#
# @param other_version [Array<Object>] the other version to compare to
# @return [Integer] -1 if this version is less than other_version, 0 if equal, or 1 if greater than
#
def compare_version_to(*other_version)
current_command_version <=> other_version
end

def required_command_version
[1, 6]
end
Expand Down
11 changes: 11 additions & 0 deletions tests/units/test_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,15 @@ def test_show
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
end

def test_compare_version_to
lib = Git::Lib.new(nil, nil)
current_version = [2, 42, 0]
lib.define_singleton_method(:current_command_version) { current_version }
assert lib.compare_version_to(0, 43, 9) == 1
assert lib.compare_version_to(2, 41, 0) == 1
assert lib.compare_version_to(2, 42, 0) == 0
assert lib.compare_version_to(2, 42, 1) == -1
assert lib.compare_version_to(2, 43, 0) == -1
assert lib.compare_version_to(3, 0, 0) == -1
end
end
14 changes: 6 additions & 8 deletions tests/units/test_repack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
require 'test_helper'

class TestRepack < Test::Unit::TestCase
def test_repack
test 'should be able to call repack with the right args' do
in_bare_repo_clone do |r1|
new_file('new_file', 'new content')

r1.add
r1.commit('my commit')

# see how big the repo is
size1 = r1.repo_size

r1.repack
# assert_nothing_raised { r1.repack }

# see how big the repo is now, should be smaller
assert(size1 > r1.repo_size)
expected_command_line = ['repack', '-a', '-d']
git_cmd = :repack
git_cmd_args = []
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
end
end
end
29 changes: 29 additions & 0 deletions tests/units/test_worktree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def setup
end

test 'adding a worktree when there are no commits should fail' do
omit('Omitted since git version is >= 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) >= 0

in_temp_dir do |path|
Dir.mkdir('main_worktree')
Dir.chdir('main_worktree') do
Expand All @@ -47,6 +49,33 @@ def setup
end
end

test 'adding a worktree when there are no commits should succeed' do
omit('Omitted since git version is < 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) < 0

in_temp_dir do |path|
Dir.mkdir('main_worktree')
Dir.chdir('main_worktree') do
`git init`
# `git commit --allow-empty -m "first commit"`
end

git = Git.open('main_worktree')

assert_nothing_raised do
git.worktree('feature1').add
end

assert_equal(2, git.worktrees.size)

expected_worktree_dirs = [
File.join(path, 'main_worktree'),
File.join(path, 'feature1')
].each_with_index do |expected_worktree_dir, i|
assert_equal(expected_worktree_dir, git.worktrees.to_a[i].dir)
end
end
end

test 'adding a worktree when there is at least one commit should succeed' do
in_temp_dir do |path|
Dir.mkdir('main_worktree')
Expand Down