summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-21 22:31:49 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-21 22:31:49 +0100
commita9dae494c1de1271bf75b8aacb2a52b4bcaa5a67 (patch)
tree554628954c9969eb32077373c1ab61b278c48399
parent712d74f87c07c60f2e1d27b44915d5d4bb941fe7 (diff)
downloadsemantic-version-a9dae494c1de1271bf75b8aacb2a52b4bcaa5a67.tar.gz
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>
-rw-r--r--doc/changelog.rst2
-rw-r--r--doc/reference.rst15
-rw-r--r--src/semantic_version/base.py9
-rwxr-xr-xtests/test_base.py40
4 files changed, 66 insertions, 0 deletions
diff --git a/doc/changelog.rst b/doc/changelog.rst
index d5fa820..40cbc06 100644
--- a/doc/changelog.rst
+++ b/doc/changelog.rst
@@ -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)
------------------
diff --git a/doc/reference.rst b/doc/reference.rst
index 66a5f7d..5b8ca3e 100644
--- a/doc/reference.rst
+++ b/doc/reference.rst
@@ -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)
------------------------------------------
diff --git a/src/semantic_version/base.py b/src/semantic_version/base.py
index b52c671..88422f6 100644
--- a/src/semantic_version/base.py
+++ b/src/semantic_version/base.py
@@ -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
diff --git a/tests/test_base.py b/tests/test_base.py
index 3e10a83..1feb14f 100755
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -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 = {