Merge branch 'config-search-relative-to-cwd' into 'master'

Simplify configuration file search to be relative to cwd

See merge request pycqa/flake8!363
This commit is contained in:
Anthony Sottile 2019-10-23 17:58:38 +00:00
commit e2c4b50a46
8 changed files with 37 additions and 66 deletions

View file

@ -32,7 +32,7 @@ def get_style_guide(**kwargs):
[] []
) )
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
application.make_config_finder(prelim_opts.append_config, prelim_args) application.make_config_finder(prelim_opts.append_config)
application.find_plugins(prelim_opts.config, prelim_opts.isolated) application.find_plugins(prelim_opts.config, prelim_opts.isolated)
application.register_plugin_options() application.register_plugin_options()
application.parse_configuration_and_cli([]) application.parse_configuration_and_cli([])

View file

@ -152,18 +152,16 @@ class Application(object):
(self.result_count > 0) or self.catastrophic_failure (self.result_count > 0) or self.catastrophic_failure
) )
def make_config_finder(self, append_config, args): def make_config_finder(self, append_config):
# type: (List[str], List[str]) -> None # type: (List[str]) -> None
"""Make our ConfigFileFinder based on preliminary opts and args. """Make our ConfigFileFinder based on preliminary opts and args.
:param list append_config: :param list append_config:
List of configuration files to be parsed for configuration. List of configuration files to be parsed for configuration.
:param list args:
The list of file arguments passed from the CLI.
""" """
if self.config_finder is None: if self.config_finder is None:
self.config_finder = config.ConfigFileFinder( self.config_finder = config.ConfigFileFinder(
self.option_manager.program_name, args, append_config self.option_manager.program_name, append_config
) )
def find_plugins(self, config_file, ignore_config_files): def find_plugins(self, config_file, ignore_config_files):
@ -363,7 +361,7 @@ class Application(object):
argv argv
) )
flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file)
self.make_config_finder(prelim_opts.append_config, prelim_args) self.make_config_finder(prelim_opts.append_config)
self.find_plugins(prelim_opts.config, prelim_opts.isolated) self.find_plugins(prelim_opts.config, prelim_opts.isolated)
self.register_plugin_options() self.register_plugin_options()
self.parse_configuration_and_cli(argv) self.parse_configuration_and_cli(argv)

View file

@ -16,14 +16,12 @@ __all__ = ("ConfigFileFinder", "MergedConfigParser")
class ConfigFileFinder(object): class ConfigFileFinder(object):
"""Encapsulate the logic for finding and reading config files.""" """Encapsulate the logic for finding and reading config files."""
def __init__(self, program_name, args, extra_config_files): def __init__(self, program_name, extra_config_files):
# type: (str, List[str], List[str]) -> None # type: (str, List[str]) -> None
"""Initialize object to find config files. """Initialize object to find config files.
:param str program_name: :param str program_name:
Name of the current program (e.g., flake8). Name of the current program (e.g., flake8).
:param list args:
The extra arguments passed on the command-line.
:param list extra_config_files: :param list extra_config_files:
Extra configuration files specified by the user to read. Extra configuration files specified by the user to read.
""" """
@ -46,9 +44,7 @@ class ConfigFileFinder(object):
self.local_directory = os.path.abspath(os.curdir) self.local_directory = os.path.abspath(os.curdir)
if not args: self.parent = self.tail = os.getcwd()
args = ["."]
self.parent = self.tail = os.path.abspath(os.path.commonprefix(args))
# caches to avoid double-reading config files # caches to avoid double-reading config files
self._local_configs = None self._local_configs = None

View file

