Remove unused and broken flake8.listen plugin type

This commit is contained in:
Anthony Sottile 2018-12-27 16:55:15 -08:00
parent 6ad56f73da
commit be88d26396
11 changed files with 19 additions and 525 deletions

View file

@ -1,54 +0,0 @@
"""Unit tests for the Notifier object."""
import pytest
from flake8.plugins import notifier
class _Listener(object):
def __init__(self, error_code):
self.error_code = error_code
self.was_notified = False
def notify(self, error_code, *args, **kwargs):
assert error_code.startswith(self.error_code)
self.was_notified = True
class TestNotifier(object):
"""Notifier unit tests."""
@pytest.fixture(autouse=True)
def setup(self):
"""Set up each TestNotifier instance."""
self.notifier = notifier.Notifier()
self.listener_map = {}
def add_listener(error_code):
listener = _Listener(error_code)
self.listener_map[error_code] = listener
self.notifier.register_listener(error_code, listener)
for i in range(10):
add_listener('E{0}'.format(i))
for j in range(30):
add_listener('E{0}{1:02d}'.format(i, j))
def test_notify(self):
"""Show that we notify a specific error code."""
self.notifier.notify('E111', 'extra', 'args')
assert self.listener_map['E111'].was_notified is True
assert self.listener_map['E1'].was_notified is True
@pytest.mark.parametrize('code', ['W123', 'W12', 'W1', 'W'])
def test_no_listeners_for(self, code):
"""Show that we return an empty list of listeners."""
assert list(self.notifier.listeners_for(code)) == []
@pytest.mark.parametrize('code,expected', [
('E101', ['E101', 'E1']),
('E211', ['E211', 'E2']),
])
def test_listeners_for(self, code, expected):
"""Verify that we retrieve the correct listeners."""
assert ([l.error_code
for l in self.notifier.listeners_for(code)] == expected)

View file

@ -203,31 +203,3 @@ def test_proxies_getitem_to_managers_plugins_dict(PluginManager): # noqa: N803
for i in range(8):
key = 'T10%i' % i
assert type_mgr[key] is plugins[key]
class FakePluginTypeManager(manager.NotifierBuilderMixin):
"""Provide an easy way to test the NotifierBuilderMixin."""
def __init__(self, manager):
"""Initialize with our fake manager."""
self.names = sorted(manager)
self.manager = manager
@pytest.fixture
def notifier_builder():
"""Create a fake plugin type manager."""
return FakePluginTypeManager(manager={
'T100': object(),
'T101': object(),
'T110': object(),
})
def test_build_notifier(notifier_builder):
"""Verify we properly build a Notifier object."""
notifier = notifier_builder.build_notifier()
for name in ('T100', 'T101', 'T110'):
assert list(notifier.listeners_for(name)) == [
notifier_builder.manager[name]
]

View file

