Skip to content

Commit

Permalink
Add test for crash file and add a class attribute for pylinter
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Aug 7, 2021
1 parent fdcc9d2 commit 058ebda
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,6 +172,7 @@ class PyLinter(
priority = 0
level = 0
msgs = MSGS
crash_file_prefix: Union[str, Path] = "pylint-crash-"

@staticmethod
def make_options():
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/lint/test_pylinter.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 058ebda

Please sign in to comment.