Add minimal code to handle --prepend-config

Add code to handle config files specified with the --prepend-config
argument.

Options specified in prepend files get a higher precedence than flake8
defaults but a lower one than user configs. This allows to override
the defaults without affecting the behaviour of user and local config
files.

This implements a solution for issue #349
This commit is contained in:
Germain Chazot 2018-07-24 23:11:24 +01:00 committed by Germain Chazot
parent 4aa4eef0e7
commit 4f3087b369
6 changed files with 111 additions and 45 deletions

View file

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

View file

@ -15,7 +15,7 @@ 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', None, [])
finder = config.ConfigFileFinder('flake8', None, [], [])
assert finder.parent == os.path.abspath('.')
@ -27,14 +27,14 @@ def test_uses_default_args():
def test_windows_detection(platform, is_windows):
"""Verify we detect Windows to the best of our knowledge."""
with mock.patch.object(sys, 'platform', platform):
finder = config.ConfigFileFinder('flake8', None, [])
finder = config.ConfigFileFinder('flake8', None, [], [])
assert finder.is_windows is is_windows
def test_cli_config():
"""Verify opening and reading the file specified via the cli."""
cli_filepath = CLI_SPECIFIED_FILEPATH
finder = config.ConfigFileFinder('flake8', None, [])
finder = config.ConfigFileFinder('flake8', None, [], [])
parsed_config = finder.cli_config(cli_filepath)
assert parsed_config.has_section('flake8')
@ -42,7 +42,7 @@ def test_cli_config():
def test_cli_config_double_read():
"""Second request for CLI config is cached."""
finder = config.ConfigFileFinder('flake8', None, [])
finder = config.ConfigFileFinder('flake8', None, [], [])
parsed_config = finder.cli_config(CLI_SPECIFIED_FILEPATH)
boom = Exception("second request for CLI config not cached")
@ -68,59 +68,82 @@ def test_cli_config_double_read():
])
def test_generate_possible_local_files(args, expected):
"""Verify generation of all possible config paths."""
finder = config.ConfigFileFinder('flake8', args, [])
finder = config.ConfigFileFinder('flake8', args, [], [])
assert (list(finder.generate_possible_local_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')]),
# Common prefix of "flake8/" with extra config files specified
(['flake8/'],
[CLI_SPECIFIED_FILEPATH],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
# Common prefix of "flake8/" with missing extra config files specified
(['flake8/'],
[CLI_SPECIFIED_FILEPATH,
'tests/fixtures/config_files/missing.ini'],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
])
def test_local_config_files(args, extra_config_files, expected):
@pytest.mark.parametrize(
'args,prepend_config_files,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')]),
# Common prefix of "flake8/" with extra config files specified
(['flake8/'],
[],
[CLI_SPECIFIED_FILEPATH],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
# Common prefix of "flake8/" with missing extra config files specified
(['flake8/'],
[],
[CLI_SPECIFIED_FILEPATH,
'tests/fixtures/config_files/missing.ini'],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
# Common prefix of "flake8/" with prepend config files specified
(['flake8/'],
[CLI_SPECIFIED_FILEPATH],
[],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini')]),
# Common prefix of "flake8/" with prepend and extra config files
(['flake8/'],
[CLI_SPECIFIED_FILEPATH],
[CLI_SPECIFIED_FILEPATH],
[os.path.abspath('setup.cfg'),
os.path.abspath('tox.ini'),
os.path.abspath(CLI_SPECIFIED_FILEPATH)]),
])
def test_local_config_files(
args, prepend_config_files, extra_config_files, expected
):
"""Verify discovery of local config files."""
finder = config.ConfigFileFinder('flake8', args, extra_config_files)
finder = config.ConfigFileFinder(
'flake8', args, prepend_config_files, extra_config_files
)
assert list(finder.local_config_files()) == expected
def test_local_configs():
"""Verify we return a ConfigParser."""
finder = config.ConfigFileFinder('flake8', None, [])
finder = config.ConfigFileFinder('flake8', None, [], [])
assert isinstance(finder.local_configs(), configparser.RawConfigParser)
def test_local_configs_double_read():
"""Second request for local configs is cached."""
finder = config.ConfigFileFinder('flake8', None, [])
finder = config.ConfigFileFinder('flake8', None, [], [])
first_read = finder.local_configs()
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():
"""Verify get_local_plugins returns expected plugins."""
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:
localcfs.return_value = [config_fixture_path]

View file

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