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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
"""Tests for the flake8.style_guide.StyleGuide class."""
import optparse
import mock
import pytest
from flake8 import defaults
from flake8 import style_guide
from flake8.formatting import base
from flake8.plugins import notifier
def create_options(**kwargs):
"""Create and return an instance of optparse.Values."""
kwargs.setdefault('select', [])
kwargs.setdefault('extended_default_select', [])
kwargs.setdefault('ignore', [])
kwargs.setdefault('disable_noqa', False)
kwargs.setdefault('enable_extensions', [])
return optparse.Values(kwargs)
@pytest.mark.parametrize('ignore_list,error_code', [
(['E111', 'E121'], 'E111'),
(['E111', 'E121'], 'E121'),
(['E11', 'E12'], 'E121'),
(['E2', 'E12'], 'E121'),
(['E2', 'E12'], 'E211'),
])
def test_is_user_ignored_ignores_errors(ignore_list, error_code):
"""Verify we detect users explicitly ignoring an error."""
guide = style_guide.StyleGuide(create_options(ignore=ignore_list),
listener_trie=None,
formatter=None)
assert guide.is_user_ignored(error_code) is style_guide.Ignored.Explicitly
@pytest.mark.parametrize('ignore_list,error_code', [
(['E111', 'E121'], 'E112'),
(['E111', 'E121'], 'E122'),
(['E11', 'E12'], 'W121'),
(['E2', 'E12'], 'E112'),
(['E2', 'E12'], 'E111'),
])
def test_is_user_ignored_implicitly_selects_errors(ignore_list, error_code):
"""Verify we detect users does not explicitly ignore an error."""
guide = style_guide.StyleGuide(create_options(ignore=ignore_list),
listener_trie=None,
formatter=None)
assert guide.is_user_ignored(error_code) is style_guide.Selected.Implicitly
@pytest.mark.parametrize('select_list,enable_extensions,error_code', [
(['E111', 'E121'], [], 'E111'),
(['E111', 'E121'], [], 'E121'),
(['E11', 'E12'], [], 'E121'),
(['E2', 'E12'], [], 'E121'),
(['E2', 'E12'], [], 'E211'),
(['E1'], ['E2'], 'E211'),
([], ['E2'], 'E211'),
])
def test_is_user_selected_selects_errors(select_list, enable_extensions,
error_code):
"""Verify we detect users explicitly selecting an error."""
guide = style_guide.StyleGuide(
options=create_options(select=select_list,
enable_extensions=enable_extensions),
listener_trie=None,
formatter=None,
)
assert (guide.is_user_selected(error_code) is
style_guide.Selected.Explicitly)
def test_is_user_selected_implicitly_selects_errors():
"""Verify we detect users implicitly selecting an error."""
select_list = []
error_code = 'E121'
guide = style_guide.StyleGuide(
create_options(
select=select_list,
extended_default_select=['E'],
),
listener_trie=None,
formatter=None,
)
assert (guide.is_user_selected(error_code) is
style_guide.Selected.Implicitly)
@pytest.mark.parametrize('select_list,error_code', [
(['E111', 'E121'], 'E112'),
(['E111', 'E121'], 'E122'),
(['E11', 'E12'], 'E132'),
(['E2', 'E12'], 'E321'),
(['E2', 'E12'], 'E410'),
])
def test_is_user_selected_excludes_errors(select_list, error_code):
"""Verify we detect users implicitly excludes an error."""
guide = style_guide.StyleGuide(create_options(select=select_list),
listener_trie=None,
formatter=None)
assert guide.is_user_selected(error_code) is style_guide.Ignored.Implicitly
@pytest.mark.parametrize('select_list,ignore_list,error_code,expected', [
(['E111', 'E121'], [], 'E111', style_guide.Decision.Selected),
(['E111', 'E121'], [], 'E112', style_guide.Decision.Ignored),
(['E111', 'E121'], [], 'E121', style_guide.Decision.Selected),
(['E111', 'E121'], [], 'E122', style_guide.Decision.Ignored),
(['E11', 'E12'], [], 'E132', style_guide.Decision.Ignored),
(['E2', 'E12'], [], 'E321', style_guide.Decision.Ignored),
(['E2', 'E12'], [], 'E410', style_guide.Decision.Ignored),
(['E11', 'E121'], ['E1'], 'E112', style_guide.Decision.Selected),
(['E111', 'E121'], ['E2'], 'E122', style_guide.Decision.Ignored),
(['E11', 'E12'], ['E13'], 'E132', style_guide.Decision.Ignored),
(['E1', 'E3'], ['E32'], 'E321', style_guide.Decision.Ignored),
([], ['E2', 'E12'], 'E410', style_guide.Decision.Ignored),
(['E4'], ['E2', 'E12', 'E41'], 'E410', style_guide.Decision.Ignored),
(['E41'], ['E2', 'E12', 'E4'], 'E410', style_guide.Decision.Selected),
(['E'], ['F'], 'E410', style_guide.Decision.Selected),
(['F'], [], 'E410', style_guide.Decision.Ignored),
])
def test_should_report_error(select_list, ignore_list, error_code, expected):
"""Verify we decide when to report an error."""
guide = style_guide.StyleGuide(create_options(select=select_list,
ignore=ignore_list),
listener_trie=None,
formatter=None)
assert guide.should_report_error(error_code) is expected
@pytest.mark.parametrize(
'select,ignore,extend_select,enabled_extensions,error_code,expected', [
(defaults.SELECT, [], ['I1'], [], 'I100',
style_guide.Decision.Selected),
(defaults.SELECT, [], ['I1'], [], 'I201',
style_guide.Decision.Selected),
(defaults.SELECT, ['I2'], ['I1'], [], 'I101',
style_guide.Decision.Selected),
(defaults.SELECT, ['I2'], ['I1'], [], 'I201',
style_guide.Decision.Ignored),
(defaults.SELECT, ['I1'], ['I10'], [], 'I101',
style_guide.Decision.Selected),
(defaults.SELECT, ['I10'], ['I1'], [], 'I101',
style_guide.Decision.Ignored),
(defaults.SELECT, [], [], ['U4'], 'U401',
style_guide.Decision.Selected),
(defaults.SELECT, ['U401'], [], ['U4'], 'U401',
style_guide.Decision.Ignored),
(defaults.SELECT, ['U401'], [], ['U4'], 'U402',
style_guide.Decision.Selected),
(['E2'], ['E21'], [], [], 'E221', style_guide.Decision.Selected),
(['E2'], ['E21'], [], [], 'E212', style_guide.Decision.Ignored),
(['F', 'W'], ['C90'], ['I1'], [], 'C901',
style_guide.Decision.Ignored),
]
)
def test_decision_for_logic(select, ignore, extend_select, enabled_extensions,
error_code, expected):
"""Verify the complicated logic of StyleGuide._decision_for.
I usually avoid testing private methods, but this one is very important in
our conflict resolution work in Flake8.
"""
guide = style_guide.StyleGuide(
create_options(
select=select, ignore=ignore,
extended_default_select=extend_select,
enable_extensions=enabled_extensions,
),
listener_trie=None,
formatter=None,
)
assert guide._decision_for(error_code) is expected
@pytest.mark.parametrize('error_code,physical_line,expected_result', [
('E111', 'a = 1', False),
('E121', 'a = 1 # noqa: E111', False),
('E121', 'a = 1 # noqa: E111,W123,F821', False),
('E111', 'a = 1 # noqa: E111,W123,F821', True),
('W123', 'a = 1 # noqa: E111,W123,F821', True),
('E111', 'a = 1 # noqa: E11,W123,F821', True),
('E111', 'a = 1 # noqa, analysis:ignore', True),
('E111', 'a = 1 # noqa analysis:ignore', True),
('E111', 'a = 1 # noqa - We do not care', True),
('E111', 'a = 1 # noqa: We do not care', True),
])
def test_is_inline_ignored(error_code, physical_line, expected_result):
"""Verify that we detect inline usage of ``# noqa``."""
guide = style_guide.StyleGuide(create_options(select=['E', 'W', 'F']),
listener_trie=None,
formatter=None)
error = style_guide.Error(error_code, 'filename.py', 1, 1, 'error text',
None)
# We want `None` to be passed as the physical line so we actually use our
# monkey-patched linecache.getline value.
with mock.patch('linecache.getline', return_value=physical_line):
assert guide.is_inline_ignored(error) is expected_result
def test_disable_is_inline_ignored():
"""Verify that is_inline_ignored exits immediately if disabling NoQA."""
guide = style_guide.StyleGuide(create_options(disable_noqa=True),
listener_trie=None,
formatter=None)
error = style_guide.Error('E121', 'filename.py', 1, 1, 'error text',
'line')
with mock.patch('linecache.getline') as getline:
assert guide.is_inline_ignored(error) is False
assert getline.called is False
@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.Error(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(
'T111', 'file.py', 1, None, 'error found', 'a = 1'
)
@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
|