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

Feature/explicit bump #54

Merged
merged 4 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,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 @@ -1942,10 +1942,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 @@ -816,6 +816,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 @@ -826,6 +827,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