Skip to content

Commit

Permalink
Test rst to md conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Dec 3, 2024
1 parent 85e12ed commit 4c2dd7d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
15 changes: 9 additions & 6 deletions extra/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ def rst2md(text: str) -> str:
)


def changelog_as_markdown() -> str:
"""Get the latest changelog entry as hacked up Markdown."""
contents = CHANGELOG.read_text()
def get_changelog_contents() -> str | None:
if m := RST_LATEST_CHANGES.search(CHANGELOG.read_text()):
return m.group(1)

return None

m = RST_LATEST_CHANGES.search(contents)
rst = m.group(1) if m else ""

def changelog_as_markdown(rst: str) -> str:
"""Get the latest changelog entry as hacked up Markdown."""
for pattern, repl in RST_REPLACEMENTS:
rst = re.sub(pattern, repl, rst, flags=re.M)

Expand Down Expand Up @@ -155,7 +157,8 @@ def bump(version: Version) -> None:
@cli.command()
def changelog():
"""Get the most recent version's changelog as Markdown."""
print(changelog_as_markdown())
if changelog := get_changelog_contents():
print(changelog_as_markdown(changelog))


if __name__ == "__main__":
Expand Down
64 changes: 64 additions & 0 deletions test/test_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Tests for the release utils."""

import pytest

from extra.release import changelog_as_markdown


@pytest.fixture
def rst_changelog():
return """New features:
* :doc:`/plugins/substitute`: Some substitute
multi-line change.
:bug:`5467`
Bug fixes:
* Some fix that refers to an issue.
:bug:`5467`
* Some fix that mentions a user @user.
Empty section:
Other changes:
* Changed `bitesize` label to `good first issue`. Our `contribute`_ page is now
automatically populated with these issues. :bug:`4855`
.. _contribute: https://github.com/beetbox/beets/contribute
2.1.0 (November 22, 2024)
-------------------------
Bug fixes:
* Fixed something."""


@pytest.fixture
def md_changelog():
return r"""### New features
- Plugin **`substitute`**: Some substitute multi-line change. :bug: (\#5467)
### Bug fixes
- Some fix that mentions a user @user.
- Some fix that refers to an issue. :bug: (\#5467)
### Other changes
# 2.1.0 (November 22, 2024)
- Changed `bitesize` label to `good first issue`. Our [contribute](https://github.com/beetbox/beets/contribute) page is now automatically populated with these issues. :bug: (\#4855)
### Bug fixes
- Fixed something.""" # noqa: E501


def test_convert_rst_to_md(rst_changelog, md_changelog):
actual = changelog_as_markdown(rst_changelog)

assert actual == md_changelog

0 comments on commit 4c2dd7d

Please sign in to comment.