Skip to content

Commit

Permalink
Merge pull request #213 from callowayproject/212-derive-current-versi…
Browse files Browse the repository at this point in the history
…on-from-git-tags

Fix KeyError in TOML file handling
  • Loading branch information
coordt authored Jul 17, 2024
2 parents 2803cc7 + f3c328a commit 609b92c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
12 changes: 8 additions & 4 deletions bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down
34 changes: 34 additions & 0 deletions tests/test_cli/test_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 609b92c

Please sign in to comment.