@ -26,7 +26,7 @@ def test_aggregate_options_with_config(optmanager):
"""Verify we aggregate options and config values appropriately.""" """Verify we aggregate options and config values appropriately."""
arguments = ['flake8', '--config', CLI_SPECIFIED_CONFIG, '--select', arguments = ['flake8', '--config', CLI_SPECIFIED_CONFIG, '--select',
'E11,E34,E402,W,F', '--exclude', 'tests/*'] 'E11,E34,E402,W,F', '--exclude', 'tests/*']
config_finder = config.ConfigFileFinder('flake8', arguments, []) config_finder = config.ConfigFileFinder('flake8', [])
options, args = aggregator.aggregate_options( options, args = aggregator.aggregate_options(
optmanager, config_finder, arguments) optmanager, config_finder, arguments)
@ -40,7 +40,7 @@ def test_aggregate_options_when_isolated(optmanager):
"""Verify we aggregate options and config values appropriately.""" """Verify we aggregate options and config values appropriately."""
arguments = ['flake8', '--isolated', '--select', 'E11,E34,E402,W,F', arguments = ['flake8', '--isolated', '--select', 'E11,E34,E402,W,F',
'--exclude', 'tests/*'] '--exclude', 'tests/*']
config_finder = config.ConfigFileFinder('flake8', arguments, []) config_finder = config.ConfigFileFinder('flake8', [])
optmanager.extend_default_ignore(['E8']) optmanager.extend_default_ignore(['E8'])
options, args = aggregator.aggregate_options( options, args = aggregator.aggregate_options(
optmanager, config_finder, arguments) optmanager, config_finder, arguments)

View file

