diff --git a/flake8/options/config.py b/flake8/options/config.py index e82cb62..d4f64d8 100644 --- a/flake8/options/config.py +++ b/flake8/options/config.py @@ -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.""" diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py index e723397..4b73979 100644 --- a/tests/unit/test_config_file_finder.py +++ b/tests/unit/test_config_file_finder.py @@ -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