mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-14 08:24:46 +00:00
Add a few tests to validate config parsing
This commit is contained in:
parent
4f3087b369
commit
06d3d25740
1 changed files with 69 additions and 0 deletions
|
|
@ -1,14 +1,19 @@
|
||||||
"""Test aggregation of config files and command-line options."""
|
"""Test aggregation of config files and command-line options."""
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from flake8 import defaults
|
||||||
from flake8.main import options
|
from flake8.main import options
|
||||||
from flake8.options import aggregator
|
from flake8.options import aggregator
|
||||||
from flake8.options import config
|
from flake8.options import config
|
||||||
from flake8.options import manager
|
from flake8.options import manager
|
||||||
|
|
||||||
CLI_SPECIFIED_CONFIG = 'tests/fixtures/config_files/cli-specified.ini'
|
CLI_SPECIFIED_CONFIG = 'tests/fixtures/config_files/cli-specified.ini'
|
||||||
|
USER_CONFIG = 'tests/fixtures/config_files/user-config.ini'
|
||||||
|
LOCAL_CONFIG = 'tests/fixtures/config_files/local-config.ini'
|
||||||
|
MISSING_CONFIG = 'tests/fixtures/config_files/missing.ini'
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
@ -22,6 +27,70 @@ def optmanager():
|
||||||
return option_manager
|
return option_manager
|
||||||
|
|
||||||
|
|
||||||
|
def mock_config_finder(program='flake8', arguments=[],
|
||||||
|
prepend_configs=[], append_configs=[],
|
||||||
|
user_config='', local_configs=[]):
|
||||||
|
"""Create a config finder with controlled access to files."""
|
||||||
|
config_finder = config.ConfigFileFinder(
|
||||||
|
program, arguments, prepend_configs, append_configs)
|
||||||
|
|
||||||
|
config_finder.user_config_file = mock.Mock(return_value=user_config)
|
||||||
|
config_finder.generate_possible_local_files = mock.Mock(
|
||||||
|
return_value=local_configs
|
||||||
|
)
|
||||||
|
|
||||||
|
return config_finder
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('config_finder_params,options_assertions', [
|
||||||
|
# Default values with no config file
|
||||||
|
({}, {
|
||||||
|
"select": {'E', 'W', 'F', 'C90'},
|
||||||
|
"ignore": set(defaults.IGNORE),
|
||||||
|
"max_line_length": 79,
|
||||||
|
}),
|
||||||
|
# Correct values with user config only
|
||||||
|
({'user_config': USER_CONFIG}, {
|
||||||
|
"select": {'E', 'W', 'F', 'C90'},
|
||||||
|
"ignore": ['D203'],
|
||||||
|
"max_line_length": 79,
|
||||||
|
}),
|
||||||
|
# Default values with missing user config file
|
||||||
|
({'user_config': MISSING_CONFIG}, {
|
||||||
|
"select": {'E', 'W', 'F', 'C90'},
|
||||||
|
"ignore": set(defaults.IGNORE),
|
||||||
|
"max_line_length": 79,
|
||||||
|
}),
|
||||||
|
# Correct values with local config only
|
||||||
|
({'local_configs': [LOCAL_CONFIG]}, {
|
||||||
|
"select": {'E', 'W', 'F'},
|
||||||
|
"ignore": set(defaults.IGNORE),
|
||||||
|
"max_line_length": 79,
|
||||||
|
}),
|
||||||
|
# Default values with missing local config file
|
||||||
|
({'local_configs': [MISSING_CONFIG]}, {
|
||||||
|
"select": {'E', 'W', 'F', 'C90'},
|
||||||
|
"ignore": set(defaults.IGNORE),
|
||||||
|
"max_line_length": 79,
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
def test_aggregate_options_resulting_values(
|
||||||
|
optmanager, config_finder_params, options_assertions
|
||||||
|
):
|
||||||
|
"""Verify we get correct values with various config files combinations."""
|
||||||
|
arguments = []
|
||||||
|
config_finder = mock_config_finder(**config_finder_params)
|
||||||
|
parsed_options, args = aggregator.aggregate_options(
|
||||||
|
optmanager, config_finder, arguments
|
||||||
|
)
|
||||||
|
|
||||||
|
for option, expected_value in options_assertions.items():
|
||||||
|
# Cast the option to the expected value type, especially for sets
|
||||||
|
expected_type = type(expected_value)
|
||||||
|
option_value = getattr(parsed_options, option)
|
||||||
|
assert expected_value == expected_type(option_value)
|
||||||
|
|
||||||
|
|
||||||
def test_aggregate_options_with_config(optmanager):
|
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',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue