From 42316b382bf56d8fb6f7e1ec8364279a2aebb9e9 Mon Sep 17 00:00:00 2001 From: Anderson Brandao Date: Tue, 8 Sep 2020 19:51:35 -0300 Subject: [PATCH 1/2] Test non default search of omitted optional value --- bumpversion/utils.py | 16 +++++----------- tests/test_cli.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/bumpversion/utils.py b/bumpversion/utils.py index f872f230..5a2b5611 100644 --- a/bumpversion/utils.py +++ b/bumpversion/utils.py @@ -51,17 +51,11 @@ def should_contain_version(self, version, context): if self.contains(search_expression): return - # the `search` pattern did not match, but the original supplied - # version number (representing the same version part values) might - # match instead. - - # check whether `search` isn't customized, i.e. should match only - # very specific parts of the file - search_pattern_is_default = self._versionconfig.search == "{current_version}" - - if search_pattern_is_default and self.contains(version.original): - # original version is present and we're not looking for something - # more specific -> this is accepted as a match + # try original version to construct search_expression + search_expression = self._versionconfig.search.format( + current_version=version.original + ) + if self.contains(search_expression): return # version not found diff --git a/tests/test_cli.py b/tests/test_cli.py index d41942ba..5331151c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1506,6 +1506,35 @@ def test_no_list_no_stdout(tmpdir, vcs): assert out == "" +def test_non_default_search_of_omitted_optional_value(tmpdir): + tmpdir.join("version.txt").write("v0.0") + tmpdir.chdir() + + tmpdir.join(".bumpversion.cfg").write(dedent(r""" + [bumpversion] + current_version = 0.0 + parse = (?P\d+)\.(?P\d+)(\-?(?P[a-z]+))? + serialize = + {major}.{minor}-{release} + {major}.{minor} + + [bumpversion:part:release] + optional_value = prod + values = + dev + prod + [bumpversion:file:version.txt] + search = v{current_version} + replace = {new_version} + """).strip()) + try: + main(['minor']) + except exceptions.VersionNotFoundException as e: + pytest.fail( + str(e) + "\n\t- '0.0' is equivalent to '0.0-prod', so 'v0.0' should be found" + ) + + def test_bump_non_numeric_parts(tmpdir): tmpdir.join("with_pre_releases.txt").write("1.5.dev") tmpdir.chdir() From 1b6c60154efbf2a41f1aabe1bc460c94055f9c2a Mon Sep 17 00:00:00 2001 From: Anderson Brandao Date: Wed, 9 Sep 2020 11:56:16 -0300 Subject: [PATCH 2/2] Test non default replace of omitted optional value --- bumpversion/utils.py | 5 ++++- tests/test_cli.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bumpversion/utils.py b/bumpversion/utils.py index 5a2b5611..787beffb 100644 --- a/bumpversion/utils.py +++ b/bumpversion/utils.py @@ -112,8 +112,11 @@ def replace(self, current_version, new_version, context, dry_run): if file_content_before == file_content_after: # TODO expose this to be configurable + search_for_original_formatted = self._versionconfig.search.format( + current_version=current_version.original + ) file_content_after = file_content_before.replace( - current_version.original, replace_with + search_for_original_formatted, replace_with ) if file_content_before != file_content_after: diff --git a/tests/test_cli.py b/tests/test_cli.py index 5331151c..a22f30c7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1535,6 +1535,31 @@ def test_non_default_search_of_omitted_optional_value(tmpdir): ) +def test_non_default_replace_of_omitted_optional_value(tmpdir): + tmpdir.join("version.txt").write("v0.0") + tmpdir.chdir() + + tmpdir.join(".bumpversion.cfg").write(dedent(r""" + [bumpversion] + current_version = 0.0 + parse = (?P\d+)\.(?P\d+)(\-?(?P[a-z]+))? + serialize = + {major}.{minor}-{release} + {major}.{minor} + + [bumpversion:part:release] + optional_value = prod + values = + dev + prod + [bumpversion:file:version.txt] + search = v{current_version} + replace = v{new_version} + """).strip()) + main(['minor']) + assert 'v0.1-dev' == tmpdir.join("version.txt").read() + + def test_bump_non_numeric_parts(tmpdir): tmpdir.join("with_pre_releases.txt").write("1.5.dev") tmpdir.chdir()