mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-04 20:26:53 +00:00
Make formatting plugin logic easier to test
By splitting out the logic to retrieve and return the formatting class for an application, we can test it more easily and increase our test coverage of this critical logic. Refs #320
This commit is contained in:
parent
15ddc3aa2e
commit
c62de6acc3
2 changed files with 84 additions and 33 deletions
|
|
@ -185,6 +185,28 @@ class Application(object):
|
|||
self.options,
|
||||
self.args)
|
||||
|
||||
def formatter_for(self, formatter_plugin_name):
|
||||
"""Retrieve the formatter class by plugin name."""
|
||||
try:
|
||||
default_formatter = self.formatting_plugins['default']
|
||||
except KeyError:
|
||||
raise exceptions.ExecutionError(
|
||||
"The 'default' Flake8 formatting plugin is unavailable. "
|
||||
"This usually indicates that your setuptools is too old. "
|
||||
"Please upgrade setuptools. If that does not fix the issue"
|
||||
" please file an issue."
|
||||
)
|
||||
|
||||
formatter_plugin = self.formatting_plugins.get(formatter_plugin_name)
|
||||
if formatter_plugin is None:
|
||||
LOG.warning(
|
||||
'"%s" is an unknown formatter. Falling back to default.',
|
||||
formatter_plugin_name,
|
||||
)
|
||||
formatter_plugin = default_formatter
|
||||
|
||||
return formatter_plugin.execute
|
||||
|
||||
def make_formatter(self, formatter_class=None):
|
||||
# type: () -> NoneType
|
||||
"""Initialize a formatter based on the parsed options."""
|
||||
|
|
@ -195,20 +217,8 @@ class Application(object):
|
|||
elif 2 <= self.options.quiet:
|
||||
format_plugin = 'quiet-nothing'
|
||||
|
||||
try:
|
||||
default_formatter = self.formatting_plugins['default']
|
||||
except KeyError:
|
||||
raise exceptions.ExecutionError(
|
||||
"The 'default' Flake8 formatting plugin is unavailable. "
|
||||
"This usually indicates that your setuptools is too old. "
|
||||
"Please upgrade setuptools. If that does not fix the issue"
|
||||
" please file an issue."
|
||||
)
|
||||
|
||||
if formatter_class is None:
|
||||
formatter_class = self.formatting_plugins.get(
|
||||
format_plugin, default_formatter
|
||||
).execute
|
||||
formatter_class = self.formatter_for(format_plugin)
|
||||
|
||||
self.formatter = formatter_class(self.options)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue