Debian Codegraph @

tests/test_infra.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # Copyright 2017-2024 Facundo Batista, Nicolás Demarchi
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 3, as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranties of
9 # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10 # PURPOSE. See the GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program. If not, see <http://www.gnu.org/licenses/>.
14 #
15 # For further info, check https://github.com/PyAr/fades
16
17 """Tests for infrastructure stuff."""
18
19 import io
20 import logging
21 from unittest.mock import patch
22
23 import docutils.core
24 import pydocstyle
25 import pytest
26 import rst2html5_
27 from flake8.api.legacy import get_style_guide
28 from pyuca import Collator
29
30 from tests import get_python_filepaths
31
32 FLAKE8_ROOTS = ['fades', 'tests']
33 FLAKE8_OPTIONS = {'max_line_length': 99, 'select': ['E', 'W', 'F', 'C', 'N']}
34 PEP257_ROOTS = ['fades']
35
36 # avoid seeing all DEBUG logs if the test fails
37 for logger_name in ('flake8.plugins', 'flake8.api', 'flake8.checker', 'flake8.main'):
38 logging.getLogger(logger_name).setLevel(logging.CRITICAL)
39
40
41 def test_flake8_pytest(capsys):
42 python_filepaths = get_python_filepaths(FLAKE8_ROOTS)
43 style_guide = get_style_guide(**FLAKE8_OPTIONS)
44 report = style_guide.check_files(python_filepaths)
45 if report.total_errors != 0:
46 out, _ = capsys.readouterr()
47 pytest.fail(f"There are {report.total_errors} issues!\n{''.join(out)}")
48
49
50 def test_pep257_pytest():
51 python_filepaths = get_python_filepaths(PEP257_ROOTS)
52 to_ignore = {
53 "D105", # Missing docstring in magic method
54 "D107", # Missing docstring in __init__
55 }
56 to_include = pydocstyle.violations.conventions.pep257 - to_ignore
57 result = list(pydocstyle.check(python_filepaths, select=to_include))
58 assert len(result) == 0, "There are issues!\n" + '\n'.join(map(str, result))
59
60
61 def test_readme_sanity():
62 fake_stdout = io.StringIO() # just to ignore the output
63 fake_stderr = io.StringIO() # will have content if there are problems
64 with open('README.rst', 'rt', encoding='utf8') as fh:
65 with patch('sys.stdout', fake_stdout):
66 with patch('sys.stderr', fake_stderr):
67 docutils.core.publish_file(source=fh, writer=rst2html5_.HTML5Writer())
68
69 errors = fake_stderr.getvalue()
70 assert not bool(errors), "There are issues!\n" + errors
71
72
73 def test_authors_ordering():
74 with open('AUTHORS', 'rt', encoding='utf8') as fh:
75 authors = fh.readlines()
76 ordered_authors = sorted(authors, key=Collator().sort_key)
77 assert authors == ordered_authors