Skip to content

Commit

Permalink
fix #884: handle v prefixes for calver by date
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Sep 11, 2023
1 parent 357f6e0 commit ada960d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ bugfixes
--------

* fix #883: use HeadersParser to ensure only mime metadata in headers is used
* fix #884: parse calver dates from versions with the v prefix

v7.1.0
======
Expand Down
12 changes: 11 additions & 1 deletion src/setuptools_scm/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,13 @@ def no_guess_dev_version(version: ScmVersion) -> str:


_DATE_REGEX = re.compile(
r"^(?P<date>(?P<year>\d{2}|\d{4})(?:\.\d{1,2}){2})(?:\.(?P<patch>\d*))?$"
r"""
^(?P<date>
(?P<prefix>[vV]?)
(?P<year>\d{2}|\d{4})(?:\.\d{1,2}){2})
(?:\.(?P<patch>\d*))?$
""",
re.VERBOSE,
)


Expand Down Expand Up @@ -339,6 +345,10 @@ def guess_next_date_ver(
# deduct date format if not provided
if date_fmt is None:
date_fmt = "%Y.%m.%d" if len(match.group("year")) == 4 else "%y.%m.%d"
if prefix := match.group("prefix"):
if not date_fmt.startswith(prefix):
date_fmt = prefix + date_fmt

today = version.time.date()
head_date = node_date or today
# compute patch
Expand Down
24 changes: 24 additions & 0 deletions testing/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from setuptools_scm import NonNormalizedVersion
from setuptools_scm.version import calver_by_date
from setuptools_scm.version import format_version
from setuptools_scm.version import guess_next_date_ver
from setuptools_scm.version import guess_next_version
from setuptools_scm.version import meta
from setuptools_scm.version import no_guess_dev_version
Expand Down Expand Up @@ -358,6 +359,29 @@ def test_calver_by_date_future_warning() -> None:
)


@pytest.mark.parametrize(
["tag", "node_date", "expected"],
[
pytest.param("20.03.03", date(2020, 3, 4), "20.03.04.0", id="next day"),
pytest.param("20.03.03", date(2020, 3, 3), "20.03.03.1", id="same day"),
pytest.param(
"20.03.03.2", date(2020, 3, 3), "20.03.03.3", id="same day with patch"
),
pytest.param(
"v20.03.03", date(2020, 3, 4), "v20.03.04.0", id="next day with v prefix"
),
],
)
def test_calver_guess_next_data(tag: str, node_date: date, expected: str) -> None:
version = meta(tag, config=c_non_normalize, node_date=node_date)
next = guess_next_date_ver(
version,
node_date=node_date,
version_cls=c_non_normalize.version_cls,
)
assert next == expected


def test_custom_version_cls() -> None:
"""Test that we can pass our own version class instead of pkg_resources"""

Expand Down

0 comments on commit ada960d

Please sign in to comment.