Merge branch 'opt-config-relative-to' into 'master'

Normalize paths in CLI-specified config relative to config file's directory

See merge request pycqa/flake8!431
This commit is contained in:
Eric N. Vander Weele 2020-05-18 21:23:20 +00:00
commit 4b49c1d1d3
2 changed files with 15 additions and 11 deletions

View file

@ -184,9 +184,12 @@ class MergedConfigParser(object):
#: Our instance of our :class:`~ConfigFileFinder` #: Our instance of our :class:`~ConfigFileFinder`
self.config_finder = config_finder 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( final_value = option.normalize(
value, self.config_finder.local_directory value, parent
) )
LOG.debug( LOG.debug(
'%r has been normalized to %r for option "%s"', '%r has been normalized to %r for option "%s"',
@ -196,7 +199,7 @@ class MergedConfigParser(object):
) )
return final_value return final_value
def _parse_config(self, config_parser): def _parse_config(self, config_parser, parent=None):
config_dict = {} config_dict = {}
for option_name in config_parser.options(self.program_name): for option_name in config_parser.options(self.program_name):
if option_name not in self.config_options: if option_name not in self.config_options:
@ -216,7 +219,7 @@ class MergedConfigParser(object):
value = method(self.program_name, option_name) value = method(self.program_name, option_name)
LOG.debug('Option "%s" returned value: %r', option_name, value) 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 config_dict[option.config_name] = final_value
return config_dict return config_dict
@ -262,7 +265,7 @@ class MergedConfigParser(object):
return {} return {}
LOG.debug("Parsing CLI configuration files.") 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): def merge_user_and_local_config(self):
"""Merge the parsed user and local configuration files. """Merge the parsed user and local configuration files.

View file

@ -31,15 +31,16 @@ def test_parse_cli_config(optmanager, config_finder):
action='count') action='count')
parser = config.MergedConfigParser(optmanager, config_finder) parser = config.MergedConfigParser(optmanager, config_finder)
parsed_config = parser.parse_cli_config( config_file = 'tests/fixtures/config_files/cli-specified.ini'
'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 == { assert parsed_config == {
'ignore': ['E123', 'W234', 'E111'], 'ignore': ['E123', 'W234', 'E111'],
'exclude': [ 'exclude': [
os.path.abspath('foo/'), os.path.abspath(os.path.join(config_dir, 'foo/')),
os.path.abspath('bar/'), os.path.abspath(os.path.join(config_dir, 'bar/')),
os.path.abspath('bogus/'), os.path.abspath(os.path.join(config_dir, 'bogus/')),
], ],
'quiet': 1, 'quiet': 1,
} }