Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterized next method wrapper #50

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ Contributors
The project has received contributions from (in alphabetical order):

* Raphaël Barrois <[email protected]> (https://github.com/rbarrois)
* Martin Ek <[email protected]> (https://github.com/ekmartin)
* Rick Eyre <[email protected]> (https://github.com/rickeyre)
* Hugo Rodger-Brown <hugo@yunojuno.com> (https://github.com/yunojuno)
* Dave Hall <skwadhd@gmail.com> (https://github.com/skwashd)
* Michael Hrivnak <[email protected]> (https://github.com/mhrivnak)
* William Minchin <[email protected]> (https://github.com/minchinweb)
* Dave Hall <skwadhd@gmail.com> (https://github.com/skwashd)
* Martin Ek <mail@ekmartin.com> (https://github.com/ekmartin)
* Hugo Rodger-Brown <hugo@yunojuno.com> (https://github.com/yunojuno)
* Cooper Stimson <cooper@cooperstimson.com> (https://github.com/6c1)


Contributor license agreement
Expand Down
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ You can also get a new version that represents a bump in one of the version leve
>>> str(new_v)
'1.1.2'

A parameterized convenience wrapper is available:

.. code-block:: pycon

>>> v = semantic_version.Version('1.1.1-pre+build')
>>> new_v = v.next_version('minor')
>>> str(new_v)
'1.2.0'

It is also possible to check whether a given string is a proper semantic version string:


Expand Down
12 changes: 12 additions & 0 deletions semantic_version/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from .compat import base_cmp


MAJOR, MINOR, PATCH = 'major', 'minor', 'patch'


def _to_int(value):
try:
return int(value), True
Expand Down Expand Up @@ -110,6 +113,15 @@ def next_patch(self):
return Version(
'.'.join(str(x) for x in [self.major, self.minor, self.patch + 1]))

def next_version(self, version_level):
"""Increment the given version level"""
if version_level not in (MAJOR, MINOR, PATCH):
raise ValueError(
'Version level must be one of {}, {} or {}'
.format(MAJOR, MINOR, PATCH)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the linter doesnt like this.

A trick I often use here is:

            raise ValueError(
                'Version level must be one of {}, {} or {}'
                ''.format(MAJOR, MINOR, PATCH)

)
return getattr(self, 'next_{}'.format(version_level))()

@classmethod
def coerce(cls, version_string, partial=False):
"""Coerce an arbitrary version string into a semver-compatible one.
Expand Down
44 changes: 44 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,50 @@ def test_bump_prerelease_versions(self):
self.assertEqual(v.prerelease, ())
self.assertEqual(v.build, ())

def test_bump_version(self):

v = base.Version('1.0.0+build')
self.assertEqual(v.next_major(), v.next_version(base.MAJOR))
v = base.Version('1.0.0+build')
self.assertEqual(v.next_minor(), v.next_version(base.MINOR))
v = base.Version('1.0.0+build')
self.assertEqual(v.next_patch(), v.next_version(base.PATCH))

v = base.Version('1.1.0+build')
self.assertEqual(v.next_major(), v.next_version(base.MAJOR))
v = base.Version('1.1.0+build')
self.assertEqual(v.next_minor(), v.next_version(base.MINOR))
v = base.Version('1.1.0+build')
self.assertEqual(v.next_patch(), v.next_version(base.PATCH))

v = base.Version('1.0.1+build')
self.assertEqual(v.next_major(), v.next_version(base.MAJOR))
v = base.Version('1.0.1+build')
self.assertEqual(v.next_minor(), v.next_version(base.MINOR))
v = base.Version('1.0.1+build')
self.assertEqual(v.next_patch(), v.next_version(base.PATCH))

v = base.Version('1.0.0-pre+build')
self.assertEqual(v.next_major(), v.next_version(base.MAJOR))
v = base.Version('1.0.0-pre+build')
self.assertEqual(v.next_minor(), v.next_version(base.MINOR))
v = base.Version('1.0.0-pre+build')
self.assertEqual(v.next_patch(), v.next_version(base.PATCH))

v = base.Version('1.1.0-pre+build')
self.assertEqual(v.next_major(), v.next_version(base.MAJOR))
v = base.Version('1.1.0-pre+build')
self.assertEqual(v.next_minor(), v.next_version(base.MINOR))
v = base.Version('1.1.0-pre+build')
self.assertEqual(v.next_patch(), v.next_version(base.PATCH))

v = base.Version('1.0.1-pre+build')
self.assertEqual(v.next_major(), v.next_version(base.MAJOR))
v = base.Version('1.0.1-pre+build')
self.assertEqual(v.next_minor(), v.next_version(base.MINOR))
v = base.Version('1.0.1-pre+build')
self.assertEqual(v.next_patch(), v.next_version(base.PATCH))


class SpecItemTestCase(unittest.TestCase):
invalids = [
Expand Down