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:
Ian Cordasco 2017-05-13 08:00:43 -05:00
parent 15ddc3aa2e
commit c62de6acc3
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A
2 changed files with 84 additions and 33 deletions

View file

@ -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)