Skip to content

Commit

Permalink
Merge pull request #106 from callowayproject/70-version-less-search-r…
Browse files Browse the repository at this point in the history
…eplace-raise-keyerror-new_version-exception

Fixes Key Error exception when searching
  • Loading branch information
coordt authored Dec 18, 2023
2 parents 7e4b73c + 6e1d63f commit dcd98f7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def make_file_change(
logger.indent()
context["new_version"] = self.version_config.serialize(new_version, context)
logger.dedent()
else:
logger.debug("No new version, using current version as new version")
context["new_version"] = context["current_version"]

search_for, raw_search_pattern = self.file_change.get_search_pattern(context)
replace_with = self.version_config.replace.format(**context)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,40 @@ def test_replace_search_with_plain_string(tmp_path, fixtures_path):
assert result.exit_code == 0


def test_replace_with_empty_string(tmp_path, fixtures_path):
"""Replace should not worry if the replace value has version info."""
from tomlkit import dumps

# Arrange
config_path = tmp_path / "pyproject.toml"
config_path.write_text(dumps(TEST_REPLACE_CONFIG))
doc_path = tmp_path / "docs.yaml"
doc_path.write_text("We should censor profanity\n\n")

runner: CliRunner = CliRunner()
with inside_dir(tmp_path):
result: Result = runner.invoke(
cli.cli,
[
"replace",
"--no-configured-files",
"--allow-dirty",
"--search",
"profanity",
"--replace",
"",
"./docs.yaml",
],
)

if result.exit_code != 0:
print("Here is the output:")
print(result.output)
print(traceback.print_exception(result.exc_info[1]))

assert result.exit_code == 0


def test_valid_regex_not_ignoring_regex(tmp_path: Path, caplog) -> None:
"""A search string not meant to be a regex (but is) is still found and replaced correctly."""
# Arrange
Expand Down
58 changes: 58 additions & 0 deletions tests/test_configuredfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Tests for the ConfiguredFile class."""
from bumpversion.files import ConfiguredFile, FileChange
from bumpversion.version_part import VersionConfig


class TestConfiguredFile:
"""Tests for the ConfiguredFile class."""

class TestClassCreation:
"""Tests for the creation of the ConfiguredFile class."""

def test_file_change_is_identical_to_input(self):
"""Test that the file change is identical to the input, but not the same object."""
change = FileChange(
filename="boobar.txt",
search="dashes: {current_version}",
replace="dashes: {new_version}",
parse=r"(?P<major>\d+)-(?P<minor>\d+)-(?P<patch>\d+)",
serialize=("{major}-{minor}-{patch}",),
regex=True,
ignore_missing_version=False,
)
version_config = VersionConfig(
parse="(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?",
serialize=("{major}.{minor}.{patch}",),
search="{current_version}",
replace="{new_version}",
)
cfg_file = ConfiguredFile(change, version_config)
assert cfg_file.file_change == change
assert cfg_file.file_change is not change

def test_version_config_uses_file_change_attrs(self):
"""Test that the version config uses the file change attributes, and the original part configs."""
change = FileChange(
filename="boobar.txt",
search="dashes: {current_version}",
replace="dashes: {new_version}",
parse=r"(?P<major>\d+)-(?P<minor>\d+)-(?P<patch>\d+)",
serialize=("{major}-{minor}-{patch}",),
regex=True,
ignore_missing_version=False,
)
version_config = VersionConfig(
parse="(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?",
serialize=("{major}.{minor}.{patch}",),
search="{current_version}",
replace="{new_version}",
)
expected = VersionConfig(
parse=change.parse,
search=change.search,
replace=change.replace,
serialize=change.serialize,
part_configs=version_config.part_configs,
)
cfg_file = ConfiguredFile(change, version_config)
assert cfg_file.version_config == expected

0 comments on commit dcd98f7

Please sign in to comment.