Skip to content

Commit

Permalink
Provide better error messages about version mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jan 28, 2022
1 parent 74f7125 commit c1d23cc
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 218 deletions.
36 changes: 32 additions & 4 deletions get_releasenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def parse_changes(

return _parse_changes(
changes=changes,
changes_file=changes_file,
version=ctx.version,
start_line=start_line,
head_line=head_line,
Expand All @@ -96,6 +97,7 @@ def parse_changes(
def _parse_changes(
*,
changes: str,
changes_file: str,
version: str,
start_line: str,
head_line: str,
Expand All @@ -105,7 +107,10 @@ def _parse_changes(
) -> str:
top, sep, msg = changes.partition(start_line)
if not sep:
raise ValueError(f"Cannot find TOWNCRIER start mark ({start_line!r})")
raise ValueError(
f"Cannot find TOWNCRIER start mark ({start_line!r}) "
"in file '{changes_file}'"
)

msg = msg.strip()
head_re = re.compile(
Expand All @@ -119,11 +124,11 @@ def _parse_changes(
match = head_re.match(msg)
if match is None:
raise ValueError(
f"Cannot find TOWNCRIER version head mark ({head_re.pattern!r})"
f"Cannot find TOWNCRIER version head mark ({head_re.pattern!r}) "
f"in file '{changes_file}'"
)
found_version = match.group("version")
if version != found_version:
raise ValueError(f"Version check mismatch: {version} != {found_version}")
check_changes_version(version, found_version, changes_file)

match2 = head_re.search(msg, match.end())
if match2 is not None:
Expand All @@ -138,6 +143,29 @@ def _parse_changes(
return msg.strip()


def check_changes_version(
declared_version: str, found_version: str, changes_file: str
) -> None:
if declared_version == found_version:
return
dver = parse_version(declared_version)
fver = parse_version(found_version)

if dver < fver:
raise ValueError(
f"The distribution version {dver} is older than "
f"{fver} (from '{changes_file}').\n"
"Hint: push git tag with the latest version."
)

else:
raise ValueError(
f"The distribution version {dver} is younger than "
f"{fver} (from '{changes_file}').\n"
"Hint: run 'towncrier' again."
)


VERSION_RE = re.compile(
"^{version} *= *{spec}".format(
version="(?:__version__|version)",
Expand Down
247 changes: 247 additions & 0 deletions tests/test_parse_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
from textwrap import dedent

import pytest

from get_releasenote import _parse_changes

START_LINE = ".. towncrier release notes start"


def test_parse_no_start_line() -> None:
with pytest.raises(ValueError, match="Cannot find TOWNCRIER start mark"):
_parse_changes(
changes="text",
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)


def test_parse_no_head_line() -> None:
CHANGES = dedent(
f"""\
{START_LINE}
NO-VERSION
"""
)
with pytest.raises(ValueError, match="Cannot find TOWNCRIER version head mark"):
_parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)


def test_parse_version_older() -> None:
CHANGES = dedent(
f"""\
{START_LINE}
1.2.4 (2020-12-16)
==================
"""
)
with pytest.raises(
ValueError, match="The distribution version 1.2.3 is older than 1.2.4"
):
_parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)


def test_parse_version_younger() -> None:
CHANGES = dedent(
f"""\
{START_LINE}
1.2.4 (2020-12-16)
==================
"""
)
with pytest.raises(
ValueError, match="The distribution version 1.2.5 is younger than 1.2.4"
):
_parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.5",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)


def test_parse_single_changes() -> None:
CHANGES = dedent(
f"""\
Header
{START_LINE}
1.2.3 (2020-12-16)
==================
Features
--------
- Feature 1 (#1024)
- Feature 2 (#1025)
"""
)
ret = _parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)
assert ret == dedent(
"""\
Features
--------
- Feature 1 (#1024)
- Feature 2 (#1025)"""
)


def test_parse_multi_changes() -> None:
CHANGES = dedent(
f"""\
Header
{START_LINE}
1.2.3 (2020-12-16)
==================
Features
--------
- Feature 1 (#1024)
- Feature 2 (#1025)
1.2.2 (2020-12-15)
==================
Bugfixes
--------
"""
)
ret = _parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)
assert ret == dedent(
"""\
Features
--------
- Feature 1 (#1024)
- Feature 2 (#1025)"""
)


def test_parse_fix_issues() -> None:
CHANGES = dedent(
f"""\
Header
{START_LINE}
1.2.3 (2020-12-16)
==================
Features
--------
- Feature 1 `#4603 <https://github.com/aio-libs/aiohttp/issues/4603>`_
"""
)
ret = _parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="{version} \\({date}\\)\n=====+\n?",
fix_issue_regex=(
"\n?\\s*`#(\\d+) <https://github.com/aio-libs/aiohttp/issues/\\1>`_"
),
fix_issue_repl=" (#\\1)",
name="name",
)
assert ret == dedent(
"""\
Features
--------
- Feature 1 (#4603)"""
)


def test_parse_with_name() -> None:
CHANGES = dedent(
f"""\
Header
{START_LINE}
Project 1.2.3 (2020-12-16)
==========================
Features
--------
- Feature 1 (#1024)
"""
)
ret = _parse_changes(
changes=CHANGES,
changes_file="CHANGES.rst",
version="1.2.3",
start_line=START_LINE,
head_line="Project {version} \\({date}\\)\n=====+\n?",
fix_issue_regex="",
fix_issue_repl="",
name="name",
)
assert ret == dedent(
"""\
Features
--------
- Feature 1 (#1024)"""
)
Loading

0 comments on commit c1d23cc

Please sign in to comment.