File: test_plugin_manager.py

package info (click to toggle)
python-flake8 3.2.1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,076 kB
  • sloc: python: 5,037; sh: 20; makefile: 18
file content (50 lines) | stat: -rw-r--r-- 1,842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Tests for flake8.plugins.manager.PluginManager."""
import mock

from flake8.plugins import manager


def create_entry_point_mock(name):
    """Create a mocked EntryPoint."""
    ep = mock.Mock(spec=['name'])
    ep.name = name
    return ep


@mock.patch('pkg_resources.iter_entry_points')
def test_calls_pkg_resources_on_instantiation(iter_entry_points):
    """Verify that we call iter_entry_points when we create a manager."""
    iter_entry_points.return_value = []
    manager.PluginManager(namespace='testing.pkg_resources')

    iter_entry_points.assert_called_once_with('testing.pkg_resources')


@mock.patch('pkg_resources.iter_entry_points')
def test_calls_pkg_resources_creates_plugins_automaticaly(iter_entry_points):
    """Verify that we create Plugins on instantiation."""
    iter_entry_points.return_value = [
        create_entry_point_mock('T100'),
        create_entry_point_mock('T200'),
    ]
    plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')

    iter_entry_points.assert_called_once_with('testing.pkg_resources')
    assert 'T100' in plugin_mgr.plugins
    assert 'T200' in plugin_mgr.plugins
    assert isinstance(plugin_mgr.plugins['T100'], manager.Plugin)
    assert isinstance(plugin_mgr.plugins['T200'], manager.Plugin)


@mock.patch('pkg_resources.iter_entry_points')
def test_handles_mapping_functions_across_plugins(iter_entry_points):
    """Verify we can use the PluginManager call functions on all plugins."""
    entry_point_mocks = [
        create_entry_point_mock('T100'),
        create_entry_point_mock('T200'),
    ]
    iter_entry_points.return_value = entry_point_mocks
    plugin_mgr = manager.PluginManager(namespace='testing.pkg_resources')
    plugins = [plugin_mgr.plugins[name] for name in plugin_mgr.names]

    assert list(plugin_mgr.map(lambda x: x)) == plugins