Skip to content

Commit 71a204b

Browse files
committed
Added tests for version parsing errors
1 parent 9cb8f60 commit 71a204b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

bumpversion/version_part.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from copy import copy
66
from typing import Any, Dict, List, MutableMapping, Optional
77

8+
from click import UsageError
9+
810
from bumpversion.config import VersionPartConfig
911
from bumpversion.exceptions import FormattingError, InvalidVersionPartError, MissingValueError
1012
from bumpversion.functions import NumericFunction, PartFunction, ValuesFunction
@@ -132,8 +134,7 @@ def __init__(
132134
try:
133135
self.parse_regex = re.compile(parse, re.VERBOSE)
134136
except re.error as e:
135-
logger.error("--parse '%s' is not a valid regex. %s", parse, e)
136-
raise e
137+
raise UsageError(f"--parse '{parse}' is not a valid regex.") from e
137138

138139
self.serialize_formats = serialize
139140
self.part_configs = part_configs or {}

tests/test_version_part.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from pathlib import Path
2+
13
import pytest
2-
from pytest import param
4+
from click import UsageError
5+
from pytest import LogCaptureFixture, param
36

47
from bumpversion import config, exceptions
58
from bumpversion.utils import get_context
69
from bumpversion.version_part import VersionPart
7-
from tests.conftest import get_config_data
10+
from tests.conftest import get_config_data, inside_dir
811

912

1013
@pytest.fixture(
@@ -274,3 +277,24 @@ def test_part_first_value(initial: str, bump_type: str, expected: str):
274277

275278
new_version = current_version.bump(bump_type, version_config.order)
276279
assert version_config.serialize(new_version, get_context(conf)) == expected
280+
281+
282+
def test_version_part_invalid_regex_exit(tmp_path: Path) -> None:
283+
"""A version part with an invalid regex should raise an exception."""
284+
# Arrange
285+
overrides = {
286+
"current_version": "12",
287+
"parse": "*kittens*",
288+
}
289+
with inside_dir(tmp_path):
290+
with pytest.raises(UsageError):
291+
get_config_data(overrides)
292+
293+
294+
def test_parse_doesnt_parse_current_version(tmp_path: Path, caplog: LogCaptureFixture) -> None:
295+
"""A warning should be output when the parse regex doesn't parse the version."""
296+
overrides = {"current_version": "12", "parse": "xxx"}
297+
with inside_dir(tmp_path):
298+
get_config_data(overrides)
299+
300+
assert caplog.messages == ["Evaluating 'parse' option: 'xxx' does not parse current version '12'"]

0 commit comments

Comments
 (0)