From 6e1d63f52ecedf9eeb442553bae9ecc7c70be8f1 Mon Sep 17 00:00:00 2001 From: Corey Oordt Date: Mon, 18 Dec 2023 08:58:35 -0600 Subject: [PATCH] Fixes Key Error exception when searching - Added a test for the reported bug - Fixes #70 --- bumpversion/files.py | 3 ++ tests/test_cli.py | 34 +++++++++++++++++++++ tests/test_configuredfile.py | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 tests/test_configuredfile.py diff --git a/bumpversion/files.py b/bumpversion/files.py index a5f8c1e1..4bca5e87 100644 --- a/bumpversion/files.py +++ b/bumpversion/files.py @@ -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) diff --git a/tests/test_cli.py b/tests/test_cli.py index 638d7df4..20532f76 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 diff --git a/tests/test_configuredfile.py b/tests/test_configuredfile.py new file mode 100644 index 00000000..f1043572 --- /dev/null +++ b/tests/test_configuredfile.py @@ -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\d+)-(?P\d+)-(?P\d+)", + serialize=("{major}-{minor}-{patch}",), + regex=True, + ignore_missing_version=False, + ) + version_config = VersionConfig( + parse="(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)(\\-(?P[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\d+)-(?P\d+)-(?P\d+)", + serialize=("{major}-{minor}-{patch}",), + regex=True, + ignore_missing_version=False, + ) + version_config = VersionConfig( + parse="(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)(\\-(?P[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