Skip to content

Commit

Permalink
Merge pull request #841 from RonnyPfannschmidt/cleanups
Browse files Browse the repository at this point in the history
fix #838 - handle empty tag regex
  • Loading branch information
RonnyPfannschmidt committed Apr 27, 2023
2 parents 126a49c + 8af496d commit 20083e1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
44 changes: 28 additions & 16 deletions 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 All @@ -143,23 +152,26 @@ def get_version(


def _get_version(config: Configuration) -> str | None:
parsed_version = _do_parse(config)
if parsed_version is None:
return None
version_string = _format_version(
parsed_version,
version_scheme=config.version_scheme,
local_scheme=config.local_scheme,
)
if config.write_to is not None:
dump_version(
root=config.root,
version=version_string,
write_to=config.write_to,
template=config.write_to_template,
from ._log import magic_debug

with magic_debug():
parsed_version = _do_parse(config)
if parsed_version is None:
return None
version_string = _format_version(
parsed_version,
version_scheme=config.version_scheme,
local_scheme=config.local_scheme,
)
if config.write_to is not None:
dump_version(
root=config.root,
version=version_string,
write_to=config.write_to,
template=config.write_to_template,
)

return version_string
return version_string


# Public API
Expand Down
6 changes: 6 additions & 0 deletions src/setuptools_scm/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ def enable_debug(handler: logging.Handler = _default_handler) -> Iterator[None]:
handler.setLevel(old_handler_level)
if handler is not _default_handler:
log.removeHandler(handler)


@contextlib.contextmanager
def magic_debug() -> Iterator[None]:
with enable_debug():
yield
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 20083e1

Please sign in to comment.