Skip to content

Commit

Permalink
Fix comparison of multiple version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
albertyw committed Oct 14, 2023
1 parent 83634ef commit 2d7df0d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions req_update/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def setUp(self) -> None:
self.util = util.Util()

def test_ints(self) -> None:
self.assertFalse(self.util.compare_versions('a10', 'a12'))
self.assertFalse(self.util.compare_versions('10a', '12a'))
self.assertTrue(self.util.compare_versions('a10', 'a12'))
self.assertTrue(self.util.compare_versions('10a', '12a'))

def test_regex(self) -> None:
tests: dict[str, list[str]] = {
Expand All @@ -197,6 +197,9 @@ def test_regex(self) -> None:
'1.21': ['1.19', '1.2'],
'18-slim': ['16-slim', '15-slim'],
'3.11-slim-bookworm': ['3.10-slim-bookworm', '3.9-slim-bookworm'],
'2.12.0': ['1.17.7'],
'v2.12.0': ['v1.17.7'],
'v2.12.0-alpine': ['v1.17.7-alpine'],
}
for newest, olders in tests.items():
for older in olders:
Expand Down
6 changes: 3 additions & 3 deletions req_update/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def compare_versions(self, current: str, proposed: str) -> bool:
proposed_structure = re.sub(structure_regex, "", proposed)
if current_structure != proposed_structure:
return False
num_regex = r"(\.|^)([0-9]+)(?![a-zA-Z])"
current_nums = [found[1] for found in re.findall(num_regex, current)]
proposed_nums = [found[1] for found in re.findall(num_regex, proposed)]
num_regex = r"\d+"
current_nums = re.findall(num_regex, current)
proposed_nums = re.findall(num_regex, proposed)
for compares in zip(current_nums, proposed_nums):
if int(compares[0]) < int(compares[1]):
return True
Expand Down

0 comments on commit 2d7df0d

Please sign in to comment.