@ -13,12 +13,6 @@ CLI_SPECIFIED_FILEPATH = 'tests/fixtures/config_files/cli-specified.ini'
BROKEN_CONFIG_PATH = 'tests/fixtures/config_files/broken.ini' BROKEN_CONFIG_PATH = 'tests/fixtures/config_files/broken.ini'
def test_uses_default_args():
"""Show that we default the args value."""
finder = config.ConfigFileFinder('flake8', [], [])
assert finder.parent == os.path.abspath('.')
@pytest.mark.parametrize('platform,is_windows', [ @pytest.mark.parametrize('platform,is_windows', [
('win32', True), ('win32', True),
('linux', False), ('linux', False),
@ -27,14 +21,14 @@ def test_uses_default_args():
def test_windows_detection(platform, is_windows): def test_windows_detection(platform, is_windows):
"""Verify we detect Windows to the best of our knowledge.""" """Verify we detect Windows to the best of our knowledge."""
with mock.patch.object(sys, 'platform', platform): with mock.patch.object(sys, 'platform', platform):
finder = config.ConfigFileFinder('flake8', [], []) finder = config.ConfigFileFinder('flake8', [])
assert finder.is_windows is is_windows assert finder.is_windows is is_windows
def test_cli_config(): def test_cli_config():
"""Verify opening and reading the file specified via the cli.""" """Verify opening and reading the file specified via the cli."""
cli_filepath = CLI_SPECIFIED_FILEPATH cli_filepath = CLI_SPECIFIED_FILEPATH
finder = config.ConfigFileFinder('flake8', [], []) finder = config.ConfigFileFinder('flake8', [])
parsed_config = finder.cli_config(cli_filepath) parsed_config = finder.cli_config(cli_filepath)
assert parsed_config.has_section('flake8') assert parsed_config.has_section('flake8')
@ -42,7 +36,7 @@ def test_cli_config():
def test_cli_config_double_read(): def test_cli_config_double_read():
"""Second request for CLI config is cached.""" """Second request for CLI config is cached."""
finder = config.ConfigFileFinder('flake8', [], []) finder = config.ConfigFileFinder('flake8', [])
parsed_config = finder.cli_config(CLI_SPECIFIED_FILEPATH) parsed_config = finder.cli_config(CLI_SPECIFIED_FILEPATH)
boom = Exception("second request for CLI config not cached") boom = Exception("second request for CLI config not cached")
@ -52,75 +46,58 @@ def test_cli_config_double_read():
assert parsed_config is parsed_config_2 assert parsed_config is parsed_config_2
@pytest.mark.parametrize('args,expected', [ @pytest.mark.parametrize('cwd,expected', [
# No arguments, common prefix of abspath('.') # Root directory of project
([], ('.',
[os.path.abspath('setup.cfg'), [os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini')]), os.path.abspath('tox.ini')]),
# Common prefix of "flake8/" # Subdirectory of project directory
(['flake8/options', 'flake8/'], ('src',
[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('setup.cfg'),
os.path.abspath('tox.ini')]), os.path.abspath('tox.ini')]),
# Outside of project directory
('/',
[]),
]) ])
def test_generate_possible_local_files(args, expected): def test_generate_possible_local_files(cwd, expected):
"""Verify generation of all possible config paths.""" """Verify generation of all possible config paths."""
finder = config.ConfigFileFinder('flake8', args, []) with mock.patch.object(os, 'getcwd', return_value=cwd):
finder = config.ConfigFileFinder('flake8', [])
assert (list(finder.generate_possible_local_files()) assert (list(finder.generate_possible_local_files())
== expected) == expected)
@pytest.mark.parametrize('args,extra_config_files,expected', [ @pytest.mark.parametrize('extra_config_files,expected', [
# No arguments, common prefix of abspath('.') # Extra config files specified
([], ([CLI_SPECIFIED_FILEPATH],
[],
[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')]),
# Common prefix of "flake8/" with extra config files specified
(['flake8/'],
[CLI_SPECIFIED_FILEPATH],
[os.path.abspath('setup.cfg'), [os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'), os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]), os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
# Common prefix of "flake8/" with missing extra config files specified # Missing extra config files specified
(['flake8/'], ([CLI_SPECIFIED_FILEPATH,
[CLI_SPECIFIED_FILEPATH, 'tests/fixtures/config_files/missing.ini'],
'tests/fixtures/config_files/missing.ini'],
[os.path.abspath('setup.cfg'), [os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'), os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]), os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
]) ])
def test_local_config_files(args, extra_config_files, expected): def test_local_config_files(extra_config_files, expected):
"""Verify discovery of local config files.""" """Verify discovery of local config files."""
finder = config.ConfigFileFinder('flake8', args, extra_config_files) finder = config.ConfigFileFinder('flake8', extra_config_files)
assert list(finder.local_config_files()) == expected assert list(finder.local_config_files()) == expected
def test_local_configs(): def test_local_configs():
"""Verify we return a ConfigParser.""" """Verify we return a ConfigParser."""
finder = config.ConfigFileFinder('flake8', [], []) finder = config.ConfigFileFinder('flake8', [])
assert isinstance(finder.local_configs(), configparser.RawConfigParser) assert isinstance(finder.local_configs(), configparser.RawConfigParser)
def test_local_configs_double_read(): def test_local_configs_double_read():
"""Second request for local configs is cached.""" """Second request for local configs is cached."""
finder = config.ConfigFileFinder('flake8', [], []) finder = config.ConfigFileFinder('flake8', [])
first_read = finder.local_configs() first_read = finder.local_configs()
boom = Exception("second request for local configs not cached") boom = Exception("second request for local configs not cached")

View file

@ -31,7 +31,7 @@ def test_get_local_plugins_uses_cli_config():
def test_get_local_plugins(): def test_get_local_plugins():
"""Verify get_local_plugins returns expected plugins.""" """Verify get_local_plugins returns expected plugins."""
config_fixture_path = 'tests/fixtures/config_files/local-plugin.ini' config_fixture_path = 'tests/fixtures/config_files/local-plugin.ini'
config_finder = config.ConfigFileFinder('flake8', [], []) config_finder = config.ConfigFileFinder('flake8', [])
with mock.patch.object(config_finder, 'local_config_files') as localcfs: with mock.patch.object(config_finder, 'local_config_files') as localcfs:
localcfs.return_value = [config_fixture_path] localcfs.return_value = [config_fixture_path]

View file

@ -28,7 +28,7 @@ def test_get_style_guide():
application.assert_called_once_with() application.assert_called_once_with()
mockedapp.parse_preliminary_options_and_args.assert_called_once_with([]) mockedapp.parse_preliminary_options_and_args.assert_called_once_with([])
mockedapp.make_config_finder.assert_called_once_with([], []) mockedapp.make_config_finder.assert_called_once_with([])
mockedapp.find_plugins.assert_called_once_with(None, False) mockedapp.find_plugins.assert_called_once_with(None, False)
mockedapp.register_plugin_options.assert_called_once_with() mockedapp.register_plugin_options.assert_called_once_with()
mockedapp.parse_configuration_and_cli.assert_called_once_with([]) mockedapp.parse_configuration_and_cli.assert_called_once_with([])

View file

@ -17,7 +17,7 @@ def optmanager():
@pytest.fixture @pytest.fixture
def config_finder(): def config_finder():
"""Generate a simple ConfigFileFinder.""" """Generate a simple ConfigFileFinder."""
return config.ConfigFileFinder('flake8', [], []) return config.ConfigFileFinder('flake8', [])
def test_parse_cli_config(optmanager, config_finder): def test_parse_cli_config(optmanager, config_finder):