application: Remove configuration finder state

This change removes the `.config_finder` object from the `Application`.
Since the configuration finder is only needed during initialization, we
constrain the finder to be returned and passed to other methods
necessary for initialization.
This commit is contained in:
Eric N. Vander Weele 2019-11-09 09:55:01 +08:00
parent c9209507a8
commit 594c16abb4
3 changed files with 26 additions and 16 deletions

View file

@ -30,12 +30,14 @@ def get_style_guide(**kwargs):
application = app.Application() application = app.Application()
prelim_opts, remaining_args = application.parse_preliminary_options([]) prelim_opts, remaining_args = application.parse_preliminary_options([])
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
application.config_finder = application.make_config_finder( config_finder = application.make_config_finder(
application.program, prelim_opts.append_config application.program, prelim_opts.append_config
) )
application.find_plugins(prelim_opts.config, prelim_opts.isolated) application.find_plugins(
config_finder, prelim_opts.config, prelim_opts.isolated
)
application.register_plugin_options() application.register_plugin_options()
application.parse_configuration_and_cli(remaining_args) application.parse_configuration_and_cli(config_finder, remaining_args)
# We basically want application.initialize to be called but with these # We basically want application.initialize to be called but with these
# options set instead before we make our formatter, notifier, internal # options set instead before we make our formatter, notifier, internal
# style guide and file checker manager. # style guide and file checker manager.

View file

@ -57,8 +57,6 @@ class Application(object):
parents=[self.prelim_arg_parser], parents=[self.prelim_arg_parser],
) )
options.register_default_options(self.option_manager) options.register_default_options(self.option_manager)
#: The instance of :class:`flake8.options.config.ConfigFileFinder`
self.config_finder = None # type: config.ConfigFileFinder
#: The :class:`flake8.options.config.LocalPlugins` found in config #: The :class:`flake8.options.config.LocalPlugins` found in config
self.local_plugins = None # type: config.LocalPlugins self.local_plugins = None # type: config.LocalPlugins
@ -150,8 +148,8 @@ class Application(object):
""" """
return config.ConfigFileFinder(program_name, extra_config_files) return config.ConfigFileFinder(program_name, extra_config_files)
def find_plugins(self, config_file, ignore_config_files): def find_plugins(self, config_finder, config_file, ignore_config_files):
# type: (Optional[str], bool) -> None # type: (config.ConfigFileFinder, Optional[str], bool) -> None
"""Find and load the plugins for this application. """Find and load the plugins for this application.
If :attr:`check_plugins`, or :attr:`formatting_plugins` are ``None`` If :attr:`check_plugins`, or :attr:`formatting_plugins` are ``None``
@ -160,6 +158,8 @@ class Application(object):
we want this to be idempotent and so only update those attributes if we want this to be idempotent and so only update those attributes if
they are ``None``. they are ``None``.
:param config.ConfigFileFinder config_finder:
The finder for finding and reading configuration files.
:param str config_file: :param str config_file:
The optional configuraiton file to override all other configuration The optional configuraiton file to override all other configuration
files (i.e., the --config option). files (i.e., the --config option).
@ -168,7 +168,7 @@ class Application(object):
--isolated option). --isolated option).
""" """
self.local_plugins = config.get_local_plugins( self.local_plugins = config.get_local_plugins(
self.config_finder, config_file, ignore_config_files config_finder, config_file, ignore_config_files
) )
sys.path.extend(self.local_plugins.paths) sys.path.extend(self.local_plugins.paths)
@ -191,15 +191,17 @@ class Application(object):
self.check_plugins.register_plugin_versions(self.option_manager) self.check_plugins.register_plugin_versions(self.option_manager)
self.formatting_plugins.register_options(self.option_manager) self.formatting_plugins.register_options(self.option_manager)
def parse_configuration_and_cli(self, argv): def parse_configuration_and_cli(self, config_finder, argv):
# type: (List[str]) -> None # type: (config.ConfigFileFinder, List[str]) -> None
"""Parse configuration files and the CLI options. """Parse configuration files and the CLI options.
:param config.ConfigFileFinder config_finder:
The finder for finding and reading configuration files.
:param list argv: :param list argv:
Command-line arguments passed in directly. Command-line arguments passed in directly.
""" """
self.options, self.args = aggregator.aggregate_options( self.options, self.args = aggregator.aggregate_options(
self.option_manager, self.config_finder, argv self.option_manager, config_finder, argv
) )
self.running_against_diff = self.options.diff self.running_against_diff = self.options.diff
@ -338,12 +340,14 @@ class Application(object):
# our legacy API calls to these same methods. # our legacy API calls to these same methods.
prelim_opts, remaining_args = self.parse_preliminary_options(argv) prelim_opts, remaining_args = self.parse_preliminary_options(argv)
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
self.config_finder = self.make_config_finder( config_finder = self.make_config_finder(
self.program, prelim_opts.append_config self.program, prelim_opts.append_config
) )
self.find_plugins(prelim_opts.config, prelim_opts.isolated) self.find_plugins(
config_finder, prelim_opts.config, prelim_opts.isolated
)
self.register_plugin_options() self.register_plugin_options()
self.parse_configuration_and_cli(remaining_args) self.parse_configuration_and_cli(config_finder, remaining_args)
self.make_formatter() self.make_formatter()
self.make_guide() self.make_guide()
self.make_file_checker_manager() self.make_file_checker_manager()

View file

@ -6,6 +6,7 @@ import pytest
from flake8.api import legacy as api from flake8.api import legacy as api
from flake8.formatting import base as formatter from flake8.formatting import base as formatter
from flake8.options.config import ConfigFileFinder
def test_get_style_guide(): def test_get_style_guide():
@ -20,6 +21,8 @@ def test_get_style_guide():
mockedapp = mock.Mock() mockedapp = mock.Mock()
mockedapp.parse_preliminary_options.return_value = (prelim_opts, []) mockedapp.parse_preliminary_options.return_value = (prelim_opts, [])
mockedapp.program = 'flake8' mockedapp.program = 'flake8'
config_finder = ConfigFileFinder(mockedapp.program, [])
mockedapp.make_config_finder.return_value = config_finder
with mock.patch('flake8.main.application.Application') as application: with mock.patch('flake8.main.application.Application') as application:
application.return_value = mockedapp application.return_value = mockedapp
style_guide = api.get_style_guide() style_guide = api.get_style_guide()
@ -27,9 +30,10 @@ def test_get_style_guide():
application.assert_called_once_with() application.assert_called_once_with()
mockedapp.parse_preliminary_options.assert_called_once_with([]) mockedapp.parse_preliminary_options.assert_called_once_with([])
mockedapp.make_config_finder.assert_called_once_with(mockedapp.program, []) mockedapp.make_config_finder.assert_called_once_with(mockedapp.program, [])
mockedapp.find_plugins.assert_called_once_with(None, False) mockedapp.find_plugins.assert_called_once_with(config_finder, None, False)
mockedapp.register_plugin_options.assert_called_once_with() mockedapp.register_plugin_options.assert_called_once_with()
mockedapp.parse_configuration_and_cli.assert_called_once_with([]) mockedapp.parse_configuration_and_cli.assert_called_once_with(
config_finder, [])
mockedapp.make_formatter.assert_called_once_with() mockedapp.make_formatter.assert_called_once_with()
mockedapp.make_guide.assert_called_once_with() mockedapp.make_guide.assert_called_once_with()
mockedapp.make_file_checker_manager.assert_called_once_with() mockedapp.make_file_checker_manager.assert_called_once_with()