Skip to content

Commit ff5aaaa

Browse files
committed
Extracted log_changes function from ConfiguredFile !minor
1 parent 0501570 commit ff5aaaa

File tree

3 files changed

+116
-17
lines changed

3 files changed

+116
-17
lines changed

bumpversion/files.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ def contains_pattern(search: re.Pattern, contents: str) -> bool:
6161
return False
6262

6363

64+
def log_changes(file_path: str, file_content_before: str, file_content_after: str, dry_run: bool = False) -> None:
65+
"""
66+
Log the changes that would be made to the file.
67+
68+
Args:
69+
file_path: The path to the file
70+
file_content_before: The file contents before the change
71+
file_content_after: The file contents after the change
72+
dry_run: True if this is a report-only job
73+
"""
74+
if file_content_before != file_content_after:
75+
logger.info("%s file %s:", "Would change" if dry_run else "Changing", file_path)
76+
logger.info(
77+
"\n".join(
78+
list(
79+
context_diff(
80+
file_content_before.splitlines(),
81+
file_content_after.splitlines(),
82+
fromfile=f"before {file_path}",
83+
tofile=f"after {file_path}",
84+
lineterm="",
85+
)
86+
)
87+
)
88+
)
89+
else:
90+
logger.info("%s file %s", "Would not change" if dry_run else "Not changing", file_path)
91+
92+
6493
class ConfiguredFile:
6594
"""A file to modify in a configured way."""
6695

@@ -156,23 +185,7 @@ def replace_version(
156185
search_for_og, og_raw_search_pattern = get_search_pattern(self.search, og_context, self.regex)
157186
file_content_after = search_for_og.sub(replace_with, file_content_before)
158187

159-
if file_content_before != file_content_after:
160-
logger.info("%s file %s:", "Would change" if dry_run else "Changing", self.path)
161-
logger.info(
162-
"\n".join(
163-
list(
164-
context_diff(
165-
file_content_before.splitlines(),
166-
file_content_after.splitlines(),
167-
fromfile=f"before {self.path}",
168-
tofile=f"after {self.path}",
169-
lineterm="",
170-
)
171-
)
172-
)
173-
)
174-
else:
175-
logger.info("%s file %s", "Would not change" if dry_run else "Not changing", self.path)
188+
log_changes(self.path, file_content_before, file_content_after, dry_run)
176189

177190
if not dry_run: # pragma: no-coverage
178191
self.write_file_contents(file_content_after)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[tool.bumpversion]
2+
current_version = "0.0.1"
3+
ignore_missing_version = true
4+
regex = true
5+
6+
[[tool.bumpversion.files]]
7+
filename = "should_contain_defaults.txt"
8+
9+
[[tool.bumpversion.files]]
10+
filename = "should_override_search.txt"
11+
search = "**unreleased**"
12+
13+
[[tool.bumpversion.files]]
14+
filename = "should_override_replace.txt"
15+
replace = "**unreleased**"
16+
17+
[[tool.bumpversion.files]]
18+
filename = "should_override_parse.txt"
19+
parse = "version(?P<major>\\d+)"
20+
21+
[[tool.bumpversion.files]]
22+
filename = "should_override_serialize.txt"
23+
serialize = ["{major}"]
24+
25+
[[tool.bumpversion.files]]
26+
filename = "should_override_ignore_missing.txt"
27+
ignore_missing_version = false
28+
29+
[[tool.bumpversion.files]]
30+
filename = "should_override_regex.txt"
31+
regex = false

tests/test_config.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,58 @@ def test_get_glob_files(glob_pattern: str, file_list: set, fixtures_path: Path):
274274
assert len(result) == len(file_list)
275275
for f in result:
276276
assert Path(f.filename) in file_list
277+
278+
279+
def test_file_overrides_config(fixtures_path: Path):
280+
"""If a file has a different config, it should override the main config."""
281+
cfg_file = fixtures_path / "file_config_overrides.toml"
282+
conf = config.get_configuration(cfg_file)
283+
file_map = {f.filename: f for f in conf.files}
284+
assert file_map["should_contain_defaults.txt"].parse == conf.parse
285+
assert file_map["should_contain_defaults.txt"].serialize == conf.serialize
286+
assert file_map["should_contain_defaults.txt"].search == conf.search
287+
assert file_map["should_contain_defaults.txt"].replace == conf.replace
288+
assert file_map["should_contain_defaults.txt"].regex == conf.regex
289+
assert file_map["should_contain_defaults.txt"].ignore_missing_version == conf.ignore_missing_version
290+
291+
assert file_map["should_override_search.txt"].parse == conf.parse
292+
assert file_map["should_override_search.txt"].serialize == conf.serialize
293+
assert file_map["should_override_search.txt"].search == "**unreleased**"
294+
assert file_map["should_override_search.txt"].replace == conf.replace
295+
assert file_map["should_override_search.txt"].regex == conf.regex
296+
assert file_map["should_override_search.txt"].ignore_missing_version == conf.ignore_missing_version
297+
298+
assert file_map["should_override_replace.txt"].parse == conf.parse
299+
assert file_map["should_override_replace.txt"].serialize == conf.serialize
300+
assert file_map["should_override_replace.txt"].search == conf.search
301+
assert file_map["should_override_replace.txt"].replace == "**unreleased**"
302+
assert file_map["should_override_replace.txt"].regex == conf.regex
303+
assert file_map["should_override_replace.txt"].ignore_missing_version == conf.ignore_missing_version
304+
305+
assert file_map["should_override_parse.txt"].parse == "version(?P<major>\d+)"
306+
assert file_map["should_override_parse.txt"].serialize == conf.serialize
307+
assert file_map["should_override_parse.txt"].search == conf.search
308+
assert file_map["should_override_parse.txt"].replace == conf.replace
309+
assert file_map["should_override_parse.txt"].regex == conf.regex
310+
assert file_map["should_override_parse.txt"].ignore_missing_version == conf.ignore_missing_version
311+
312+
assert file_map["should_override_serialize.txt"].parse == conf.parse
313+
assert file_map["should_override_serialize.txt"].serialize == ["{major}"]
314+
assert file_map["should_override_serialize.txt"].search == conf.search
315+
assert file_map["should_override_serialize.txt"].replace == conf.replace
316+
assert file_map["should_override_serialize.txt"].regex == conf.regex
317+
assert file_map["should_override_serialize.txt"].ignore_missing_version == conf.ignore_missing_version
318+
319+
assert file_map["should_override_ignore_missing.txt"].parse == conf.parse
320+
assert file_map["should_override_ignore_missing.txt"].serialize == conf.serialize
321+
assert file_map["should_override_ignore_missing.txt"].search == conf.search
322+
assert file_map["should_override_ignore_missing.txt"].replace == conf.replace
323+
assert file_map["should_override_ignore_missing.txt"].regex == conf.regex
324+
assert file_map["should_override_ignore_missing.txt"].ignore_missing_version is False
325+
326+
assert file_map["should_override_regex.txt"].parse == conf.parse
327+
assert file_map["should_override_regex.txt"].serialize == conf.serialize
328+
assert file_map["should_override_regex.txt"].search == conf.search
329+
assert file_map["should_override_regex.txt"].replace == conf.replace
330+
assert file_map["should_override_regex.txt"].regex is False
331+
assert file_map["should_override_regex.txt"].ignore_missing_version == conf.ignore_missing_version

0 commit comments

Comments
 (0)