From f3c328a6b11c5be2e285453714d4e7a3148b1078 Mon Sep 17 00:00:00 2001 From: Corey Oordt Date: Wed, 17 Jul 2024 09:15:35 -0500 Subject: [PATCH] Fix KeyError in TOML file handling The code has been updated to handle KeyErrors when updating TOML files. If a KeyError is raised, it's now caught and managed depending on the file_change attributes 'ignore_missing_file' or 'ignore_missing_version'. This aims to provide more robust handling of edge cases in TOML files. In addition, a new test case has been added to ensure current version is not required in the configuration. Fixes #212 --- bumpversion/files.py | 12 ++++++++---- tests/test_cli/test_bump.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/bumpversion/files.py b/bumpversion/files.py index 92c44e40..2c1c3a7b 100644 --- a/bumpversion/files.py +++ b/bumpversion/files.py @@ -337,7 +337,13 @@ def update_file( search_for, raw_search_pattern = self.file_change.get_search_pattern(new_context) replace_with = self.file_change.replace.format(**new_context) if self.path.suffix == ".toml": - self._update_toml_file(search_for, raw_search_pattern, replace_with, dry_run) + try: + self._update_toml_file(search_for, raw_search_pattern, replace_with, dry_run) + except KeyError as e: + if self.file_change.ignore_missing_file or self.file_change.ignore_missing_version: + pass + else: + raise e def _update_toml_file( self, search_for: re.Pattern, raw_search_pattern: str, replace_with: str, dry_run: bool = False @@ -348,9 +354,7 @@ def _update_toml_file( toml_data = tomlkit.parse(self.path.read_text(encoding="utf-8")) value_before = get_nested_value(toml_data, self.file_change.key_path) - if value_before is None: - raise KeyError(f"Key path '{self.file_change.key_path}' does not exist in {self.path}") - elif not contains_pattern(search_for, value_before) and not self.file_change.ignore_missing_version: + if not contains_pattern(search_for, value_before) and not self.file_change.ignore_missing_version: raise ValueError( f"Key '{self.file_change.key_path}' in {self.path} does not contain the correct contents: " f"{raw_search_pattern}" diff --git a/tests/test_cli/test_bump.py b/tests/test_cli/test_bump.py index 9406a39e..9d97fb0b 100644 --- a/tests/test_cli/test_bump.py +++ b/tests/test_cli/test_bump.py @@ -312,3 +312,37 @@ def test_dash_in_tag_matches_current_version(git_repo, runner, caplog): print(traceback.print_exception(result.exc_info[1])) assert result.exit_code == 0 + + +def test_current_version_not_required_in_config(tmp_path, fixtures_path, runner): + """The current version is not required in the configuration.""" + + config_file = tmp_path / ".bumpversion.toml" + config_contents = "[tool.bumpversion]\nallow_dirty = true\n\n" + config_file.write_text( + config_contents, + encoding="utf-8", + ) + + # Act + with inside_dir(tmp_path): + result: Result = runner.invoke( + cli.cli, + [ + "bump", + "-vv", + "--current-version", + "1.0.0", + "--dry-run", + "minor", + ], + ) + + # Assert + 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 + assert config_file.read_text() == config_contents