diff options
Diffstat (limited to 'tests/primer/test_primer_stdlib.py')
-rw-r--r-- | tests/primer/test_primer_stdlib.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/primer/test_primer_stdlib.py b/tests/primer/test_primer_stdlib.py new file mode 100644 index 000000000..fdc16adcd --- /dev/null +++ b/tests/primer/test_primer_stdlib.py @@ -0,0 +1,65 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE + +import contextlib +import io +import os +import sys + +import pytest +from pytest import CaptureFixture + +import pylint.lint + + +def is_module(filename: str) -> bool: + return filename.endswith(".py") + + +def is_package(filename: str, location: str) -> bool: + return os.path.exists(os.path.join(location, filename, "__init__.py")) + + +@contextlib.contextmanager +def _patch_stdout(out): + sys.stdout = out + try: + yield + finally: + sys.stdout = sys.__stdout__ + + +LIB_DIRS = [os.path.dirname(os.__file__)] +MODULES_TO_CHECK = [ + (location, module) + for location in LIB_DIRS + for module in os.listdir(location) + if is_module(module) or is_package(module, location) +] +MODULES_NAMES = [m[1] for m in MODULES_TO_CHECK] + + +@pytest.mark.primer_stdlib +@pytest.mark.parametrize( + ("test_module_location", "test_module_name"), MODULES_TO_CHECK, ids=MODULES_NAMES +) +def test_lib_module_no_crash( + test_module_location: str, test_module_name: str, capsys: CaptureFixture +) -> None: + """Test that pylint does not produces any crashes or fatal errors on stdlib modules""" + os.chdir(test_module_location) + with _patch_stdout(io.StringIO()): + try: + # We want to test all the code we can + enables = ["--enable-all-extensions", "--enable=all"] + # Duplicate code takes too long and is relatively safe + # We don't want to lint the test directory which are repetitive + disables = ["--disable=duplicate-code", "--ignore=test"] + pylint.lint.Run([test_module_name] + enables + disables) + except SystemExit as ex: + out, err = capsys.readouterr() + assert not err, err + assert not out + msg = f"Encountered {{}} during primer stlib test for {test_module_name}" + assert ex.code != 32, msg.format("a crash") + assert ex.code % 2 == 0, msg.format("a message of category 'fatal'") |