Skip to content

Commit

Permalink
Added tests for version parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Apr 10, 2023
1 parent 9cb8f60 commit 71a204b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions bumpversion/version_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from copy import copy
from typing import Any, Dict, List, MutableMapping, Optional

from click import UsageError

from bumpversion.config import VersionPartConfig
from bumpversion.exceptions import FormattingError, InvalidVersionPartError, MissingValueError
from bumpversion.functions import NumericFunction, PartFunction, ValuesFunction
Expand Down Expand Up @@ -132,8 +134,7 @@ def __init__(
try:
self.parse_regex = re.compile(parse, re.VERBOSE)
except re.error as e:
logger.error("--parse '%s' is not a valid regex. %s", parse, e)
raise e
raise UsageError(f"--parse '{parse}' is not a valid regex.") from e

self.serialize_formats = serialize
self.part_configs = part_configs or {}
Expand Down
28 changes: 26 additions & 2 deletions tests/test_version_part.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from pathlib import Path

import pytest
from pytest import param
from click import UsageError
from pytest import LogCaptureFixture, param

from bumpversion import config, exceptions
from bumpversion.utils import get_context
from bumpversion.version_part import VersionPart
from tests.conftest import get_config_data
from tests.conftest import get_config_data, inside_dir


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

new_version = current_version.bump(bump_type, version_config.order)
assert version_config.serialize(new_version, get_context(conf)) == expected


def test_version_part_invalid_regex_exit(tmp_path: Path) -> None:
"""A version part with an invalid regex should raise an exception."""
# Arrange
overrides = {
"current_version": "12",
"parse": "*kittens*",
}
with inside_dir(tmp_path):
with pytest.raises(UsageError):
get_config_data(overrides)


def test_parse_doesnt_parse_current_version(tmp_path: Path, caplog: LogCaptureFixture) -> None:
"""A warning should be output when the parse regex doesn't parse the version."""
overrides = {"current_version": "12", "parse": "xxx"}
with inside_dir(tmp_path):
get_config_data(overrides)

assert caplog.messages == ["Evaluating 'parse' option: 'xxx' does not parse current version '12'"]

0 comments on commit 71a204b

Please sign in to comment.