Make version_to_string() and version_from_string() more tolerant

With those changes they can be used to parse plugin API versions too.

Test cases were modified accordingly.

Main changes:
```python
>>> version_from_string("1.0")
(1, 0, 0, 'final', 0)
>>> version_from_string("1.0.1")
(1, 0, 1, 'final', 0)
```
This commit is contained in:
Laurent Monin
2014-04-23 15:13:24 +02:00
parent fb7e730932
commit d7ba54dc51
2 changed files with 50 additions and 4 deletions

View File

@@ -58,11 +58,15 @@ def version_to_string(version, short=False):
return version_str
_version_re = re.compile("(\d+)[._](\d+)[._](\d+)[._]?(dev|final)[._]?(\d+)$")
_version_re = re.compile("(\d+)[._](\d+)(?:[._](\d+)[._]?(?:(dev|final)[._]?(\d+))?)?$")
def version_from_string(version_str):
m = _version_re.search(version_str)
if m:
g = m.groups()
if g[2] is None:
return (int(g[0]), int(g[1]), 0, 'final', 0)
if g[3] is None:
return (int(g[0]), int(g[1]), int(g[2]), 'final', 0)
return (int(g[0]), int(g[1]), int(g[2]), g[3], int(g[4]))
raise VersionError("String '%s' do not match regex '%s'" % (version_str,
_version_re.pattern))

View File

@@ -1,11 +1,21 @@
# -*- coding: utf-8 -*-
import sys
import unittest
from picard import (version_to_string,
version_from_string,
VersionError)
# assertLess is available since 2.7 only
if sys.version_info[:2] == (2, 6):
def assertLess(self, a, b, msg=None):
if not a < b:
self.fail('%s not less than %s' % (repr(a), repr(b)))
unittest.TestCase.assertLess = assertLess
class VersionsTest(unittest.TestCase):
def test_version_conv_1(self):
@@ -24,9 +34,9 @@ class VersionsTest(unittest.TestCase):
self.assertEqual(l, version_from_string(s))
def test_version_conv_4(self):
l, s = (1, 0, 2, '', 0), '1.0.2'
self.assertRaises(VersionError, version_to_string, (l))
self.assertRaises(VersionError, version_from_string, (s))
l, s = (1, 0, 2, 'final', 0), '1.0.2'
self.assertEqual(version_to_string(l, short=True), s)
self.assertEqual(l, version_from_string(s))
def test_version_conv_5(self):
l, s = (999, 999, 999, 'dev', 999), '999.999.999dev999'
@@ -68,3 +78,35 @@ class VersionsTest(unittest.TestCase):
def test_version_conv_14(self):
l = 'anything_28x_1_0_dev_0'
self.assertRaises(VersionError, version_to_string, (l))
def test_version_conv_15(self):
l, s = (1, 1, 0, 'final', 0), 'anything_28_1_1_0'
self.assertEqual(l, version_from_string(s))
def test_version_conv_16(self):
self.assertRaises(VersionError, version_from_string, '1.1.0dev')
def test_version_conv_17(self):
self.assertRaises(VersionError, version_from_string, '1.1.0devx')
def test_version_conv_18(self):
l, s = (1, 1, 0, 'final', 0), '1.1'
self.assertEqual(version_to_string(l, short=True), s)
self.assertEqual(l, version_from_string(s))
def test_version_conv_19(self):
self.assertRaises(VersionError, version_from_string, '123')
def test_version_conv_20(self):
self.assertRaises(VersionError, version_from_string, '123.')
def test_api_vesions_1(self):
"Ensure api versions are ordered from oldest to newest"
from picard import api_versions
len_api_versions = len(api_versions)
if len_api_versions > 1:
for i in xrange(len_api_versions - 1):
a = version_from_string(api_versions[i])
b = version_from_string(api_versions[i+1])
self.assertLess(a, b)