diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f40e9a..46f533e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/dunamai/__init__.py b/dunamai/__init__.py index 1408488..b2e5463 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -1,6 +1,5 @@ __all__ = [ "bump_version", - "bump_complete_version", "check_version", "get_version", "serialize_pep440", @@ -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): @@ -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""" @@ -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) diff --git a/tests/unit/test_dunamai.py b/tests/unit/test_dunamai.py index 3ba6b24..2da154f 100644 --- a/tests/unit/test_dunamai.py +++ b/tests/unit/test_dunamai.py @@ -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)