Skip to content

Commit

Permalink
Merge pull request #13 from matan1008/bugfix/ls/fix-version-compare
Browse files Browse the repository at this point in the history
filevercmp: Fix 0 padded versions compare
  • Loading branch information
matan1008 committed Apr 22, 2023
2 parents 39b7c9a + 04689f8 commit 9910513
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pygnuutils/filevercmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def verrevcmp(s1: str, s2: str):
return s1_c - s2_c
s1 = s1[1:]
s2 = s2[1:]
s1.lstrip('0')
s2.lstrip('0')
s1 = s1.lstrip('0')
s2 = s2.lstrip('0')
while (s1 and s1[0].isdigit()) and (s2 and s2[0].isdigit()):
if not first_diff:
first_diff = ord(s1[0]) - ord(s2[0])
Expand Down
15 changes: 15 additions & 0 deletions tests/test_filevercmp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import cmp_to_key

from pygnuutils.filevercmp import filevercmp

examples = [
Expand Down Expand Up @@ -79,3 +81,16 @@ def test_filevercmp():
assert i > j
else:
assert i == j


example_with_zeros = [
'.', '..', '.001', '.01', '.1', '..001', '..01', '..1', '...', '...001', '...01', '...1', '0', '1~~', '1~', '1',
'1.~~', '1.~', '1.', '1.0~', '1.0', '1.1~~', '1.1~', '1.01', '1.1', '1.1a', '1.1b', '1.1pre1', '1.1rc1', '1.1rc2',
'1.1-rc1', '1.1-rc2', '1.01.01', '1.01.1', '1.1.01', '1.1.1', '1.1/', '1/', 'a', 'aa', 'aaa', 'aab', 'ab', 'abb',
'b'
]


def test_filevercmp_with_zeros():
expected = sorted(example_with_zeros, key=cmp_to_key(lambda file1, file2: filevercmp(file1, file2)))
assert expected == example_with_zeros

0 comments on commit 9910513

Please sign in to comment.