diff --git a/bumpversion/utils.py b/bumpversion/utils.py index f872f230..787beffb 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 @@ -118,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 d41942ba..a22f30c7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1506,6 +1506,60 @@ 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_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()