Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non default search and replace of omitted optional value #160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions bumpversion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<major>\d+)\.(?P<minor>\d+)(\-?(?P<release>[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<major>\d+)\.(?P<minor>\d+)(\-?(?P<release>[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()
Expand Down