Skip to content

Commit

Permalink
fix #838: handle incorrectly passed empty tag regex as default
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Apr 27, 2023
1 parent 9fbd394 commit 8af496d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/setuptools_scm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import os
import re
import warnings
from typing import Any
from typing import Pattern
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -133,7 +134,15 @@ def get_version(
version_cls = _validate_version_cls(version_cls, normalize)
del normalize
if isinstance(tag_regex, str):
tag_regex = re.compile(tag_regex)
if tag_regex == "":
warnings.warn(
DeprecationWarning(
"empty regex for tag regex is invalid, using default"
)
)
tag_regex = DEFAULT_TAG_REGEX
else:
tag_regex = re.compile(tag_regex)
config = Configuration(**locals())
maybe_version = _get_version(config)

Expand Down
21 changes: 14 additions & 7 deletions src/setuptools_scm/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,32 @@
def _parse_version_tag(
tag: str | object, config: _config.Configuration
) -> dict[str, str] | None:
tagstring = tag if isinstance(tag, str) else str(tag)
match = config.tag_regex.match(tagstring)
match = config.tag_regex.match(str(tag))

result = None
if match:
key: str | int
if len(match.groups()) == 1:
key = 1
else:
key = "version"

full = match.group(0)
log.debug("%r %r %s", tag, config.tag_regex, match)
log.debug(
"key %s data %s, %s, %r", key, match.groupdict(), match.groups(), full
)
result = {
"version": match.group(key),
"prefix": match.group(0)[: match.start(key)],
"suffix": match.group(0)[match.end(key) :],
"prefix": full[: match.start(key)],
"suffix": full[match.end(key) :],
}

log.debug(f"tag '{tag}' parsed to {result}")
return result
log.debug("tag %r parsed to %r", tag, result)
return result
else:
log.debug("tag %r did not parse", tag)

return None


def callable_or_entrypoint(group: str, callable_or_name: str | Any) -> Any:
Expand Down
7 changes: 7 additions & 0 deletions testing/test_basic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def test_empty_pretend_version_named(
assert res.stdout == "12.34"


def test_get_version_blank_tag_regex() -> None:
with pytest.warns(
DeprecationWarning, match="empty regex for tag regex is invalid, using default"
):
setuptools_scm.get_version(tag_regex="")


@pytest.mark.parametrize(
"version", ["1.0", "1.2.3.dev1+ge871260", "1.2.3.dev15+ge871260.d20180625", "2345"]
)
Expand Down

0 comments on commit 8af496d

Please sign in to comment.