Skip to content

Commit

Permalink
Fixed the indentation problem
Browse files Browse the repository at this point in the history
- Added a dedent when a file does not match the change pattern.
- Fixes #181
  • Loading branch information
coordt committed Apr 25, 2024
1 parent 406aeb0 commit ec3cd99
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
19 changes: 11 additions & 8 deletions bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,9 @@ def make_file_change(
logger.dedent()
return
raise FileNotFoundError(f"File not found: '{self.file_change.filename}'") # pragma: no-coverage
logger.debug("Serializing the current version")
logger.indent()
context["current_version"] = self.version_config.serialize(current_version, context)
logger.dedent()
context["current_version"] = self._get_serialized_version("current_version", current_version, context)
if new_version:
logger.debug("Serializing the new version")
logger.indent()
context["new_version"] = self.version_config.serialize(new_version, context)
logger.dedent()
context["new_version"] = self._get_serialized_version("new_version", new_version, context)
else:
logger.debug("No new version, using current version as new version")
context["new_version"] = context["current_version"]
Expand All @@ -200,6 +194,7 @@ def make_file_change(
replace_with = self.version_config.replace.format(**context)

if not self._contains_change_pattern(search_for, raw_search_pattern, current_version, context):
logger.dedent()
return

file_content_before = self.get_file_contents()
Expand All @@ -217,6 +212,14 @@ def make_file_change(
if not dry_run: # pragma: no-coverage
self.write_file_contents(file_content_after)

def _get_serialized_version(self, context_key: str, version: Version, context: MutableMapping) -> str:
"""Get the serialized version."""
logger.debug("Serializing the %s", context_key.replace("_", " "))
logger.indent()
serialized_version = self.version_config.serialize(version, context)
logger.dedent()
return serialized_version

def __str__(self) -> str: # pragma: no-coverage
return self.file_change.filename

Expand Down
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from click.testing import CliRunner
from pathlib import Path
from typing import Generator
from bumpversion.ui import setup_logging

import pytest

setup_logging(1)


@pytest.fixture
def tests_path() -> Path:
Expand Down
120 changes: 120 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,126 @@
from tests.conftest import get_config_data, inside_dir


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

class TestGetFileContents:
"""Tests of the get_file_content method."""

def test_returns_contents_of_existing_file(self, tmp_path: Path) -> None:
"""The get_file_content method returns the contents of an existing file."""
# Arrange
filepath = tmp_path / "file.md"
expected_data = "My current version is 1.2.3"
filepath.write_text(expected_data, encoding="utf-8")
overrides = {
"current_version": "1.2.3",
"files": [{"filename": str(filepath)}],
}
conf, version_config, current_version = get_config_data(overrides)
assert len(conf.files_to_modify) == 1
assert conf.files_to_modify[0].filename == str(filepath)

# Act
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)

# Assert
assert configured_file.get_file_contents() == expected_data

def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None:
"""The get_file_content method raises an exception if the file does not exist."""
filepath = tmp_path / "file.md"
overrides = {
"current_version": "1.2.3",
"files": [{"filename": str(filepath)}],
}
conf, version_config, current_version = get_config_data(overrides)
assert len(conf.files_to_modify) == 1
assert conf.files_to_modify[0].filename == str(filepath)
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)

# Act
with pytest.raises(FileNotFoundError):
configured_file.get_file_contents()

class TestMakeFileChange:
"""Tests of the make_file_change function."""

def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None:
"""The make_file_change method raises an exception if the file does not exist."""
# Assemble
filepath = tmp_path / "file.md"
overrides = {
"current_version": "1.2.3",
"files": [{"filename": str(filepath)}],
}
conf, version_config, current_version = get_config_data(overrides)
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)
new_version = current_version.bump("patch")
ctx = get_context(conf)

# Act
with pytest.raises(FileNotFoundError):
configured_file.make_file_change(current_version, new_version, ctx, dry_run=True)

def test_logs_missing_file_when_ignore_missing_file_set(self, tmp_path: Path, caplog) -> None:
"""The make_file_change method logs missing file when ignore_missing_file set to True."""
# Assemble
filepath = tmp_path / "file.md"
overrides = {
"current_version": "1.2.3",
"files": [{"filename": str(filepath), "ignore_missing_file": True}],
}
conf, version_config, current_version = get_config_data(overrides)
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)
new_version = current_version.bump("patch")
ctx = get_context(conf)

# Act
configured_file.make_file_change(current_version, new_version, ctx, dry_run=True)

# Assert
logs = caplog.messages
assert logs[0] == " Reading configuration"
assert logs[1] == " Configuration file not found: missing."
assert logs[2].endswith("file.md: replace `{current_version}` with `{new_version}`")
assert logs[3] == " File not found, but ignoring"

def test_dedents_properly_when_file_does_not_contain_pattern(self, fixtures_path: Path, caplog) -> None:
"""The make_file_change method dedents the logging context when it does not contain a pattern."""
# Assemble
globs = fixtures_path / "glob" / "**/*.txt"
overrides = {
"current_version": "1.2.3",
"files": [{"glob": str(globs), "ignore_missing_file": True, "ignore_missing_version": True}],
}
conf, version_config, current_version = get_config_data(overrides)
configured_file1 = files.ConfiguredFile(conf.files_to_modify[0], version_config)
configured_file2 = files.ConfiguredFile(conf.files_to_modify[1], version_config)
configured_file3 = files.ConfiguredFile(conf.files_to_modify[2], version_config)
new_version = current_version.bump("patch")
ctx = get_context(conf)

# Act
configured_file1.make_file_change(current_version, new_version, ctx, dry_run=True)
configured_file2.make_file_change(current_version, new_version, ctx, dry_run=True)
configured_file3.make_file_change(current_version, new_version, ctx, dry_run=True)

# Assert
logs = caplog.messages
assert logs[0] == " Reading configuration"
assert logs[1] == " Configuration file not found: missing."
assert logs[2] == (
f" \n File {configured_file1.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)
assert logs[3] == (
f" \n File {configured_file2.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)
assert logs[4] == (
f" \n File {configured_file3.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)


def test_single_file_processed_twice(tmp_path: Path):
"""
Verify that a single file "file2" can be processed twice.
Expand Down

0 comments on commit ec3cd99

Please sign in to comment.