Add broken config file to test error handling

ConfigFileFinder should absolutely handle broken/invalid config files
by refusing to try to parse them. Here we catch the ParsingError,
log the exception, and then return normally. The RawConfigParser
instance is perfectly valid still and will behave as if nothing had
been read and we just need to indicate that we didn't find any files
worthy of reading.

Related to: https://github.com/PyCQA/pycodestyle/issues/506
This commit is contained in:
Ian Cordasco 2016-06-16 16:19:09 -05:00
parent 9a9bcdfb52
commit 3f434f7d1c
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A
3 changed files with 26 additions and 1 deletions

View file

@ -52,7 +52,13 @@ class ConfigFileFinder(object):
@staticmethod
def _read_config(files):
config = configparser.RawConfigParser()
found_files = config.read(files)
try:
found_files = config.read(files)
except configparser.ParsingError:
LOG.exception("There was an error trying to parse a config "
"file. The files we were attempting to parse "
"were: %r", files)
found_files = []
return (config, found_files)
def cli_config(self, files):

View file

@ -0,0 +1,9 @@
[flake8]
exclude =
<<<<<<< 642f88cb1b6027e184d9a662b255f7fea4d9eacc
tests/fixtures/,
=======
tests/,
>>>>>>> HEAD
docs/
ignore = D203

View file

@ -10,6 +10,7 @@ import mock
import pytest
CLI_SPECIFIED_FILEPATH = 'tests/fixtures/config_files/cli-specified.ini'
BROKEN_CONFIG_PATH = 'tests/fixtures/config_files/broken.ini'
def test_uses_default_args():
@ -115,3 +116,12 @@ def test_local_configs():
finder = config.ConfigFileFinder('flake8', None, [])
assert isinstance(finder.local_configs(), configparser.RawConfigParser)
@pytest.mark.parametrize('files', [
[BROKEN_CONFIG_PATH],
[CLI_SPECIFIED_FILEPATH, BROKEN_CONFIG_PATH],
])
def test_read_config_catches_broken_config_files(files):
"""Verify that we do not allow the exception to bubble up."""
assert config.ConfigFileFinder._read_config(files)[1] == []