xonsh/tests/test_news.py
pre-commit-ci[bot] 66c0490d37
[pre-commit.ci] pre-commit autoupdate (#5271)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/psf/black: 23.12.1 → 24.1.1](https://github.com/psf/black/compare/23.12.1...24.1.1)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-01-30 12:23:50 +01:00

73 lines
2.1 KiB
Python

"""Testing that news entries are well formed."""
import os
import re
import pytest
NEWSDIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "news")
CATEGORIES = frozenset(
["Added", "Changed", "Deprecated", "Removed", "Fixed", "Security"]
)
single_grave_reg = re.compile(r"[^`]`[^`]+`[^`_]")
def check_news_file(fname):
import restructuredtext_lint
name = fname.name
with open(fname.path) as f:
content = f.read()
errors = restructuredtext_lint.lint(content)
if errors:
err_msgs = os.linesep.join(err.message for err in errors)
pytest.fail(f"{fname}: Invalid ReST\n{err_msgs}")
form = ""
for i, l in enumerate(content.splitlines()):
# determine the form of line
if l.startswith("**"):
cat = l[2:].rsplit(":")[0]
if cat not in CATEGORIES:
pytest.fail(
f"{name}:{i + 1}: {cat!r} not a proper category "
f"must be one of {list(CATEGORIES)}"
"",
pytrace=True,
)
if l.endswith("None"):
form += "3"
else:
form += "2"
elif l.startswith("* <news item>"):
form += "4"
elif l.startswith("* ") or l.startswith("- ") or l.startswith(" "):
form += "1"
elif l.strip() == "":
form += "0"
else:
pytest.fail(f"{name}:{i + 1}: invalid rst", pytrace=True)
# The file should have:
# empty lines around categories
# at least one content line in a non null category
reg = re.compile(r"^(3(0|$)|20(1|4)(1|0|4)*0|204$)+$")
if not reg.match(form):
print(form)
pytest.fail(f"{name}: invalid rst", pytrace=True)
@pytest.fixture(params=list(os.scandir(NEWSDIR)))
def fname(request):
if request.node.config.option.markexpr != "news":
pytest.skip("Run news items check explicitly")
return request.param
@pytest.mark.news
def test_news(fname):
base, ext = os.path.splitext(fname.path)
assert "rst" in ext
check_news_file(fname)