From 626cf0f6e69cf3d8dcf9d02cc4c5549f519e6347 Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Mon, 18 Jan 2021 10:22:18 +0100 Subject: [PATCH 1/5] Allow serialization without a specified context This makes the upcoming unit test a little shorter. --- bumpversion/version_part.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bumpversion/version_part.py b/bumpversion/version_part.py index ae34895c..522e4ca7 100644 --- a/bumpversion/version_part.py +++ b/bumpversion/version_part.py @@ -277,7 +277,8 @@ def _choose_serialize_format(self, version, context): return chosen - def serialize(self, version, context): + def serialize(self, version, context=None): + context = context or {} serialized = self._serialize( version, self._choose_serialize_format(version, context), context ) From 37857f5cc3e54ab88d51c9c285b6bc0820fdc697 Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Mon, 18 Jan 2021 09:48:48 +0100 Subject: [PATCH 2/5] Add unit test for bug 134 --- tests/test_version_part.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_version_part.py b/tests/test_version_part.py index afdd4d52..dbe289aa 100644 --- a/tests/test_version_part.py +++ b/tests/test_version_part.py @@ -4,6 +4,7 @@ ConfiguredVersionPartConfiguration, NumericVersionPartConfiguration, VersionPart, + VersionConfig, ) @@ -36,6 +37,29 @@ def test_version_part_bump(confvpc): assert vc.value == confvpc.bump(confvpc.first_value) +def test_bump_resets_lower_parts_to_default_value(): + # See bug 134. + release_part_config = ConfiguredVersionPartConfiguration( + first_value="dev", + optional_value="prod", + values=["dev", "prod"], + ) + version_config = VersionConfig( + parse=r"(?P\d+)(\-(?P[a-z]+)(?P\d+))?", + serialize=["{major}-{release}{build}", "{major}"], + part_configs={"release": release_part_config}, + search=None, + replace=None, + ) + + # Start with version "0", mapping to "0-prod0". + # Bump `build` from 0 to 1. + # Part `release` should remain `prod` and be serialized as such. + version = version_config.parse("0") + new_version = version.bump("build", version_config.order()) + assert version_config.serialize(new_version) == "0-prod1" + + def test_version_part_check_optional_false(confvpc): assert not VersionPart(confvpc.first_value, confvpc).bump().is_optional() From bbaeadd9b795593b0c22b3a0c260062569baf634 Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Mon, 18 Jan 2021 09:49:06 +0100 Subject: [PATCH 3/5] Fix 134: copy VersionPart config in copy() This way, the optional_value can be used when required. --- bumpversion/version_part.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bumpversion/version_part.py b/bumpversion/version_part.py index 522e4ca7..2b05ab1e 100644 --- a/bumpversion/version_part.py +++ b/bumpversion/version_part.py @@ -61,7 +61,7 @@ def value(self): return self._value or self.config.optional_value def copy(self): - return VersionPart(self._value) + return VersionPart(self._value, self.config) def bump(self): return VersionPart(self.config.bump(self.value), self.config) From c911dd05ff912a1d8d87e92a7c036674ae90db8a Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Mon, 18 Jan 2021 09:49:24 +0100 Subject: [PATCH 4/5] Cleanup: simplify code a bit --- bumpversion/version_part.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bumpversion/version_part.py b/bumpversion/version_part.py index 2b05ab1e..891f4f7f 100644 --- a/bumpversion/version_part.py +++ b/bumpversion/version_part.py @@ -148,14 +148,10 @@ def __init__(self, parse, serialize, search, replace, part_configs=None): self.serialize_formats = serialize - if not part_configs: - part_configs = {} - - self.part_configs = part_configs + self.part_configs = part_configs or {} self.search = search self.replace = replace - def order(self): # currently, order depends on the first given serialization format # this seems like a good idea because this should be the most complete format @@ -177,7 +173,6 @@ def parse(self, version_string): match = self.parse_regex.search(version_string) - _parsed = {} if not match: logger.warning( "Evaluating 'parse' option: '%s' does not parse current version '%s'", @@ -186,6 +181,7 @@ def parse(self, version_string): ) return None + _parsed = {} for key, value in match.groupdict().items(): _parsed[key] = VersionPart(value, self.part_configs.get(key)) From ca29428b0a06ee5bcdbf32909d3bc522270abea3 Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Mon, 18 Jan 2021 10:23:53 +0100 Subject: [PATCH 5/5] Docs: Clarify that first `serialize` should be complete If you reverse the order, funny behavior ensues. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ede93cb3..e63b3ca3 100644 --- a/README.md +++ b/README.md @@ -324,8 +324,8 @@ you may append a description between parens to the `file` keyword: `$`). Can be specified multiple times, bumpversion will try the serialization - formats beginning with the first and choose the last one where all values can - be represented like this: + formats beginning with the first (most complete) one and choose the last + one where all values can be represented like this: ```ini serialize =