Skip to content

Commit e46ddcc

Browse files
committed
Add Git::Lib#compare_version_to(other_version)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent dd5a24d commit e46ddcc

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
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

0 commit comments

Comments
 (0)