Skip to content

Commit

Permalink
Merge pull request #183 from callowayproject/181-glob-pattern-indente…
Browse files Browse the repository at this point in the history
…d-output-bug

Fixed the indentation problem
  • Loading branch information
coordt authored Apr 26, 2024
2 parents db69f9a + 3777f27 commit b76114e
Show file tree
Hide file tree
Showing 2 changed files with 141 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
130 changes: 130 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""File processing tests."""

import logging
import os
import shutil
from datetime import datetime, timezone
Expand All @@ -15,10 +16,139 @@
from bumpversion.config.models import FileChange
from bumpversion.context import get_context
from bumpversion.exceptions import VersionNotFoundError
from bumpversion.ui import setup_logging, get_indented_logger
from bumpversion.version_part import VersionConfig
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
setup_logging(1)
caplog.set_level(logging.INFO)
logger = get_indented_logger(__name__)
logger.reset()
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] == f"\nFile {filepath}: 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
setup_logging(1)
caplog.set_level(logging.INFO)
logger = get_indented_logger(__name__)
logger.reset()
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"\nFile {configured_file1.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)
assert logs[3] == (
f"\nFile {configured_file2.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)
assert logs[4] == (
f"\nFile {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 b76114e

Please sign in to comment.