Fix VersionSplit comparison

The tests on Python revealed a bug with comparing dev versions.

Switch to comparing by integers and setting non-dev version to infinity.

There is still an issue with suffix release comparisons beyond single
digits but will leave that for now.
This commit is contained in:
Calum Lind 2018-04-25 20:10:52 +01:00
parent 196aa48727
commit 7cc9aaca49
2 changed files with 13 additions and 11 deletions

View File

@ -992,25 +992,27 @@ class VersionSplit(object):
self.version = [int(x) for x in vs[0].split('.') if x.isdigit()]
self.version_string = ''.join(str(x) for x in vs[0].split('.') if x.isdigit())
self.suffix = None
self.dev = False
self.dev = None
if len(vs) > 1:
if vs[1].startswith(('rc', 'a', 'b', 'c')):
self.suffix = vs[1]
if vs[-1].startswith('dev'):
self.dev = vs[-1]
try:
# Store only the dev numeral.
self.dev = int(vs[-1].rsplit('dev')[1])
except ValueError:
# Implicit dev numeral is 0.
self.dev = 0
def get_comparable_versions(self, other):
"""
Returns a 2-tuple of lists for use in the comparison
methods.
"""
# PEP 386 versions with .devN precede release version
if bool(self.dev) != bool(other.dev):
if self.dev != 'dev':
self.dev = not self.dev
if other.dev != 'dev':
other.dev = not other.dev
# PEP 386 versions with .devN precede release version so default
# non-dev versions to infinity while dev versions are ints.
self.dev = float('inf') if self.dev is None else self.dev
other.dev = float('inf') if other.dev is None else other.dev
# If there is no suffix we use z because we want final
# to appear after alpha, beta, and rc alphabetically.
v1 = [self.version, self.suffix or 'z', self.dev]

View File

@ -99,10 +99,10 @@ class CommonTestCase(unittest.TestCase):
self.assertTrue(VersionSplit('1.2.1') < VersionSplit('1.2.2'))
self.assertTrue(VersionSplit('1.1.9') < VersionSplit('1.2.2'))
self.assertTrue(VersionSplit('1.2.2') > VersionSplit('1.2.1'))
self.assertTrue(VersionSplit('1.2.2') < VersionSplit('1.2.2-dev'))
self.assertTrue(VersionSplit('1.2.2') > VersionSplit('1.2.2-dev0'))
self.assertTrue(VersionSplit('1.2.2-dev') < VersionSplit('1.3.0-rc2'))
self.assertTrue(VersionSplit('1.2.2') > VersionSplit('1.2.2-rc2'))
self.assertTrue(VersionSplit('1.2.2-rc2-dev') > VersionSplit('1.2.2-rc2'))
self.assertTrue(VersionSplit('1.2.2-rc2-dev') < VersionSplit('1.2.2-rc2'))
self.assertTrue(VersionSplit('1.2.2-rc3') > VersionSplit('1.2.2-rc2'))
self.assertTrue(VersionSplit('0.14.9') == VersionSplit('0.14.9'))
self.assertTrue(VersionSplit('0.14.9') > VersionSplit('0.14.5'))