Skip to content

Commit

Permalink
Merge pull request #54 from legendof-selda/feature/explicit_bump
Browse files Browse the repository at this point in the history
Feature/explicit bump
  • Loading branch information
mtkennerly committed Feb 20, 2023
2 parents 19e3c5c + 64f118f commit 9a5b0e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ def serialize_pvp(base: str, metadata: Optional[Sequence[Union[str, int]]] = Non
return serialized


def bump_version(base: str, index: int = -1) -> str:
def bump_version(base: str, index: int = -1, bump: int = 1) -> str:
"""
Increment one of the numerical positions of a version.
Expand All @@ -1953,10 +1953,12 @@ def bump_version(base: str, index: int = -1) -> str:
This follows Python indexing rules, so positive numbers start from
the left side and count up from 0, while negative numbers start from
the right side and count down from -1.
:param bump: By how much the `index` needs to increment. Default: 1.
:return: Bumped version.
"""
bump = int(bump) if isinstance(bump, str) else bump
bases = [int(x) for x in base.split(".")]
bases[index] += 1
bases[index] += bump

limit = 0 if index < 0 else len(bases)
i = index + 1
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ def test__serialize_pvp():


def test__bump_version():
# default bump=1
assert bump_version("1.2.3") == "1.2.4"

assert bump_version("1.2.3", 0) == "2.0.0"
Expand All @@ -833,6 +834,18 @@ def test__bump_version():
assert bump_version("1.2.3", -2) == "1.3.0"
assert bump_version("1.2.3", -3) == "2.0.0"

# expicit bump increment
assert bump_version("1.2.3", bump=3) == "1.2.6"

assert bump_version("1.2.3", 0, bump=3) == "4.0.0"
assert bump_version("1.2.3", 1, bump=3) == "1.5.0"
assert bump_version("1.2.3", 2, bump=3) == "1.2.6"

assert bump_version("1.2.3", -1, bump=3) == "1.2.6"
assert bump_version("1.2.3", -2, bump=3) == "1.5.0"
assert bump_version("1.2.3", -3, bump=3) == "4.0.0"

# check if incorrect index raises issues
with pytest.raises(IndexError):
bump_version("1.2.3", 3)

Expand Down

0 comments on commit 9a5b0e6

Please sign in to comment.