@ -7,7 +7,6 @@ import pytest
from flake8 import style_guide
from flake8 import utils
from flake8.formatting import base
from flake8.plugins import notifier
def create_options(**kwargs):
@ -22,35 +21,10 @@ def create_options(**kwargs):
return optparse.Values(kwargs)
@pytest.mark.parametrize('select_list,ignore_list,error_code', [
(['E111', 'E121'], [], 'E111'),
(['E111', 'E121'], [], 'E121'),
(['E11', 'E121'], ['E1'], 'E112'),
(['E41'], ['E2', 'E12', 'E4'], 'E410'),
])
def test_handle_error_notifies_listeners(select_list, ignore_list, error_code):
"""Verify that error codes notify the listener trie appropriately."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
guide = style_guide.StyleGuide(create_options(select=select_list,
ignore=ignore_list),
listener_trie=listener_trie,
formatter=formatter)
with mock.patch('linecache.getline', return_value=''):
guide.handle_error(error_code, 'stdin', 1, 0, 'error found')
error = style_guide.Violation(
error_code, 'stdin', 1, 1, 'error found', None)
listener_trie.notify.assert_called_once_with(error_code, error)
formatter.handle.assert_called_once_with(error)
def test_handle_error_does_not_raise_type_errors():
"""Verify that we handle our inputs better."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
guide = style_guide.StyleGuide(create_options(select=['T111'], ignore=[]),
listener_trie=listener_trie,
formatter=formatter)
assert 1 == guide.handle_error(
@ -58,41 +32,11 @@ def test_handle_error_does_not_raise_type_errors():
)
@pytest.mark.parametrize('select_list,ignore_list,error_code', [
(['E111', 'E121'], [], 'E122'),
(['E11', 'E12'], [], 'E132'),
(['E2', 'E12'], [], 'E321'),
(['E2', 'E12'], [], 'E410'),
(['E111', 'E121'], ['E2'], 'E122'),
(['E11', 'E12'], ['E13'], 'E132'),
(['E1', 'E3'], ['E32'], 'E321'),
(['E4'], ['E2', 'E12', 'E41'], 'E410'),
(['E111', 'E121'], [], 'E112'),
])
def test_handle_error_does_not_notify_listeners(select_list, ignore_list,
error_code):
"""Verify that error codes notify the listener trie appropriately."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
guide = style_guide.StyleGuide(create_options(select=select_list,
ignore=ignore_list),
listener_trie=listener_trie,
formatter=formatter)
with mock.patch('linecache.getline', return_value=''):
guide.handle_error(error_code, 'stdin', 1, 1, 'error found')
assert listener_trie.notify.called is False
assert formatter.handle.called is False
def test_style_guide_manager():
"""Verify how the StyleGuideManager creates a default style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options()
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
guide = style_guide.StyleGuideManager(options, formatter=formatter)
assert guide.default_style_guide.options is options
assert len(guide.style_guides) == 1
@ -114,11 +58,9 @@ PER_FILE_IGNORES_UNPARSED = [
])
def test_style_guide_applies_to(style_guide_file, filename, expected):
"""Verify that we match a file to its style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options()
guide = style_guide.StyleGuide(options,
listener_trie=listener_trie,
formatter=formatter,
filename=style_guide_file)
assert guide.applies_to(filename) is expected
@ -126,12 +68,9 @@ def test_style_guide_applies_to(style_guide_file, filename, expected):
def test_style_guide_manager_pre_file_ignores_parsing():
"""Verify how the StyleGuideManager creates a default style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED)
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
guide = style_guide.StyleGuideManager(options, formatter=formatter)
assert len(guide.style_guides) == 5
assert list(map(utils.normalize_path,
["first_file.py", "second_file.py", "third_file.py",
@ -150,14 +89,11 @@ def test_style_guide_manager_pre_file_ignores_parsing():
def test_style_guide_manager_pre_file_ignores(ignores, violation, filename,
handle_error_return):
"""Verify how the StyleGuideManager creates a default style guide."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options(ignore=ignores,
select=['E', 'F', 'W'],
per_file_ignores=PER_FILE_IGNORES_UNPARSED)
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
guide = style_guide.StyleGuideManager(options, formatter=formatter)
assert (guide.handle_error(violation, filename, 1, 1, "Fake text")
== handle_error_return)
@ -172,12 +108,9 @@ def test_style_guide_manager_pre_file_ignores(ignores, violation, filename,
])
def test_style_guide_manager_style_guide_for(filename, expected):
"""Verify the style guide selection function."""
listener_trie = mock.create_autospec(notifier.Notifier, instance=True)
formatter = mock.create_autospec(base.BaseFormatter, instance=True)
options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED)
guide = style_guide.StyleGuideManager(options,
listener_trie=listener_trie,
formatter=formatter)
guide = style_guide.StyleGuideManager(options, formatter=formatter)
file_guide = guide.style_guide_for(filename)
assert file_guide.filename == expected

View file

@ -1,122 +0,0 @@
"""Unit test for the _trie module."""
from flake8.plugins import _trie as trie
class TestTrie(object):
"""Collection of tests for the Trie class."""
def test_traverse_without_data(self):
"""Verify the behaviour when traversing an empty Trie."""
tree = trie.Trie()
assert list(tree.traverse()) == []
def test_traverse_with_data(self):
"""Verify that traversal of our Trie is depth-first and pre-order."""
tree = trie.Trie()
tree.add('A', 'A')
tree.add('a', 'a')
tree.add('AB', 'B')
tree.add('Ab', 'b')
tree.add('AbC', 'C')
tree.add('Abc', 'c')
# The trie tree here should look something like
#
# <root>
# / \
# A a
# / |
# B b
# / \
# C c
#
# And the traversal should look like:
#
# A B b C c a
expected_order = ['A', 'B', 'b', 'C', 'c', 'a']
for expected, actual_node in zip(expected_order, tree.traverse()):
assert actual_node.prefix == expected
def test_find(self):
"""Exercise the Trie.find method."""
tree = trie.Trie()
tree.add('A', 'A')
tree.add('a', 'a')
tree.add('AB', 'AB')
tree.add('Ab', 'Ab')
tree.add('AbC', 'AbC')
tree.add('Abc', 'Abc')
assert tree.find('AB').data == ['AB']
assert tree.find('AbC').data == ['AbC']
assert tree.find('A').data == ['A']
assert tree.find('X') is None
class TestTrieNode(object):
"""Collection of tests for the TrieNode class."""
def test_add_child(self):
"""Verify we add children appropriately."""
node = trie.TrieNode('E', 'E is for Eat')
assert node.find_prefix('a') is None
added = node.add_child('a', 'a is for Apple')
assert node.find_prefix('a') is added
def test_add_child_overrides_previous_child(self):
"""Verify adding a child will replace the previous child."""
node = trie.TrieNode('E', 'E is for Eat', children={
'a': trie.TrieNode('a', 'a is for Apple')
})
previous = node.find_prefix('a')
assert previous is not None
added = node.add_child('a', 'a is for Ascertain')
assert node.find_prefix('a') is added
def test_find_prefix(self):
"""Verify we can find a child with the specified prefix."""
node = trie.TrieNode('E', 'E is for Eat', children={
'a': trie.TrieNode('a', 'a is for Apple')
})
child = node.find_prefix('a')
assert child is not None
assert child.prefix == 'a'
assert child.data == 'a is for Apple'
def test_find_prefix_returns_none_when_no_children_have_the_prefix(self):
"""Verify we receive None from find_prefix for missing children."""
node = trie.TrieNode('E', 'E is for Eat', children={
'a': trie.TrieNode('a', 'a is for Apple')
})
assert node.find_prefix('b') is None
def test_traverse_does_nothing_when_a_node_has_no_children(self):
"""Verify traversing a node with no children does nothing."""
node = trie.TrieNode('E', 'E is for Eat')
assert list(node.traverse()) == []
def test_traverse(self):
"""Verify traversal is depth-first and pre-order."""
root = trie.TrieNode(None, None)
node = root.add_child('A', 'A')
root.add_child('a', 'a')
node.add_child('B', 'B')
node = node.add_child('b', 'b')
node.add_child('C', 'C')
node.add_child('c', 'c')
# The sub-tree here should look something like
#
# <root>
# / \
# A a
# / |
# B b
# / \
# C c
#
# And the traversal should look like:
#
# A B b C c a
expected_order = ['A', 'B', 'b', 'C', 'c', 'a']
for expected, actual_node in zip(expected_order, root.traverse()):
assert actual_node.prefix == expected