Skip to content

Commit 93191f3

Browse files
committed
Changed default regex CLI value to None.
Fixes #64 The default value of False was overriding other values.
1 parent d96e07a commit 93191f3

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

bumpversion/cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def cli(ctx: Context) -> None:
141141
)
142142
@click.option(
143143
"--regex/--no-regex",
144-
default=False,
144+
default=None,
145145
envvar="BUMPVERSION_REGEX",
146146
help="Treat the search parameter as a regular expression or explicitly do not treat it as a regular expression.",
147147
)
@@ -232,7 +232,7 @@ def bump(
232232
serialize: Optional[List[str]],
233233
search: Optional[str],
234234
replace: Optional[str],
235-
regex: bool,
235+
regex: Optional[bool],
236236
no_configured_files: bool,
237237
ignore_missing_version: bool,
238238
dry_run: bool,

bumpversion/files.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Methods for changing files."""
2+
import os.path
23
import re
34
from copy import deepcopy
45
from difflib import context_diff
@@ -94,7 +95,18 @@ def __init__(
9495
self._newlines: Optional[str] = None
9596

9697
def get_file_contents(self) -> str:
97-
"""Return the contents of the file."""
98+
"""
99+
Return the contents of the file.
100+
101+
Raises:
102+
FileNotFoundError: if the file doesn't exist
103+
104+
Returns:
105+
The contents of the file
106+
"""
107+
if not os.path.exists(self.file_change.filename):
108+
raise FileNotFoundError(f"File not found: '{self.file_change.filename}'")
109+
98110
with open(self.file_change.filename, "rt", encoding="utf-8") as f:
99111
contents = f.read()
100112
self._newlines = f.newlines[0] if isinstance(f.newlines, tuple) else f.newlines

bumpversion/version_part.py

+12
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ def __init__(
150150
self.search = search
151151
self.replace = replace
152152

153+
def __repr__(self) -> str:
154+
return f"<bumpversion.VersionConfig:{self.parse_regex.pattern}:{self.serialize_formats}>"
155+
156+
def __eq__(self, other: Any) -> bool:
157+
return (
158+
self.parse_regex.pattern == other.parse_regex.pattern
159+
and self.serialize_formats == other.serialize_formats
160+
and self.part_configs == other.part_configs
161+
and self.search == other.search
162+
and self.replace == other.replace
163+
)
164+
153165
@property
154166
def order(self) -> List[str]:
155167
"""

tests/fixtures/basic_cfg.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ replace = """**unreleased**
4141
[tool.bumpversion.parts.release]
4242
optional_value = "gamma"
4343
values =[
44-
"dev",
45-
"gamma",
44+
"dev",
45+
"gamma",
4646
]

tests/fixtures/regex_test_config.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[tool.bumpversion]
2+
current_version = "4.7.1"
3+
regex = true
4+
5+
[[tool.bumpversion.files]]
6+
filename = "./citation.cff"
7+
search = "date-released: \\d{{4}}-\\d{{2}}-\\d{{2}}"
8+
replace = "date-released: {utcnow:%Y-%m-%d}"

tests/test_cli.py

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
import subprocess
44
import traceback
5+
from datetime import datetime
56
from pathlib import Path
67

78
import pytest
@@ -45,6 +46,32 @@ def test_bump_no_configured_files(mocker, tmp_path):
4546
assert len(call_args[2].files) == 0
4647

4748

49+
def test_bump_nested_regex(tmp_path: Path, fixtures_path: Path, caplog):
50+
"""
51+
Arrange/Act: Run the `bump` subcommand with --no-configured-files.
52+
53+
Assert: There is no configured files specified to modify
54+
"""
55+
cff_path = tmp_path / "citation.cff"
56+
cff_path.write_text("cff-version: 1.2.0\ndate-released: 2023-09-19\n")
57+
content = fixtures_path.joinpath("regex_test_config.toml").read_text()
58+
config_path = tmp_path / ".bumpversion.toml"
59+
config_path.write_text(content)
60+
61+
runner: CliRunner = CliRunner()
62+
with inside_dir(tmp_path):
63+
result: Result = runner.invoke(cli.cli, ["bump", "-vv", "patch"])
64+
65+
if result.exit_code != 0:
66+
print(result.output)
67+
print(caplog.text)
68+
69+
assert result.exit_code == 0
70+
71+
now = datetime.now().isoformat()[:10]
72+
assert cff_path.read_text() == f"cff-version: 1.2.0\ndate-released: {now}\n"
73+
74+
4875
def test_bump_legacy(mocker, tmp_path):
4976
"""
5077
Arrange/Act: Run the `bump` subcommand with --no-configured-files.

0 commit comments

Comments
 (0)