mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-06 13:06:53 +00:00
ConfigFileFinder should absolutely handle broken/invalid config files by refusing to try to parse them. Here we catch the ParsingError, log the exception, and then return normally. The RawConfigParser instance is perfectly valid still and will behave as if nothing had been read and we just need to indicate that we didn't find any files worthy of reading. Related to: https://github.com/PyCQA/pycodestyle/issues/506
127 lines
4.3 KiB
Python
127 lines
4.3 KiB
Python
"""Tests for the ConfigFileFinder."""
|
|
import configparser
|
|
import os
|
|
import sys
|
|
|
|
from flake8.options import config
|
|
|
|
import mock
|
|
|
|
import pytest
|
|
|
|
CLI_SPECIFIED_FILEPATH = 'tests/fixtures/config_files/cli-specified.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', None, [])
|
|
assert finder.parent == os.path.abspath('.')
|
|
|
|
|
|
@pytest.mark.parametrize('platform,is_windows', [
|
|
('win32', True),
|
|
('linux', False),
|
|
('darwin', False),
|
|
])
|
|
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, [])
|
|
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, [])
|
|
|
|
parsed_config = finder.cli_config(cli_filepath)
|
|
assert parsed_config.has_section('flake8')
|
|
|
|
|
|
@pytest.mark.parametrize('args,expected', [
|
|
# No arguments, common prefix of abspath('.')
|
|
([],
|
|
[os.path.abspath('setup.cfg'),
|
|
os.path.abspath('tox.ini'),
|
|
os.path.abspath('.flake8')]),
|
|
# Common prefix of "flake8/"
|
|
(['flake8/options', 'flake8/'],
|
|
[os.path.abspath('flake8/setup.cfg'),
|
|
os.path.abspath('flake8/tox.ini'),
|
|
os.path.abspath('flake8/.flake8'),
|
|
os.path.abspath('setup.cfg'),
|
|
os.path.abspath('tox.ini'),
|
|
os.path.abspath('.flake8')]),
|
|
# Common prefix of "flake8/options"
|
|
(['flake8/options', 'flake8/options/sub'],
|
|
[os.path.abspath('flake8/options/setup.cfg'),
|
|
os.path.abspath('flake8/options/tox.ini'),
|
|
os.path.abspath('flake8/options/.flake8'),
|
|
os.path.abspath('flake8/setup.cfg'),
|
|
os.path.abspath('flake8/tox.ini'),
|
|
os.path.abspath('flake8/.flake8'),
|
|
os.path.abspath('setup.cfg'),
|
|
os.path.abspath('tox.ini'),
|
|
os.path.abspath('.flake8')]),
|
|
])
|
|
def test_generate_possible_local_files(args, expected):
|
|
"""Verify generation of all possible config paths."""
|
|
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):
|
|
"""Verify discovery of local config files."""
|
|
finder = config.ConfigFileFinder('flake8', args, extra_config_files)
|
|
|
|
assert list(finder.local_config_files()) == expected
|
|
|
|
|
|
def test_local_configs():
|
|
"""Verify we return a ConfigParser."""
|
|
finder = config.ConfigFileFinder('flake8', None, [])
|
|
|
|
assert isinstance(finder.local_configs(), configparser.RawConfigParser)
|
|
|
|
|
|
@pytest.mark.parametrize('files', [
|
|
[BROKEN_CONFIG_PATH],
|
|
[CLI_SPECIFIED_FILEPATH, BROKEN_CONFIG_PATH],
|
|
])
|
|
def test_read_config_catches_broken_config_files(files):
|
|
"""Verify that we do not allow the exception to bubble up."""
|
|
assert config.ConfigFileFinder._read_config(files)[1] == []
|