Skip to content

Commit

Permalink
#38: Convert standalone bump_complete_version() into method Version.b…
Browse files Browse the repository at this point in the history
…ump()
  • Loading branch information
mtkennerly committed Feb 17, 2022
1 parent d706669 commit 9094b1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
([Contributed by marnikow](https://github.com/mtkennerly/dunamai/pull/40))
* Added `ignore` option to `get_version()`.
([Contributed by marnikow](https://github.com/mtkennerly/dunamai/pull/39))
* Added `Version.parse()`.
([Contributed by marnikow](https://github.com/mtkennerly/dunamai/pull/41))
* Added `Version.bump()`.
([Contributed by marnikow](https://github.com/mtkennerly/dunamai/pull/38))

## v1.8.0 (2022-01-27)

Expand Down
64 changes: 27 additions & 37 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
__all__ = [
"bump_version",
"bump_complete_version",
"check_version",
"get_version",
"serialize_pep440",
Expand Down Expand Up @@ -466,13 +465,9 @@ def serialize(
base = self.base
revision = self.revision
if bump:
if self.stage is None:
base = bump_version(self.base)
else:
if self.revision is None:
revision = 2
else:
revision = self.revision + 1
bumped = self.bump()
base = bumped.base
revision = bumped.revision

if format is not None:
if callable(format):
Expand Down Expand Up @@ -630,6 +625,30 @@ def parse(cls, version: Union[str, "Version"], pattern: str = _VERSION_PATTERN)
epoch=epoch,
)

def bump(self, index: int = -1) -> "Version":
"""
Increment the version.
The base is bumped unless there is a stage defined, in which case,
the revision is bumped instead.
:param index: Numerical position to increment in the base. Default: -1.
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.
Only has an effect when the base is bumped.
:return: Bumped version.
"""
bumped = copy.deepcopy(self)
if bumped.stage is None:
bumped.base = bump_version(bumped.base, index)
else:
if bumped.revision is None:
bumped.revision = 2
else:
bumped.revision = bumped.revision + 1
return bumped

@classmethod
def from_git(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
r"""
Expand Down Expand Up @@ -1273,35 +1292,6 @@ def bump_version(base: str, index: int = -1) -> str:
return ".".join(str(x) for x in bases)


def bump_complete_version(version: Version, index: int = -1) -> Version:
"""
Increment a version.
If there is no pre-release the core version is bumped. Otherwise, the revision number.
The index parameter is only used when the base version is bumped.
:param version: Version, such as 0.1.0a8+linux.
:param index: Numerical position to increment. Default: -1.
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.
Only has an effect when the core version (e.g. 0.1.0) is bumped.
:return: Bumped version.
"""
base = version.base
revision = version.revision
if version.stage is None:
base = bump_version(version.base, index)
else:
if version.revision is None:
revision = 2
else:
revision = version.revision + 1
new_version = copy.copy(version)
new_version.base = base
new_version.revision = revision
return new_version


def _parse_git_timestamp_iso_strict(raw: str) -> dt.datetime:
# Remove colon from timezone offset for pre-3.7 Python:
compat = re.sub(r"(.*T.*[-+]\d+):(\d+)", r"\1\2", raw)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,14 @@ def immutable(v: Version) -> str:
assert version.distance == 0


def test__version__bump() -> None:
assert Version("1.2.3").bump().serialize() == "1.2.4"
assert Version("1.2.3").bump(-2).serialize() == "1.3.0"
assert Version("1.2.3").bump(0).serialize() == "2.0.0"
assert Version("1.2.3", stage=("a", None)).bump().serialize() == "1.2.3a2"
assert Version("1.2.3", stage=("a", 4)).bump().serialize() == "1.2.3a5"


def test__get_version__from_name() -> None:
assert get_version("dunamai") == Version(pkg_resources.get_distribution("dunamai").version)

Expand Down

0 comments on commit 9094b1f

Please sign in to comment.