Skip to content

Commit ec3cd99

Browse files
committed
Fixed the indentation problem
- Added a dedent when a file does not match the change pattern. - Fixes #181
1 parent 406aeb0 commit ec3cd99

File tree

3 files changed

+134
-8
lines changed

3 files changed

+134
-8
lines changed

bumpversion/files.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,9 @@ def make_file_change(
183183
logger.dedent()
184184
return
185185
raise FileNotFoundError(f"File not found: '{self.file_change.filename}'") # pragma: no-coverage
186-
logger.debug("Serializing the current version")
187-
logger.indent()
188-
context["current_version"] = self.version_config.serialize(current_version, context)
189-
logger.dedent()
186+
context["current_version"] = self._get_serialized_version("current_version", current_version, context)
190187
if new_version:
191-
logger.debug("Serializing the new version")
192-
logger.indent()
193-
context["new_version"] = self.version_config.serialize(new_version, context)
194-
logger.dedent()
188+
context["new_version"] = self._get_serialized_version("new_version", new_version, context)
195189
else:
196190
logger.debug("No new version, using current version as new version")
197191
context["new_version"] = context["current_version"]
@@ -200,6 +194,7 @@ def make_file_change(
200194
replace_with = self.version_config.replace.format(**context)
201195

202196
if not self._contains_change_pattern(search_for, raw_search_pattern, current_version, context):
197+
logger.dedent()
203198
return
204199

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

215+
def _get_serialized_version(self, context_key: str, version: Version, context: MutableMapping) -> str:
216+
"""Get the serialized version."""
217+
logger.debug("Serializing the %s", context_key.replace("_", " "))
218+
logger.indent()
219+
serialized_version = self.version_config.serialize(version, context)
220+
logger.dedent()
221+
return serialized_version
222+
220223
def __str__(self) -> str: # pragma: no-coverage
221224
return self.file_change.filename
222225

tests/conftest.py

+3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
from click.testing import CliRunner
66
from pathlib import Path
77
from typing import Generator
8+
from bumpversion.ui import setup_logging
89

910
import pytest
1011

12+
setup_logging(1)
13+
1114

1215
@pytest.fixture
1316
def tests_path() -> Path:

tests/test_files.py

+120
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,126 @@
1919
from tests.conftest import get_config_data, inside_dir
2020

2121

22+
class TestConfiguredFile:
23+
"""Tests of the ConfiguredFile class."""
24+
25+
class TestGetFileContents:
26+
"""Tests of the get_file_content method."""
27+
28+
def test_returns_contents_of_existing_file(self, tmp_path: Path) -> None:
29+
"""The get_file_content method returns the contents of an existing file."""
30+
# Arrange
31+
filepath = tmp_path / "file.md"
32+
expected_data = "My current version is 1.2.3"
33+
filepath.write_text(expected_data, encoding="utf-8")
34+
overrides = {
35+
"current_version": "1.2.3",
36+
"files": [{"filename": str(filepath)}],
37+
}
38+
conf, version_config, current_version = get_config_data(overrides)
39+
assert len(conf.files_to_modify) == 1
40+
assert conf.files_to_modify[0].filename == str(filepath)
41+
42+
# Act
43+
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)
44+
45+
# Assert
46+
assert configured_file.get_file_contents() == expected_data
47+
48+
def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None:
49+
"""The get_file_content method raises an exception if the file does not exist."""
50+
filepath = tmp_path / "file.md"
51+
overrides = {
52+
"current_version": "1.2.3",
53+
"files": [{"filename": str(filepath)}],
54+
}
55+
conf, version_config, current_version = get_config_data(overrides)
56+
assert len(conf.files_to_modify) == 1
57+
assert conf.files_to_modify[0].filename == str(filepath)
58+
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)
59+
60+
# Act
61+
with pytest.raises(FileNotFoundError):
62+
configured_file.get_file_contents()
63+
64+
class TestMakeFileChange:
65+
"""Tests of the make_file_change function."""
66+
67+
def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None:
68+
"""The make_file_change method raises an exception if the file does not exist."""
69+
# Assemble
70+
filepath = tmp_path / "file.md"
71+
overrides = {
72+
"current_version": "1.2.3",
73+
"files": [{"filename": str(filepath)}],
74+
}
75+
conf, version_config, current_version = get_config_data(overrides)
76+
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)
77+
new_version = current_version.bump("patch")
78+
ctx = get_context(conf)
79+
80+
# Act
81+
with pytest.raises(FileNotFoundError):
82+
configured_file.make_file_change(current_version, new_version, ctx, dry_run=True)
83+
84+
def test_logs_missing_file_when_ignore_missing_file_set(self, tmp_path: Path, caplog) -> None:
85+
"""The make_file_change method logs missing file when ignore_missing_file set to True."""
86+
# Assemble
87+
filepath = tmp_path / "file.md"
88+
overrides = {
89+
"current_version": "1.2.3",
90+
"files": [{"filename": str(filepath), "ignore_missing_file": True}],
91+
}
92+
conf, version_config, current_version = get_config_data(overrides)
93+
configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config)
94+
new_version = current_version.bump("patch")
95+
ctx = get_context(conf)
96+
97+
# Act
98+
configured_file.make_file_change(current_version, new_version, ctx, dry_run=True)
99+
100+
# Assert
101+
logs = caplog.messages
102+
assert logs[0] == " Reading configuration"
103+
assert logs[1] == " Configuration file not found: missing."
104+
assert logs[2].endswith("file.md: replace `{current_version}` with `{new_version}`")
105+
assert logs[3] == " File not found, but ignoring"
106+
107+
def test_dedents_properly_when_file_does_not_contain_pattern(self, fixtures_path: Path, caplog) -> None:
108+
"""The make_file_change method dedents the logging context when it does not contain a pattern."""
109+
# Assemble
110+
globs = fixtures_path / "glob" / "**/*.txt"
111+
overrides = {
112+
"current_version": "1.2.3",
113+
"files": [{"glob": str(globs), "ignore_missing_file": True, "ignore_missing_version": True}],
114+
}
115+
conf, version_config, current_version = get_config_data(overrides)
116+
configured_file1 = files.ConfiguredFile(conf.files_to_modify[0], version_config)
117+
configured_file2 = files.ConfiguredFile(conf.files_to_modify[1], version_config)
118+
configured_file3 = files.ConfiguredFile(conf.files_to_modify[2], version_config)
119+
new_version = current_version.bump("patch")
120+
ctx = get_context(conf)
121+
122+
# Act
123+
configured_file1.make_file_change(current_version, new_version, ctx, dry_run=True)
124+
configured_file2.make_file_change(current_version, new_version, ctx, dry_run=True)
125+
configured_file3.make_file_change(current_version, new_version, ctx, dry_run=True)
126+
127+
# Assert
128+
logs = caplog.messages
129+
assert logs[0] == " Reading configuration"
130+
assert logs[1] == " Configuration file not found: missing."
131+
assert logs[2] == (
132+
f" \n File {configured_file1.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
133+
)
134+
assert logs[3] == (
135+
f" \n File {configured_file2.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
136+
)
137+
assert logs[4] == (
138+
f" \n File {configured_file3.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
139+
)
140+
141+
22142
def test_single_file_processed_twice(tmp_path: Path):
23143
"""
24144
Verify that a single file "file2" can be processed twice.

0 commit comments

Comments
 (0)