From 8be5a7294bcdc40248e483b8a1ca734471179eda Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Mon, 18 May 2020 16:30:05 -0400 Subject: [PATCH 1/2] config: Normalize paths in config relative to provided parent directory This sets things up to support normalizing paths relative to parent directories specified by callers who have more context for determining what paths should be relative to. --- src/flake8/options/config.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index 5884873..c258fec 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -184,9 +184,12 @@ class MergedConfigParser(object): #: Our instance of our :class:`~ConfigFileFinder` self.config_finder = config_finder - def _normalize_value(self, option, value): + def _normalize_value(self, option, value, parent=None): + if parent is None: + parent = self.config_finder.local_directory + final_value = option.normalize( - value, self.config_finder.local_directory + value, parent ) LOG.debug( '%r has been normalized to %r for option "%s"', @@ -196,7 +199,7 @@ class MergedConfigParser(object): ) return final_value - def _parse_config(self, config_parser): + def _parse_config(self, config_parser, parent=None): config_dict = {} for option_name in config_parser.options(self.program_name): if option_name not in self.config_options: @@ -216,7 +219,7 @@ class MergedConfigParser(object): value = method(self.program_name, option_name) LOG.debug('Option "%s" returned value: %r', option_name, value) - final_value = self._normalize_value(option, value) + final_value = self._normalize_value(option, value, parent) config_dict[option.config_name] = final_value return config_dict From 563220b711a294a90472c49a795bc5eb0dbce93a Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Mon, 18 May 2020 17:12:07 -0400 Subject: [PATCH 2/2] config: Normalize paths in CLI-specified config relative to config dir Paths specified in configuration files should be relative to the directory where the configuration file resides. Formerly, paths were normalized relative to the current working directory where `flake8` was invoked. The former behavior was not expected, especially for directory structures with subprojects each having their own configuration. --- src/flake8/options/config.py | 2 +- tests/unit/test_merged_config_parser.py | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index c258fec..6c66d89 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -265,7 +265,7 @@ class MergedConfigParser(object): return {} LOG.debug("Parsing CLI configuration files.") - return self._parse_config(config) + return self._parse_config(config, os.path.dirname(config_path)) def merge_user_and_local_config(self): """Merge the parsed user and local configuration files. diff --git a/tests/unit/test_merged_config_parser.py b/tests/unit/test_merged_config_parser.py index 41885e4..d446ad8 100644 --- a/tests/unit/test_merged_config_parser.py +++ b/tests/unit/test_merged_config_parser.py @@ -31,15 +31,16 @@ def test_parse_cli_config(optmanager, config_finder): action='count') parser = config.MergedConfigParser(optmanager, config_finder) - parsed_config = parser.parse_cli_config( - 'tests/fixtures/config_files/cli-specified.ini' - ) + config_file = 'tests/fixtures/config_files/cli-specified.ini' + parsed_config = parser.parse_cli_config(config_file) + + config_dir = os.path.dirname(config_file) assert parsed_config == { 'ignore': ['E123', 'W234', 'E111'], 'exclude': [ - os.path.abspath('foo/'), - os.path.abspath('bar/'), - os.path.abspath('bogus/'), + os.path.abspath(os.path.join(config_dir, 'foo/')), + os.path.abspath(os.path.join(config_dir, 'bar/')), + os.path.abspath(os.path.join(config_dir, 'bogus/')), ], 'quiet': 1, }