Add semantic_version.validate (Closes #2).
Simple way of testing whether a string is a 'valid' SemVer version. Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
This commit is contained in:
@@ -14,6 +14,8 @@ ChangeLog
|
||||
* Add the :meth:`Version.coerce <semantic_version.Version.coerce>` class method to
|
||||
:class:`~semantic_version.Version` class for mapping arbitrary version strings to
|
||||
semver.
|
||||
* Add the :func:`~semantic_version.validate` method to validate a version
|
||||
string against the SemVer rules.
|
||||
|
||||
2.1.2 (22/05/2012)
|
||||
------------------
|
||||
|
||||
@@ -43,6 +43,21 @@ Module-level functions
|
||||
:rtype: ``bool``
|
||||
|
||||
|
||||
.. function:: validate(version)
|
||||
|
||||
Check whether a version string complies with the `SemVer`_ rules.
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> semantic_version.validate('1.1.1')
|
||||
True
|
||||
>>> semantic_version.validate('1.2.3a4')
|
||||
False
|
||||
|
||||
:param str version: The version string to validate
|
||||
:rtype: ``bool``
|
||||
|
||||
|
||||
Representing a version (the Version class)
|
||||
------------------------------------------
|
||||
|
||||
|
||||
@@ -438,3 +438,12 @@ def compare(v1, v2):
|
||||
|
||||
def match(spec, version):
|
||||
return Spec(spec).match(Version(version))
|
||||
|
||||
|
||||
def validate(version_string):
|
||||
"""Validates a version string againt the SemVer specification."""
|
||||
try:
|
||||
Version.parse(version_string)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
@@ -88,6 +88,46 @@ class TopLevelTestCase(unittest.TestCase):
|
||||
self.assertTrue(base.match(spec, version),
|
||||
"%r should accept %r" % (spec, version))
|
||||
|
||||
valid_strings = (
|
||||
'1.0.0-alpha',
|
||||
'1.0.0-alpha.1',
|
||||
'1.0.0-beta.2',
|
||||
'1.0.0-beta.11',
|
||||
'1.0.0-rc.1',
|
||||
'1.0.0-rc.1+build.1',
|
||||
'1.0.0',
|
||||
'1.0.0+0.3.7',
|
||||
'1.3.7+build',
|
||||
'1.3.7+build.2.b8f12d7',
|
||||
'1.3.7+build.11.e0f985a',
|
||||
'1.1.1',
|
||||
'1.1.2',
|
||||
'1.1.3-rc4.5',
|
||||
'1.1.3-rc42.3-14-15.24+build.2012-04-13.223',
|
||||
'1.1.3+build.2012-04-13.HUY.alpha-12.1',
|
||||
)
|
||||
|
||||
def test_validate_valid(self):
|
||||
for version in self.valid_strings:
|
||||
self.assertTrue(base.validate(version),
|
||||
"%r should be a valid version" % (version,))
|
||||
|
||||
invalid_strings = (
|
||||
'1',
|
||||
'v1',
|
||||
'1.2.3.4',
|
||||
'1.2',
|
||||
'1.2a3',
|
||||
'1.2.3a4',
|
||||
'v12.34.5',
|
||||
'1.2.3+4+5',
|
||||
)
|
||||
|
||||
def test_validate_invalid(self):
|
||||
for version in self.invalid_strings:
|
||||
self.assertFalse(base.validate(version),
|
||||
"%r should not be a valid version" % (version,))
|
||||
|
||||
|
||||
class VersionTestCase(unittest.TestCase):
|
||||
versions = {
|
||||
|
||||
Reference in New Issue
Block a user