extend black formatting to tests as well

This commit is contained in:
Anthony Sottile 2021-04-18 09:17:48 -07:00
parent a7174759e9
commit af1668bf04
45 changed files with 1644 additions and 1307 deletions

View file

@ -10,13 +10,13 @@ from flake8.processor import FileProcessor
PHYSICAL_LINE = "# Physical line content"
EXPECTED_REPORT = (1, 1, 'T000 Expected Message')
EXPECTED_REPORT_PHYSICAL_LINE = (1, 'T000 Expected Message')
EXPECTED_REPORT = (1, 1, "T000 Expected Message")
EXPECTED_REPORT_PHYSICAL_LINE = (1, "T000 Expected Message")
EXPECTED_RESULT_PHYSICAL_LINE = (
'T000',
"T000",
0,
1,
'Expected Message',
"Expected Message",
None,
)
@ -24,8 +24,8 @@ EXPECTED_RESULT_PHYSICAL_LINE = (
class PluginClass:
"""Simple file plugin class yielding the expected report."""
name = 'test'
version = '1.0.0'
name = "test"
version = "1.0.0"
def __init__(self, tree):
"""Construct a dummy object to provide mandatory parameter."""
@ -33,26 +33,26 @@ class PluginClass:
def run(self):
"""Run class yielding one element containing the expected report."""
yield EXPECTED_REPORT + (type(self), )
yield EXPECTED_REPORT + (type(self),)
def plugin_func(func):
"""Decorate file plugins which are implemented as functions."""
func.name = 'test'
func.version = '1.0.0'
func.name = "test"
func.version = "1.0.0"
return func
@plugin_func
def plugin_func_gen(tree):
"""Yield the expected report."""
yield EXPECTED_REPORT + (type(plugin_func_gen), )
yield EXPECTED_REPORT + (type(plugin_func_gen),)
@plugin_func
def plugin_func_list(tree):
"""Return a list of expected reports."""
return [EXPECTED_REPORT + (type(plugin_func_list), )]
return [EXPECTED_REPORT + (type(plugin_func_list),)]
@plugin_func
@ -98,35 +98,37 @@ def mock_file_checker_with_plugin(plugin_target):
Useful as a starting point for mocking reports/results.
"""
# Mock an entry point returning the plugin target
entry_point = mock.Mock(spec=['load'])
entry_point = mock.Mock(spec=["load"])
entry_point.name = plugin_target.name
entry_point.load.return_value = plugin_target
entry_point.value = 'mocked:value'
entry_point.value = "mocked:value"
# Load the checker plugins using the entry point mock
with mock.patch.object(
importlib_metadata,
'entry_points',
return_value={'flake8.extension': [entry_point]},
importlib_metadata,
"entry_points",
return_value={"flake8.extension": [entry_point]},
):
checks = manager.Checkers()
# Prevent it from reading lines from stdin or somewhere else
with mock.patch('flake8.processor.FileProcessor.read_lines',
return_value=['Line 1']):
with mock.patch(
"flake8.processor.FileProcessor.read_lines", return_value=["Line 1"]
):
file_checker = checker.FileChecker(
'-',
checks.to_dictionary(),
mock.MagicMock()
"-", checks.to_dictionary(), mock.MagicMock()
)
return file_checker
@pytest.mark.parametrize('plugin_target', [
PluginClass,
plugin_func_gen,
plugin_func_list,
])
@pytest.mark.parametrize(
"plugin_target",
[
PluginClass,
plugin_func_gen,
plugin_func_list,
],
)
def test_handle_file_plugins(plugin_target):
"""Test the FileChecker class handling different file plugin types."""
file_checker = mock_file_checker_with_plugin(plugin_target)
@ -138,20 +140,25 @@ def test_handle_file_plugins(plugin_target):
report = mock.Mock()
file_checker.report = report
file_checker.run_ast_checks()
report.assert_called_once_with(error_code=None,
line_number=EXPECTED_REPORT[0],
column=EXPECTED_REPORT[1],
text=EXPECTED_REPORT[2])
report.assert_called_once_with(
error_code=None,
line_number=EXPECTED_REPORT[0],
column=EXPECTED_REPORT[1],
text=EXPECTED_REPORT[2],
)
@pytest.mark.parametrize('plugin_target,len_results', [
(plugin_func_physical_ret, 1),
(plugin_func_physical_none, 0),
(plugin_func_physical_list_single, 1),
(plugin_func_physical_list_multiple, 2),
(plugin_func_physical_gen_single, 1),
(plugin_func_physical_gen_multiple, 3),
])
@pytest.mark.parametrize(
"plugin_target,len_results",
[
(plugin_func_physical_ret, 1),
(plugin_func_physical_none, 0),
(plugin_func_physical_list_single, 1),
(plugin_func_physical_list_multiple, 2),
(plugin_func_physical_gen_single, 1),
(plugin_func_physical_gen_multiple, 3),
],
)
def test_line_check_results(plugin_target, len_results):
"""Test the FileChecker class handling results from line checks."""
file_checker = mock_file_checker_with_plugin(plugin_target)
@ -167,54 +174,100 @@ def test_logical_line_offset_out_of_bounds():
@plugin_func
def _logical_line_out_of_bounds(logical_line):
yield 10000, 'L100 test'
yield 10000, "L100 test"
file_checker = mock_file_checker_with_plugin(_logical_line_out_of_bounds)
logical_ret = (
'',
"",
'print("xxxxxxxxxxx")',
[(0, (1, 0)), (5, (1, 5)), (6, (1, 6)), (19, (1, 19)), (20, (1, 20))],
)
with mock.patch.object(
FileProcessor, 'build_logical_line', return_value=logical_ret,
FileProcessor,
"build_logical_line",
return_value=logical_ret,
):
file_checker.run_logical_checks()
assert file_checker.results == [('L100', 0, 0, 'test', None)]
assert file_checker.results == [("L100", 0, 0, "test", None)]
PLACEHOLDER_CODE = 'some_line = "of" * code'
@pytest.mark.parametrize('results, expected_order', [
# No entries should be added
([], []),
# Results are correctly ordered
([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 1]),
# Reversed order of lines
([('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE)], [1, 0]),
# Columns are not ordered correctly (when reports are ordered correctly)
([('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE),
('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [1, 0, 2]),
([('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE)], [1, 2, 0]),
([('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 2, 1]),
([('A101', 1, 3, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE),
('A101', 3, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 1, 2]),
([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 1, 3, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE)], [0, 1, 2]),
# Previously sort column and message (so reversed) (see bug 196)
([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
('A101', 2, 1, 'charlie error', PLACEHOLDER_CODE)], [0, 1]),
])
@pytest.mark.parametrize(
"results, expected_order",
[
# No entries should be added
([], []),
# Results are correctly ordered
(
[
("A101", 1, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 1, "placeholder error", PLACEHOLDER_CODE),
],
[0, 1],
),
# Reversed order of lines
(
[
("A101", 2, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 1, 1, "placeholder error", PLACEHOLDER_CODE),
],
[1, 0],
),
# Columns are not ordered correctly
# (when reports are ordered correctly)
(
[
("A101", 1, 2, "placeholder error", PLACEHOLDER_CODE),
("A101", 1, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 1, "placeholder error", PLACEHOLDER_CODE),
],
[1, 0, 2],
),
(
[
("A101", 2, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 1, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 1, 2, "placeholder error", PLACEHOLDER_CODE),
],
[1, 2, 0],
),
(
[
("A101", 1, 2, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 2, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 1, "placeholder error", PLACEHOLDER_CODE),
],
[0, 2, 1],
),
(
[
("A101", 1, 3, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 2, "placeholder error", PLACEHOLDER_CODE),
("A101", 3, 1, "placeholder error", PLACEHOLDER_CODE),
],
[0, 1, 2],
),
(
[
("A101", 1, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 1, 3, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 2, "placeholder error", PLACEHOLDER_CODE),
],
[0, 1, 2],
),
# Previously sort column and message (so reversed) (see bug 196)
(
[
("A101", 1, 1, "placeholder error", PLACEHOLDER_CODE),
("A101", 2, 1, "charlie error", PLACEHOLDER_CODE),
],
[0, 1],
),
],
)
def test_report_order(results, expected_order):
"""
Test in which order the results will be reported.
@ -222,6 +275,7 @@ def test_report_order(results, expected_order):
It gets a list of reports from the file checkers and verifies that the
result will be ordered independent from the original report.
"""
def count_side_effect(name, sorted_results):
"""Side effect for the result handler to tell all are reported."""
return len(sorted_results)
@ -230,11 +284,11 @@ def test_report_order(results, expected_order):
# tuples to create the expected result lists from the indexes
expected_results = [results[index] for index in expected_order]
file_checker = mock.Mock(spec=['results', 'display_name'])
file_checker = mock.Mock(spec=["results", "display_name"])
file_checker.results = results
file_checker.display_name = 'placeholder'
file_checker.display_name = "placeholder"
style_guide = mock.MagicMock(spec=['options', 'processing_file'])
style_guide = mock.MagicMock(spec=["options", "processing_file"])
# Create a placeholder manager without arguments or plugins
# Just add one custom file checker which just provides the results
@ -244,9 +298,9 @@ def test_report_order(results, expected_order):
# _handle_results is the first place which gets the sorted result
# Should something non-private be mocked instead?
handler = mock.Mock(side_effect=count_side_effect)
with mock.patch.object(manager, '_handle_results', handler):
with mock.patch.object(manager, "_handle_results", handler):
assert manager.report() == (len(results), len(results))
handler.assert_called_once_with('placeholder', expected_results)
handler.assert_called_once_with("placeholder", expected_results)
def test_acquire_when_multiprocessing_pool_can_initialize():