From 058ebda126f72dda8bf85b506cc59e7199423c8d Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sat, 7 Aug 2021 22:21:36 +0200 Subject: [PATCH] Add test for crash file and add a class attribute for pylinter --- pylint/lint/pylinter.py | 6 ++++-- tests/lint/test_pylinter.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 65914f7a59f..3c82a826b01 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -11,6 +11,8 @@ import traceback import warnings from io import TextIOWrapper +from pathlib import Path +from typing import Union import astroid from astroid import AstroidError @@ -170,6 +172,7 @@ class PyLinter( priority = 0 level = 0 msgs = MSGS + crash_file_prefix: Union[str, Path] = "pylint-crash-" @staticmethod def make_options(): @@ -965,7 +968,6 @@ def check(self, files_or_modules): files_or_modules is either a string or list of strings presenting modules to check. """ - self.initialize() if not isinstance(files_or_modules, (list, tuple)): @@ -1027,7 +1029,7 @@ def _check_files(self, get_ast, file_descrs): except Exception as ex: # pylint: disable=broad-except error = ex template_path = prepare_crash_report( - error, filepath, "pylint-crash-" + error, filepath, self.crash_file_prefix ) if error is not None: msg = get_fatal_error_message(filepath, template_path) diff --git a/tests/lint/test_pylinter.py b/tests/lint/test_pylinter.py index e69de29bb2d..03daf6c5c84 100644 --- a/tests/lint/test_pylinter.py +++ b/tests/lint/test_pylinter.py @@ -0,0 +1,25 @@ +from unittest.mock import patch + +from astroid import AstroidBuildingError + +from pylint.utils import FileState + + +def raise_exception(*args, **kwargs): + raise AstroidBuildingError(modname="spam") + + +@patch.object(FileState, "iter_spurious_suppression_messages", raise_exception) +def test_crash_in_file(linter, capsys, tmpdir): + args = linter.load_command_line_configuration([__file__]) + linter.crash_file_prefix = tmpdir / "pylint-crash-" + linter.check(args) + out, err = capsys.readouterr() + assert not out + assert not err + files = tmpdir.listdir() + assert len(files) == 1 + assert "pylint-crash" in str(files[0]) + with open(files[0], encoding="utf8") as f: + content = f.read() + assert "Failed to import module spam." in content