From 71a204b0eb1ea2e7ae291055f26f5c499d429f1b Mon Sep 17 00:00:00 2001 From: Corey Oordt Date: Mon, 10 Apr 2023 08:56:00 -0500 Subject: [PATCH] Added tests for version parsing errors --- bumpversion/version_part.py | 5 +++-- tests/test_version_part.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/bumpversion/version_part.py b/bumpversion/version_part.py index 6bbc8966..aa49858f 100644 --- a/bumpversion/version_part.py +++ b/bumpversion/version_part.py @@ -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 @@ -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 {} diff --git a/tests/test_version_part.py b/tests/test_version_part.py index bc11fb65..faf856de 100644 --- a/tests/test_version_part.py +++ b/tests/test_version_part.py @@ -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( @@ -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'"]