Skip to content

Commit b1799f6

Browse files
authored
Update test of 'git worktree add' with no commits (#670)
* Add Git::Lib#compare_version_to(other_version) Signed-off-by: James Couball <jcouball@yahoo.com> * Update test of 1git worktree add1 with no commits git-2.42.0 changes the behavior of `git worktree add` when there are no commits in the repository. Prior to 2.42.0, an error would result with creating a new worktree. Starting wtih 2.42.0, git will create a new, orphaned branch for the worktree. Signed-off-by: James Couball <jcouball@yahoo.com> * Rewrite test_repack so it works in Windows Signed-off-by: James Couball <jcouball@yahoo.com> --------- Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent dd5a24d commit b1799f6

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

lib/git/lib.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,22 @@ def current_command_version
11081108
version_parts.fill(0, version_parts.length...3)
11091109
end
11101110

1111+
# Returns current_command_version <=> other_version
1112+
#
1113+
# @example
1114+
# lib.current_command_version #=> [2, 42, 0]
1115+
#
1116+
# lib.compare_version_to(2, 41, 0) #=> 1
1117+
# lib.compare_version_to(2, 42, 0) #=> 0
1118+
# lib.compare_version_to(2, 43, 0) #=> -1
1119+
#
1120+
# @param other_version [Array<Object>] the other version to compare to
1121+
# @return [Integer] -1 if this version is less than other_version, 0 if equal, or 1 if greater than
1122+
#
1123+
def compare_version_to(*other_version)
1124+
current_command_version <=> other_version
1125+
end
1126+
11111127
def required_command_version
11121128
[1, 6]
11131129
end

tests/units/test_lib.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,15 @@ def test_show
325325
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
326326
end
327327

328+
def test_compare_version_to
329+
lib = Git::Lib.new(nil, nil)
330+
current_version = [2, 42, 0]
331+
lib.define_singleton_method(:current_command_version) { current_version }
332+
assert lib.compare_version_to(0, 43, 9) == 1
333+
assert lib.compare_version_to(2, 41, 0) == 1
334+
assert lib.compare_version_to(2, 42, 0) == 0
335+
assert lib.compare_version_to(2, 42, 1) == -1
336+
assert lib.compare_version_to(2, 43, 0) == -1
337+
assert lib.compare_version_to(3, 0, 0) == -1
338+
end
328339
end

tests/units/test_repack.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
require 'test_helper'
44

55
class TestRepack < Test::Unit::TestCase
6-
def test_repack
6+
test 'should be able to call repack with the right args' do
77
in_bare_repo_clone do |r1|
88
new_file('new_file', 'new content')
9-
109
r1.add
1110
r1.commit('my commit')
1211

13-
# see how big the repo is
14-
size1 = r1.repo_size
15-
16-
r1.repack
12+
# assert_nothing_raised { r1.repack }
1713

18-
# see how big the repo is now, should be smaller
19-
assert(size1 > r1.repo_size)
14+
expected_command_line = ['repack', '-a', '-d']
15+
git_cmd = :repack
16+
git_cmd_args = []
17+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
2018
end
2119
end
2220
end

tests/units/test_worktree.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def setup
3131
end
3232

3333
test 'adding a worktree when there are no commits should fail' do
34+
omit('Omitted since git version is >= 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) >= 0
35+
3436
in_temp_dir do |path|
3537
Dir.mkdir('main_worktree')
3638
Dir.chdir('main_worktree') do
@@ -47,6 +49,33 @@ def setup
4749
end
4850
end
4951

52+
test 'adding a worktree when there are no commits should succeed' do
53+
omit('Omitted since git version is < 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) < 0
54+
55+
in_temp_dir do |path|
56+
Dir.mkdir('main_worktree')
57+
Dir.chdir('main_worktree') do
58+
`git init`
59+
# `git commit --allow-empty -m "first commit"`
60+
end
61+
62+
git = Git.open('main_worktree')
63+
64+
assert_nothing_raised do
65+
git.worktree('feature1').add
66+
end
67+
68+
assert_equal(2, git.worktrees.size)
69+
70+
expected_worktree_dirs = [
71+
File.join(path, 'main_worktree'),
72+
File.join(path, 'feature1')
73+
].each_with_index do |expected_worktree_dir, i|
74+
assert_equal(expected_worktree_dir, git.worktrees.to_a[i].dir)
75+
end
76+
end
77+
end
78+
5079
test 'adding a worktree when there is at least one commit should succeed' do
5180
in_temp_dir do |path|
5281
Dir.mkdir('main_worktree')

0 commit comments

Comments
 (0)