summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2017-08-03 00:25:37 -0700
committerCarl Meyer <carl@oddbird.net>2017-08-03 00:25:37 -0700
commit4e58068657ece52e3f1636cb028e42c15b86fec3 (patch)
treec739fe9428a1a3f46d836773e243cdcdb22ddb27 /tests/unit
parent6df26ffd57178e50194aea31b3bf9c572f91fa54 (diff)
downloadflake8-4e58068657ece52e3f1636cb028e42c15b86fec3.tar.gz
Add support for local (in-repo, non-setuptools) plugins.
Closes #357
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/test_config_file_finder.py24
-rw-r--r--tests/unit/test_get_local_plugins.py38
-rw-r--r--tests/unit/test_legacy_api.py4
-rw-r--r--tests/unit/test_merged_config_parser.py81
-rw-r--r--tests/unit/test_plugin_manager.py12
-rw-r--r--tests/unit/test_plugin_type_manager.py2
6 files changed, 109 insertions, 52 deletions
diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py
index 3f321da..2a9f903 100644
--- a/tests/unit/test_config_file_finder.py
+++ b/tests/unit/test_config_file_finder.py
@@ -39,6 +39,18 @@ def test_cli_config():
assert parsed_config.has_section('flake8')
+def test_cli_config_double_read():
+ """Second request for CLI config is cached."""
+ finder = config.ConfigFileFinder('flake8', None, [])
+
+ parsed_config = finder.cli_config(CLI_SPECIFIED_FILEPATH)
+ boom = Exception("second request for CLI config not cached")
+ with mock.patch.object(finder, '_read_config', side_effect=boom):
+ parsed_config_2 = finder.cli_config(CLI_SPECIFIED_FILEPATH)
+
+ assert parsed_config is parsed_config_2
+
+
@pytest.mark.parametrize('args,expected', [
# No arguments, common prefix of abspath('.')
([],
@@ -105,6 +117,18 @@ def test_local_configs():
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, [])
+
+ first_read = finder.local_configs()
+ boom = Exception("second request for local configs not cached")
+ with mock.patch.object(finder, '_read_config', side_effect=boom):
+ second_read = finder.local_configs()
+
+ assert first_read is second_read
+
+
@pytest.mark.parametrize('files', [
[BROKEN_CONFIG_PATH],
[CLI_SPECIFIED_FILEPATH, BROKEN_CONFIG_PATH],
diff --git a/tests/unit/test_get_local_plugins.py b/tests/unit/test_get_local_plugins.py
new file mode 100644
index 0000000..942599a
--- /dev/null
+++ b/tests/unit/test_get_local_plugins.py
@@ -0,0 +1,38 @@
+"""Tests for get_local_plugins."""
+import mock
+
+from flake8.options import config
+
+
+def test_get_local_plugins_respects_isolated():
+ """Verify behaviour of get_local_plugins with isolated=True."""
+ config_finder = mock.MagicMock()
+
+ local_plugins = config.get_local_plugins(config_finder, isolated=True)
+
+ assert local_plugins.extension == []
+ assert local_plugins.report == []
+ assert config_finder.local_configs.called is False
+ assert config_finder.user_config.called is False
+
+
+def test_get_local_plugins_uses_cli_config():
+ """Verify behaviour of get_local_plugins with a specified config."""
+ config_finder = mock.MagicMock()
+
+ config.get_local_plugins(config_finder, cli_config='foo.ini')
+
+ config_finder.cli_config.assert_called_once_with('foo.ini')
+
+
+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', [], [])
+
+ with mock.patch.object(config_finder, 'local_config_files') as localcfs:
+ localcfs.return_value = [config_fixture_path]
+ local_plugins = config.get_local_plugins(config_finder)
+
+ assert local_plugins.extension == ['XE = test_plugins:ExtensionTestPlugin']
+ assert local_plugins.report == ['XR = test_plugins:ReportTestPlugin']
diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py
index 0f1d1f3..f456bae 100644
--- a/tests/unit/test_legacy_api.py
+++ b/tests/unit/test_legacy_api.py
@@ -9,11 +9,15 @@ from flake8.formatting import base as formatter
def test_get_style_guide():
"""Verify the methods called on our internal Application."""
mockedapp = mock.Mock()
+ mockedapp.prelim_opts.verbose = 0
+ mockedapp.prelim_opts.output_file = None
with mock.patch('flake8.main.application.Application') as Application:
Application.return_value = mockedapp
style_guide = api.get_style_guide()
Application.assert_called_once_with()
+ mockedapp.parse_preliminary_options_and_args.assert_called_once_with([])
+ mockedapp.make_config_finder.assert_called_once_with()
mockedapp.find_plugins.assert_called_once_with()
mockedapp.register_plugin_options.assert_called_once_with()
mockedapp.parse_configuration_and_cli.assert_called_once_with([])
diff --git a/tests/unit/test_merged_config_parser.py b/tests/unit/test_merged_config_parser.py
index 1bc2bbe..37d5e6f 100644
--- a/tests/unit/test_merged_config_parser.py
+++ b/tests/unit/test_merged_config_parser.py
@@ -14,33 +14,13 @@ def optmanager():
return manager.OptionManager(prog='flake8', version='3.0.0a1')
-@pytest.mark.parametrize('args,extra_config_files', [
- (None, None),
- (None, []),
- (None, ['foo.ini']),
- ('flake8/', []),
- ('flake8/', ['foo.ini']),
-])
-def test_creates_its_own_config_file_finder(args, extra_config_files,
- optmanager):
- """Verify we create a ConfigFileFinder correctly."""
- class_path = 'flake8.options.config.ConfigFileFinder'
- with mock.patch(class_path) as ConfigFileFinder:
- parser = config.MergedConfigParser(
- option_manager=optmanager,
- extra_config_files=extra_config_files,
- args=args,
- )
-
- assert parser.program_name == 'flake8'
- ConfigFileFinder.assert_called_once_with(
- 'flake8',
- args,
- extra_config_files or [],
- )
+@pytest.fixture
+def config_finder():
+ """Generate a simple ConfigFileFinder."""
+ return config.ConfigFileFinder('flake8', [], [])
-def test_parse_cli_config(optmanager):
+def test_parse_cli_config(optmanager, config_finder):
"""Parse the specified config file as a cli config file."""
optmanager.add_option('--exclude', parse_from_config=True,
comma_separated_list=True,
@@ -51,7 +31,7 @@ def test_parse_cli_config(optmanager):
action='count')
optmanager.add_option('--quiet', parse_from_config=True,
action='count')
- parser = config.MergedConfigParser(optmanager)
+ parser = config.MergedConfigParser(optmanager, config_finder)
parsed_config = parser.parse_cli_config(
'tests/fixtures/config_files/cli-specified.ini'
@@ -72,15 +52,16 @@ def test_parse_cli_config(optmanager):
('tests/fixtures/config_files/cli-specified.ini', True),
('tests/fixtures/config_files/no-flake8-section.ini', False),
])
-def test_is_configured_by(filename, is_configured_by, optmanager):
+def test_is_configured_by(
+ filename, is_configured_by, optmanager, config_finder):
"""Verify the behaviour of the is_configured_by method."""
parsed_config, _ = config.ConfigFileFinder._read_config(filename)
- parser = config.MergedConfigParser(optmanager)
+ parser = config.MergedConfigParser(optmanager, config_finder)
assert parser.is_configured_by(parsed_config) is is_configured_by
-def test_parse_user_config(optmanager):
+def test_parse_user_config(optmanager, config_finder):
"""Verify parsing of user config files."""
optmanager.add_option('--exclude', parse_from_config=True,
comma_separated_list=True,
@@ -91,7 +72,7 @@ def test_parse_user_config(optmanager):
action='count')
optmanager.add_option('--quiet', parse_from_config=True,
action='count')
- parser = config.MergedConfigParser(optmanager)
+ parser = config.MergedConfigParser(optmanager, config_finder)
with mock.patch.object(parser.config_finder, 'user_config_file') as usercf:
usercf.return_value = 'tests/fixtures/config_files/cli-specified.ini'
@@ -109,7 +90,7 @@ def test_parse_user_config(optmanager):
}
-def test_parse_local_config(optmanager):
+def test_parse_local_config(optmanager, config_finder):
"""Verify parsing of local config files."""
optmanager.add_option('--exclude', parse_from_config=True,
comma_separated_list=True,
@@ -120,8 +101,7 @@ def test_parse_local_config(optmanager):
action='count')
optmanager.add_option('--quiet', parse_from_config=True,
action='count')
- parser = config.MergedConfigParser(optmanager)
- config_finder = parser.config_finder
+ parser = config.MergedConfigParser(optmanager, config_finder)
with mock.patch.object(config_finder, 'local_config_files') as localcfs:
localcfs.return_value = [
@@ -141,7 +121,7 @@ def test_parse_local_config(optmanager):
}
-def test_merge_user_and_local_config(optmanager):
+def test_merge_user_and_local_config(optmanager, config_finder):
"""Verify merging of parsed user and local config files."""
optmanager.add_option('--exclude', parse_from_config=True,
comma_separated_list=True,
@@ -150,8 +130,7 @@ def test_merge_user_and_local_config(optmanager):
comma_separated_list=True)
optmanager.add_option('--select', parse_from_config=True,
comma_separated_list=True)
- parser = config.MergedConfigParser(optmanager)
- config_finder = parser.config_finder
+ parser = config.MergedConfigParser(optmanager, config_finder)
with mock.patch.object(config_finder, 'local_config_files') as localcfs:
localcfs.return_value = [
@@ -172,23 +151,23 @@ def test_merge_user_and_local_config(optmanager):
}
-@mock.patch('flake8.options.config.ConfigFileFinder')
-def test_parse_isolates_config(ConfigFileManager, optmanager):
+def test_parse_isolates_config(optmanager):
"""Verify behaviour of the parse method with isolated=True."""
- parser = config.MergedConfigParser(optmanager)
+ config_finder = mock.MagicMock()
+ parser = config.MergedConfigParser(optmanager, config_finder)
assert parser.parse(isolated=True) == {}
- assert parser.config_finder.local_configs.called is False
- assert parser.config_finder.user_config.called is False
+ assert config_finder.local_configs.called is False
+ assert config_finder.user_config.called is False
-@mock.patch('flake8.options.config.ConfigFileFinder')
-def test_parse_uses_cli_config(ConfigFileManager, optmanager):
+def test_parse_uses_cli_config(optmanager):
"""Verify behaviour of the parse method with a specified config."""
- parser = config.MergedConfigParser(optmanager)
+ config_finder = mock.MagicMock()
+ parser = config.MergedConfigParser(optmanager, config_finder)
parser.parse(cli_config='foo.ini')
- parser.config_finder.cli_config.assert_called_once_with('foo.ini')
+ config_finder.cli_config.assert_called_once_with('foo.ini')
@pytest.mark.parametrize('config_fixture_path', [
@@ -196,7 +175,8 @@ def test_parse_uses_cli_config(ConfigFileManager, optmanager):
'tests/fixtures/config_files/cli-specified-with-inline-comments.ini',
'tests/fixtures/config_files/cli-specified-without-inline-comments.ini',
])
-def test_parsed_configs_are_equivalent(optmanager, config_fixture_path):
+def test_parsed_configs_are_equivalent(
+ optmanager, config_finder, config_fixture_path):
"""Verify the each file matches the expected parsed output.
This is used to ensure our documented behaviour does not regress.
@@ -206,8 +186,7 @@ def test_parsed_configs_are_equivalent(optmanager, config_fixture_path):
normalize_paths=True)
optmanager.add_option('--ignore', parse_from_config=True,
comma_separated_list=True)
- parser = config.MergedConfigParser(optmanager)
- config_finder = parser.config_finder
+ parser = config.MergedConfigParser(optmanager, config_finder)
with mock.patch.object(config_finder, 'local_config_files') as localcfs:
localcfs.return_value = [config_fixture_path]
@@ -227,7 +206,8 @@ def test_parsed_configs_are_equivalent(optmanager, config_fixture_path):
@pytest.mark.parametrize('config_file', [
'tests/fixtures/config_files/config-with-hyphenated-options.ini'
])
-def test_parsed_hyphenated_and_underscored_names(optmanager, config_file):
+def test_parsed_hyphenated_and_underscored_names(
+ optmanager, config_finder, config_file):
"""Verify we find hyphenated option names as well as underscored.
This tests for options like --max-line-length and --enable-extensions
@@ -238,8 +218,7 @@ def test_parsed_hyphenated_and_underscored_names(optmanager, config_file):
type='int')
optmanager.add_option('--enable-extensions', parse_from_config=True,
comma_separated_list=True)
- parser = config.MergedConfigParser(optmanager)
- config_finder = parser.config_finder
+ parser = config.MergedConfigParser(optmanager, config_finder)
with mock.patch.object(config_finder, 'local_config_files') as localcfs:
localcfs.return_value = [config_file]
diff --git a/tests/unit/test_plugin_manager.py b/tests/unit/test_plugin_manager.py
index 8991b96..9019ead 100644
--- a/tests/unit/test_plugin_manager.py
+++ b/tests/unit/test_plugin_manager.py
@@ -48,3 +48,15 @@ def test_handles_mapping_functions_across_plugins(iter_entry_points):
plugins = [plugin_mgr.plugins[name] for name in plugin_mgr.names]
assert list(plugin_mgr.map(lambda x: x)) == plugins
+
+
+@mock.patch('pkg_resources.iter_entry_points')
+def test_local_plugins(iter_entry_points):
+ """Verify PluginManager can load given local plugins."""
+ iter_entry_points.return_value = []
+ plugin_mgr = manager.PluginManager(
+ namespace='testing.pkg_resources',
+ local_plugins=['X = path.to:Plugin']
+ )
+
+ assert plugin_mgr.plugins['X'].entry_point.module_name == 'path.to'
diff --git a/tests/unit/test_plugin_type_manager.py b/tests/unit/test_plugin_type_manager.py
index 4e69cee..b81a1ae 100644
--- a/tests/unit/test_plugin_type_manager.py
+++ b/tests/unit/test_plugin_type_manager.py
@@ -53,7 +53,7 @@ def test_instantiates_a_manager(PluginManager):
"""Verify we create a PluginManager on instantiation."""
FakeTestType()
- PluginManager.assert_called_once_with(TEST_NAMESPACE)
+ PluginManager.assert_called_once_with(TEST_NAMESPACE, local_plugins=None)
@mock.patch('flake8.plugins.manager.PluginManager')