2016-07-05 14:42:18 -04:00
|
|
|
"""Testing that news entries are well formed."""
|
2024-01-30 12:23:50 +01:00
|
|
|
|
2016-07-05 14:42:18 -04:00
|
|
|
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):
|
2021-07-28 19:46:35 +05:30
|
|
|
import restructuredtext_lint
|
|
|
|
|
2016-09-05 22:36:12 +03:00
|
|
|
name = fname.name
|
|
|
|
with open(fname.path) as f:
|
2021-07-28 19:46:35 +05:30
|
|
|
content = f.read()
|
|
|
|
errors = restructuredtext_lint.lint(content)
|
|
|
|
|
|
|
|
if errors:
|
2021-12-07 01:12:26 +05:30
|
|
|
err_msgs = os.linesep.join(err.message for err in errors)
|
2021-07-28 19:46:35 +05:30
|
|
|
pytest.fail(f"{fname}: Invalid ReST\n{err_msgs}")
|
2018-06-06 12:28:23 +05:30
|
|
|
|
2021-07-28 19:46:35 +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(
|
2023-09-18 05:25:53 +00:00
|
|
|
f"{name}:{i + 1}: {cat!r} not a proper category "
|
|
|
|
f"must be one of {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:
|
2021-12-07 01:12:26 +05:30
|
|
|
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)
|
2021-12-07 01:12:26 +05:30
|
|
|
pytest.fail(f"{name}: invalid rst", pytrace=True)
|
2016-07-05 14:42:18 -04:00
|
|
|
|
|
|
|
|
2021-06-11 17:31:35 +05:30
|
|
|
@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)
|