Skip to content

Commit

Permalink
#54: Rename new argument to 'increment' and add it to 'Version.bump' …
Browse files Browse the repository at this point in the history
…as well
  • Loading branch information
mtkennerly committed Feb 20, 2023
1 parent 9a5b0e6 commit 9e871df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Updated `Version.parse` to better handle PEP 440 versions produced by Dunamai itself.
Specifically, in `1.2.3.post4.dev5`, the post number becomes the distance and the dev number is ignored.
In `1.2.3.dev5`, the dev number becomes the distance.
* Added `increment` argument to `bump_version` and `Version.bump`.
([Contributed by legendof-selda](https://github.com/mtkennerly/dunamai/pull/54))

## v1.15.0 (2022-12-02)

Expand Down
14 changes: 7 additions & 7 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ def parse(cls, version: str, pattern: Union[str, Pattern] = Pattern.Default) ->
epoch=epoch,
)

def bump(self, index: int = -1) -> "Version":
def bump(self, index: int = -1, increment: int = 1) -> "Version":
"""
Increment the version.
Expand All @@ -818,16 +818,17 @@ def bump(self, index: int = -1) -> "Version":
the left side and count up from 0, while negative numbers start from
the right side and count down from -1.
Only has an effect when the base is bumped.
:param increment: By how much to increment the relevant position. Default: 1.
:return: Bumped version.
"""
bumped = copy.deepcopy(self)
if bumped.stage is None:
bumped.base = bump_version(bumped.base, index)
bumped.base = bump_version(bumped.base, index, increment)
else:
if bumped.revision is None:
bumped.revision = 2
else:
bumped.revision = bumped.revision + 1
bumped.revision += increment
return bumped

@classmethod
Expand Down Expand Up @@ -1943,7 +1944,7 @@ def serialize_pvp(base: str, metadata: Optional[Sequence[Union[str, int]]] = Non
return serialized


def bump_version(base: str, index: int = -1, bump: int = 1) -> str:
def bump_version(base: str, index: int = -1, increment: int = 1) -> str:
"""
Increment one of the numerical positions of a version.
Expand All @@ -1953,12 +1954,11 @@ def bump_version(base: str, index: int = -1, bump: 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.
:param increment: 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] += bump
bases[index] += increment

limit = 0 if index < 0 else len(bases)
i = index + 1
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,15 @@ def test__bump_version():
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", increment=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", 0, increment=3) == "4.0.0"
assert bump_version("1.2.3", 1, increment=3) == "1.5.0"
assert bump_version("1.2.3", 2, increment=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"
assert bump_version("1.2.3", -1, increment=3) == "1.2.6"
assert bump_version("1.2.3", -2, increment=3) == "1.5.0"
assert bump_version("1.2.3", -3, increment=3) == "4.0.0"

# check if incorrect index raises issues
with pytest.raises(IndexError):
Expand Down

0 comments on commit 9e871df

Please sign in to comment.