From a9dae494c1de1271bf75b8aacb2a52b4bcaa5a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 21 Mar 2013 22:31:49 +0100 Subject: Add semantic_version.validate (Closes #2). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simple way of testing whether a string is a 'valid' SemVer version. Signed-off-by: Raphaƫl Barrois --- doc/changelog.rst | 2 ++ doc/reference.rst | 15 +++++++++++++++ src/semantic_version/base.py | 9 +++++++++ tests/test_base.py | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) 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 ` 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 = { -- cgit v1.2.1