Skip to content

Commit

Permalink
Changed default regex CLI value to None.
Browse files Browse the repository at this point in the history
Fixes #64

The default value of False was overriding other values.
  • Loading branch information
coordt committed Dec 15, 2023
1 parent d96e07a commit 93191f3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
4 changes: 2 additions & 2 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def cli(ctx: Context) -> None:
)
@click.option(
"--regex/--no-regex",
default=False,
default=None,
envvar="BUMPVERSION_REGEX",
help="Treat the search parameter as a regular expression or explicitly do not treat it as a regular expression.",
)
Expand Down Expand Up @@ -232,7 +232,7 @@ def bump(
serialize: Optional[List[str]],
search: Optional[str],
replace: Optional[str],
regex: bool,
regex: Optional[bool],
no_configured_files: bool,
ignore_missing_version: bool,
dry_run: bool,
Expand Down
14 changes: 13 additions & 1 deletion bumpversion/files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Methods for changing files."""
import os.path
import re
from copy import deepcopy
from difflib import context_diff
Expand Down Expand Up @@ -94,7 +95,18 @@ def __init__(
self._newlines: Optional[str] = None

def get_file_contents(self) -> str:
"""Return the contents of the file."""
"""
Return the contents of the file.
Raises:
FileNotFoundError: if the file doesn't exist
Returns:
The contents of the file
"""
if not os.path.exists(self.file_change.filename):
raise FileNotFoundError(f"File not found: '{self.file_change.filename}'")

Check warning on line 108 in bumpversion/files.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/files.py#L108

Added line #L108 was not covered by tests

with open(self.file_change.filename, "rt", encoding="utf-8") as f:
contents = f.read()
self._newlines = f.newlines[0] if isinstance(f.newlines, tuple) else f.newlines
Expand Down
12 changes: 12 additions & 0 deletions bumpversion/version_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ def __init__(
self.search = search
self.replace = replace

def __repr__(self) -> str:
return f"<bumpversion.VersionConfig:{self.parse_regex.pattern}:{self.serialize_formats}>"

Check warning on line 154 in bumpversion/version_part.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/version_part.py#L154

Added line #L154 was not covered by tests

def __eq__(self, other: Any) -> bool:
return (

Check warning on line 157 in bumpversion/version_part.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/version_part.py#L157

Added line #L157 was not covered by tests
self.parse_regex.pattern == other.parse_regex.pattern
and self.serialize_formats == other.serialize_formats
and self.part_configs == other.part_configs
and self.search == other.search
and self.replace == other.replace
)

@property
def order(self) -> List[str]:
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/basic_cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ replace = """**unreleased**
[tool.bumpversion.parts.release]
optional_value = "gamma"
values =[
"dev",
"gamma",
"dev",
"gamma",
]
8 changes: 8 additions & 0 deletions tests/fixtures/regex_test_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool.bumpversion]
current_version = "4.7.1"
regex = true

[[tool.bumpversion.files]]
filename = "./citation.cff"
search = "date-released: \\d{{4}}-\\d{{2}}-\\d{{2}}"
replace = "date-released: {utcnow:%Y-%m-%d}"
27 changes: 27 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import subprocess
import traceback
from datetime import datetime
from pathlib import Path

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


def test_bump_nested_regex(tmp_path: Path, fixtures_path: Path, caplog):
"""
Arrange/Act: Run the `bump` subcommand with --no-configured-files.
Assert: There is no configured files specified to modify
"""
cff_path = tmp_path / "citation.cff"
cff_path.write_text("cff-version: 1.2.0\ndate-released: 2023-09-19\n")
content = fixtures_path.joinpath("regex_test_config.toml").read_text()
config_path = tmp_path / ".bumpversion.toml"
config_path.write_text(content)

runner: CliRunner = CliRunner()
with inside_dir(tmp_path):
result: Result = runner.invoke(cli.cli, ["bump", "-vv", "patch"])

if result.exit_code != 0:
print(result.output)
print(caplog.text)

assert result.exit_code == 0

now = datetime.now().isoformat()[:10]
assert cff_path.read_text() == f"cff-version: 1.2.0\ndate-released: {now}\n"


def test_bump_legacy(mocker, tmp_path):
"""
Arrange/Act: Run the `bump` subcommand with --no-configured-files.
Expand Down

0 comments on commit 93191f3

Please sign in to comment.