File: test_news.py

package info (click to toggle)
xonsh 0.13.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,024 kB
  • sloc: python: 46,350; makefile: 136; sh: 41; xml: 17
file content (72 lines) | stat: -rw-r--r-- 2,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""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(
                    "{}:{}: {!r} not a proper category "
                    "must be one of {}"
                    "".format(name, i + 1, cat, 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)