Start testing ConfigFileFinder.local_config_files

Make sure extra_config_files are all absolute paths and filter them
in local_config_files to ensure they exist.
This commit is contained in:
Ian Cordasco 2016-01-10 09:39:37 -06:00
parent fdff0f705e
commit b8dfc9a8e2
2 changed files with 42 additions and 3 deletions

View file

@ -29,7 +29,11 @@ class ConfigFileFinder(object):
Extra configuration files specified by the user to read.
"""
# The values of --append-config from the CLI
self.extra_config_files = extra_config_files
extra_config_files = extra_config_files or []
self.extra_config_files = [
# Ensure the paths are absolute paths for local_config_files
os.path.abspath(f) for f in extra_config_files
]
# Platform specific settings
self.is_windows = sys.platform == 'win32'
@ -77,12 +81,23 @@ class ConfigFileFinder(object):
(parent, tail) = os.path.split(parent)
def local_config_files(self):
"""Find all local config files."""
"""Find all local config files which actually exist.
Filter results from
:meth:`~ConfigFileFinder.generate_possible_local_config_files` based
on whether the filename exists or not.
:returns:
List of files that exist that are local project config files with
extra config files appended to that list (which also exist).
:rtype:
[str]
"""
return [
filename
for filename in self.generate_possible_local_config_files()
if os.path.exists(filename)
] + self.extra_config_files
] + list(filter(os.path.exists, self.extra_config_files))
def local_configs(self):
"""Parse all local config files into one config object."""

View file

@ -67,3 +67,27 @@ def test_generate_possible_local_config_files(args, expected):
assert (list(finder.generate_possible_local_config_files()) ==
expected)
@pytest.mark.parametrize('args,extra_config_files,expected', [
# No arguments, common prefix of abspath('.')
([],
[],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini')]),
# Common prefix of "flake8/"
(['flake8/options', 'flake8/'],
[],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini')]),
# Common prefix of "flake8/options"
(['flake8/options', 'flake8/options/sub'],
[],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini')]),
])
def test_local_config_files(args, extra_config_files, expected):
"""Verify discovery of local config files."""
finder = config.ConfigFileFinder('flake8', args, extra_config_files)
assert list(finder.local_config_files()) == expected