Skip to content

Commit f3c328a

Browse files
committed
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
1 parent 2803cc7 commit f3c328a

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

bumpversion/files.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,13 @@ def update_file(
337337
search_for, raw_search_pattern = self.file_change.get_search_pattern(new_context)
338338
replace_with = self.file_change.replace.format(**new_context)
339339
if self.path.suffix == ".toml":
340-
self._update_toml_file(search_for, raw_search_pattern, replace_with, dry_run)
340+
try:
341+
self._update_toml_file(search_for, raw_search_pattern, replace_with, dry_run)
342+
except KeyError as e:
343+
if self.file_change.ignore_missing_file or self.file_change.ignore_missing_version:
344+
pass
345+
else:
346+
raise e
341347

342348
def _update_toml_file(
343349
self, search_for: re.Pattern, raw_search_pattern: str, replace_with: str, dry_run: bool = False
@@ -348,9 +354,7 @@ def _update_toml_file(
348354
toml_data = tomlkit.parse(self.path.read_text(encoding="utf-8"))
349355
value_before = get_nested_value(toml_data, self.file_change.key_path)
350356

351-
if value_before is None:
352-
raise KeyError(f"Key path '{self.file_change.key_path}' does not exist in {self.path}")
353-
elif not contains_pattern(search_for, value_before) and not self.file_change.ignore_missing_version:
357+
if not contains_pattern(search_for, value_before) and not self.file_change.ignore_missing_version:
354358
raise ValueError(
355359
f"Key '{self.file_change.key_path}' in {self.path} does not contain the correct contents: "
356360
f"{raw_search_pattern}"

tests/test_cli/test_bump.py

+34
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,37 @@ def test_dash_in_tag_matches_current_version(git_repo, runner, caplog):
312312
print(traceback.print_exception(result.exc_info[1]))
313313

314314
assert result.exit_code == 0
315+
316+
317+
def test_current_version_not_required_in_config(tmp_path, fixtures_path, runner):
318+
"""The current version is not required in the configuration."""
319+
320+
config_file = tmp_path / ".bumpversion.toml"
321+
config_contents = "[tool.bumpversion]\nallow_dirty = true\n\n"
322+
config_file.write_text(
323+
config_contents,
324+
encoding="utf-8",
325+
)
326+
327+
# Act
328+
with inside_dir(tmp_path):
329+
result: Result = runner.invoke(
330+
cli.cli,
331+
[
332+
"bump",
333+
"-vv",
334+
"--current-version",
335+
"1.0.0",
336+
"--dry-run",
337+
"minor",
338+
],
339+
)
340+
341+
# Assert
342+
if result.exit_code != 0:
343+
print("Here is the output:")
344+
print(result.output)
345+
print(traceback.print_exception(result.exc_info[1]))
346+
347+
assert result.exit_code == 0
348+
assert config_file.read_text() == config_contents

0 commit comments

Comments
 (0)