xonsh/tests/test_news.py

73 lines
2.1 KiB
Python
Raw Normal View History

2016-07-05 14:42:18 -04:00
"""Testing that news entries are well formed."""
import os
2016-09-05 21:27:06 +03:00
import re
2016-07-05 14:42:18 -04:00
2016-09-05 22:49:11 +03:00
import pytest
2018-08-30 09:18:49 -05:00
NEWSDIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "news")
2016-09-05 14:33:42 +03:00
2018-08-30 09:18:49 -05:00
CATEGORIES = frozenset(
["Added", "Changed", "Deprecated", "Removed", "Fixed", "Security"]
)
single_grave_reg = re.compile(r"[^`]`[^`]+`[^`_]")
2016-07-05 14:42:18 -04:00
def check_news_file(fname):
import restructuredtext_lint
2016-09-05 22:36:12 +03:00
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}")
2018-06-06 12:28:23 +05:30
form = ""
for i, l in enumerate(content.splitlines()):
2018-06-06 12:28:23 +05:30
# determine the form of line
2018-08-30 09:18:49 -05:00
if l.startswith("**"):
cat = l[2:].rsplit(":")[0]
2018-06-06 12:34:30 +05:30
if cat not in CATEGORIES:
2018-08-30 09:18:49 -05:00
pytest.fail(
"{}:{}: {!r} not a proper category "
"must be one of {}"
"".format(name, i + 1, cat, list(CATEGORIES)),
2018-12-22 12:00:22 -08:00
pytrace=True,
2018-08-30 09:18:49 -05:00
)
if l.endswith("None"):
form += "3"
2016-07-05 14:42:18 -04:00
else:
2018-08-30 09:18:49 -05:00
form += "2"
2018-10-20 14:12:09 -06:00
elif l.startswith("* <news item>"):
2018-10-20 14:07:18 -06:00
form += "4"
2018-08-30 09:18:49 -05:00
elif l.startswith("* ") or l.startswith("- ") or l.startswith(" "):
form += "1"
elif l.strip() == "":
form += "0"
2016-07-05 14:42:18 -04:00
else:
pytest.fail(f"{name}:{i + 1}: invalid rst", pytrace=True)
2018-06-06 12:28:23 +05:30
# The file should have:
# empty lines around categories
# at least one content line in a non null category
2018-10-20 14:07:18 -06:00
reg = re.compile(r"^(3(0|$)|20(1|4)(1|0|4)*0|204$)+$")
2018-06-06 12:28:23 +05:30
if not reg.match(form):
2018-10-20 14:07:18 -06:00
print(form)
pytest.fail(f"{name}: invalid rst", pytrace=True)
2016-07-05 14:42:18 -04:00
@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
2016-09-05 14:33:42 +03:00
def test_news(fname):
base, ext = os.path.splitext(fname.path)
2018-08-30 09:18:49 -05:00
assert "rst" in ext
2016-09-05 22:36:12 +03:00
check_news_file(fname)