refactor run_checks to not take an Optional list of filenames

This commit is contained in:
Anthony Sottile 2021-11-25 15:45:01 -05:00
parent 6e5600f8ec
commit 77a054688b
6 changed files with 18 additions and 22 deletions

View file

@ -103,7 +103,8 @@ class StyleGuide:
:rtype:
flake8.api.legacy.Report
"""
self._application.run_checks(paths)
self._application.options.filenames = paths
self._application.run_checks()
self._application.report_errors()
return Report(self._application)

View file

@ -56,21 +56,18 @@ class Manager:
together and make our output deterministic.
"""
def __init__(self, style_guide, arguments, checker_plugins):
def __init__(self, style_guide, checker_plugins):
"""Initialize our Manager instance.
:param style_guide:
The instantiated style guide for this instance of Flake8.
:type style_guide:
flake8.style_guide.StyleGuide
:param list arguments:
The extra arguments parsed from the CLI (if any)
:param checker_plugins:
The plugins representing checks parsed from entry-points.
:type checker_plugins:
flake8.plugins.manager.Checkers
"""
self.arguments = arguments
self.style_guide = style_guide
self.options = style_guide.options
self.checks = checker_plugins
@ -112,7 +109,7 @@ class Manager:
)
return 0
if utils.is_using_stdin(self.arguments):
if utils.is_using_stdin(self.options.filenames):
LOG.warning(
"The --jobs option is not compatible with supplying "
"input using - . Ignoring --jobs arguments."
@ -159,7 +156,7 @@ class Manager:
def make_checkers(self, paths: Optional[List[str]] = None) -> None:
"""Create checkers for each file."""
if paths is None:
paths = self.arguments
paths = self.options.filenames
checks = self.checks.to_dictionary()
self._all_checkers = [

View file

@ -246,28 +246,25 @@ class Application:
def make_file_checker_manager(self) -> None:
"""Initialize our FileChecker Manager."""
assert self.options is not None
self.file_checker_manager = checker.Manager(
style_guide=self.guide,
arguments=self.options.filenames,
checker_plugins=self.check_plugins,
)
def run_checks(self, files: Optional[List[str]] = None) -> None:
def run_checks(self) -> None:
"""Run the actual checks with the FileChecker Manager.
This method encapsulates the logic to make a
:class:`~flake8.checker.Manger` instance run the checks it is
managing.
:param list files:
List of filenames to process
"""
assert self.file_checker_manager is not None
if self.running_against_diff:
files = sorted(self.parsed_diff)
files: Optional[List[str]] = sorted(self.parsed_diff)
if not files:
return
else:
files = None
self.file_checker_manager.start(files)
try:

View file

@ -293,7 +293,7 @@ def test_report_order(results, expected_order):
# Create a placeholder manager without arguments or plugins
# Just add one custom file checker which just provides the results
manager = checker.Manager(style_guide, [], [])
manager = checker.Manager(style_guide, [])
manager.checkers = manager._all_checkers = [file_checker]
# _handle_results is the first place which gets the sorted result

View file

@ -22,7 +22,7 @@ def style_guide_mock():
def _parallel_checker_manager():
"""Call Manager.run() and return the number of calls to `run_serial`."""
style_guide = style_guide_mock()
manager = checker.Manager(style_guide, [], [])
manager = checker.Manager(style_guide, [])
# multiple checkers is needed for parallel mode
manager.checkers = [mock.Mock(), mock.Mock()]
return manager
@ -54,7 +54,7 @@ def test_oserrors_are_reraised(_):
def test_multiprocessing_is_disabled(_):
"""Verify not being able to import multiprocessing forces jobs to 0."""
style_guide = style_guide_mock()
manager = checker.Manager(style_guide, [], [])
manager = checker.Manager(style_guide, [])
assert manager.jobs == 0
@ -68,7 +68,7 @@ def test_multiprocessing_cpu_count_not_implemented():
"cpu_count",
side_effect=NotImplementedError,
):
manager = checker.Manager(style_guide, [], [])
manager = checker.Manager(style_guide, [])
assert manager.jobs == 0
@ -76,14 +76,14 @@ def test_multiprocessing_cpu_count_not_implemented():
def test_make_checkers(_):
"""Verify that we create a list of FileChecker instances."""
style_guide = style_guide_mock()
files = ["file1", "file2"]
style_guide.options.filenames = ["file1", "file2"]
checkplugins = mock.Mock()
checkplugins.to_dictionary.return_value = {
"ast_plugins": [],
"logical_line_plugins": [],
"physical_line_plugins": [],
}
manager = checker.Manager(style_guide, files, checkplugins)
manager = checker.Manager(style_guide, checkplugins)
with mock.patch("flake8.utils.fnmatch", return_value=True):
with mock.patch("flake8.processor.FileProcessor"):
@ -91,5 +91,5 @@ def test_make_checkers(_):
assert manager._all_checkers
for file_checker in manager._all_checkers:
assert file_checker.filename in files
assert file_checker.filename in style_guide.options.filenames
assert not manager.checkers # the files don't exist

View file

@ -68,7 +68,8 @@ def test_styleguide_check_files():
style_guide = api.StyleGuide(app)
report = style_guide.check_files(paths)
app.run_checks.assert_called_once_with(paths)
assert app.options.filenames == paths
app.run_checks.assert_called_once_with()
app.report_errors.assert_called_once_with()
assert isinstance(report, api.Report)