mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-06 13:06:53 +00:00
extend black formatting to tests as well
This commit is contained in:
parent
a7174759e9
commit
af1668bf04
45 changed files with 1644 additions and 1307 deletions
|
|
@ -4,8 +4,8 @@
|
|||
class ExtensionTestPlugin2:
|
||||
"""Extension test plugin in its own directory."""
|
||||
|
||||
name = 'ExtensionTestPlugin2'
|
||||
version = '1.0.0'
|
||||
name = "ExtensionTestPlugin2"
|
||||
version = "1.0.0"
|
||||
|
||||
def __init__(self, tree):
|
||||
"""Construct an instance of test plugin."""
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from flake8.options import aggregator
|
|||
from flake8.options import config
|
||||
from flake8.options import manager
|
||||
|
||||
CLI_SPECIFIED_CONFIG = 'tests/fixtures/config_files/cli-specified.ini'
|
||||
CLI_SPECIFIED_CONFIG = "tests/fixtures/config_files/cli-specified.ini"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -18,8 +18,8 @@ def optmanager():
|
|||
prelim_parser = argparse.ArgumentParser(add_help=False)
|
||||
options.register_preliminary_options(prelim_parser)
|
||||
option_manager = manager.OptionManager(
|
||||
prog='flake8',
|
||||
version='3.0.0',
|
||||
prog="flake8",
|
||||
version="3.0.0",
|
||||
parents=[prelim_parser],
|
||||
)
|
||||
options.register_default_options(option_manager)
|
||||
|
|
@ -28,31 +28,50 @@ def optmanager():
|
|||
|
||||
def test_aggregate_options_with_config(optmanager):
|
||||
"""Verify we aggregate options and config values appropriately."""
|
||||
arguments = ['flake8', '--select',
|
||||
'E11,E34,E402,W,F', '--exclude', 'tests/*']
|
||||
arguments = [
|
||||
"flake8",
|
||||
"--select",
|
||||
"E11,E34,E402,W,F",
|
||||
"--exclude",
|
||||
"tests/*",
|
||||
]
|
||||
config_finder = config.ConfigFileFinder(
|
||||
'flake8',
|
||||
config_file=CLI_SPECIFIED_CONFIG)
|
||||
"flake8", config_file=CLI_SPECIFIED_CONFIG
|
||||
)
|
||||
options, args = aggregator.aggregate_options(
|
||||
optmanager, config_finder, arguments)
|
||||
optmanager, config_finder, arguments
|
||||
)
|
||||
|
||||
assert options.select == ['E11', 'E34', 'E402', 'W', 'F']
|
||||
assert options.ignore == ['E123', 'W234', 'E111']
|
||||
assert options.exclude == [os.path.abspath('tests/*')]
|
||||
assert options.select == ["E11", "E34", "E402", "W", "F"]
|
||||
assert options.ignore == ["E123", "W234", "E111"]
|
||||
assert options.exclude == [os.path.abspath("tests/*")]
|
||||
|
||||
|
||||
def test_aggregate_options_when_isolated(optmanager):
|
||||
"""Verify we aggregate options and config values appropriately."""
|
||||
arguments = ['flake8', '--select', 'E11,E34,E402,W,F',
|
||||
'--exclude', 'tests/*']
|
||||
config_finder = config.ConfigFileFinder(
|
||||
'flake8', ignore_config_files=True)
|
||||
optmanager.extend_default_ignore(['E8'])
|
||||
options, args = aggregator.aggregate_options(
|
||||
optmanager, config_finder, arguments)
|
||||
|
||||
assert options.select == ['E11', 'E34', 'E402', 'W', 'F']
|
||||
assert sorted(options.ignore) == [
|
||||
'E121', 'E123', 'E126', 'E226', 'E24', 'E704', 'E8', 'W503', 'W504',
|
||||
arguments = [
|
||||
"flake8",
|
||||
"--select",
|
||||
"E11,E34,E402,W,F",
|
||||
"--exclude",
|
||||
"tests/*",
|
||||
]
|
||||
assert options.exclude == [os.path.abspath('tests/*')]
|
||||
config_finder = config.ConfigFileFinder("flake8", ignore_config_files=True)
|
||||
optmanager.extend_default_ignore(["E8"])
|
||||
options, args = aggregator.aggregate_options(
|
||||
optmanager, config_finder, arguments
|
||||
)
|
||||
|
||||
assert options.select == ["E11", "E34", "E402", "W", "F"]
|
||||
assert sorted(options.ignore) == [
|
||||
"E121",
|
||||
"E123",
|
||||
"E126",
|
||||
"E226",
|
||||
"E24",
|
||||
"E704",
|
||||
"E8",
|
||||
"W503",
|
||||
"W504",
|
||||
]
|
||||
assert options.exclude == [os.path.abspath("tests/*")]
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ from flake8.api import legacy
|
|||
def test_legacy_api(tmpdir):
|
||||
"""A basic end-to-end test for the legacy api reporting errors."""
|
||||
with tmpdir.as_cwd():
|
||||
t_py = tmpdir.join('t.py')
|
||||
t_py.write('import os # unused import\n')
|
||||
t_py = tmpdir.join("t.py")
|
||||
t_py.write("import os # unused import\n")
|
||||
|
||||
style_guide = legacy.get_style_guide()
|
||||
report = style_guide.check_files([t_py.strpath])
|
||||
|
|
|
|||
|
|
@ -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():
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ def _call_main(argv, retv=0):
|
|||
|
||||
def test_diff_option(tmpdir, capsys):
|
||||
"""Ensure that `flake8 --diff` works."""
|
||||
t_py_contents = '''\
|
||||
t_py_contents = """\
|
||||
import os
|
||||
import sys # unused but not part of diff
|
||||
|
||||
|
|
@ -26,9 +26,9 @@ print('(to avoid trailing whitespace in test)')
|
|||
print(os.path.join('foo', 'bar'))
|
||||
|
||||
y # part of the diff and an error
|
||||
'''
|
||||
"""
|
||||
|
||||
diff = '''\
|
||||
diff = """\
|
||||
diff --git a/t.py b/t.py
|
||||
index d64ac39..7d943de 100644
|
||||
--- a/t.py
|
||||
|
|
@ -39,39 +39,39 @@ index d64ac39..7d943de 100644
|
|||
print(os.path.join('foo', 'bar'))
|
||||
+
|
||||
+y # part of the diff and an error
|
||||
'''
|
||||
"""
|
||||
|
||||
with mock.patch.object(utils, 'stdin_get_value', return_value=diff):
|
||||
with mock.patch.object(utils, "stdin_get_value", return_value=diff):
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write(t_py_contents)
|
||||
_call_main(['--diff'], retv=1)
|
||||
tmpdir.join("t.py").write(t_py_contents)
|
||||
_call_main(["--diff"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == "t.py:8:1: F821 undefined name 'y'\n"
|
||||
assert err == ''
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_form_feed_line_split(tmpdir, capsys):
|
||||
"""Test that form feed is treated the same for stdin."""
|
||||
src = 'x=1\n\f\ny=1\n'
|
||||
expected_out = '''\
|
||||
src = "x=1\n\f\ny=1\n"
|
||||
expected_out = """\
|
||||
t.py:1:2: E225 missing whitespace around operator
|
||||
t.py:3:2: E225 missing whitespace around operator
|
||||
'''
|
||||
"""
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write(src)
|
||||
tmpdir.join("t.py").write(src)
|
||||
|
||||
with mock.patch.object(utils, 'stdin_get_value', return_value=src):
|
||||
_call_main(['-', '--stdin-display-name=t.py'], retv=1)
|
||||
with mock.patch.object(utils, "stdin_get_value", return_value=src):
|
||||
_call_main(["-", "--stdin-display-name=t.py"], retv=1)
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected_out
|
||||
assert err == ''
|
||||
assert err == ""
|
||||
|
||||
_call_main(['t.py'], retv=1)
|
||||
_call_main(["t.py"], retv=1)
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected_out
|
||||
assert err == ''
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_e101_indent_char_does_not_reset(tmpdir, capsys):
|
||||
|
|
@ -89,82 +89,79 @@ if True:
|
|||
"""
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write(t_py_contents)
|
||||
_call_main(['t.py'])
|
||||
tmpdir.join("t.py").write(t_py_contents)
|
||||
_call_main(["t.py"])
|
||||
|
||||
|
||||
def test_statistics_option(tmpdir, capsys):
|
||||
"""Ensure that `flake8 --statistics` works."""
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write('import os\nimport sys\n')
|
||||
_call_main(['--statistics', 't.py'], retv=1)
|
||||
tmpdir.join("t.py").write("import os\nimport sys\n")
|
||||
_call_main(["--statistics", "t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
expected = """\
|
||||
t.py:1:1: F401 'os' imported but unused
|
||||
t.py:2:1: F401 'sys' imported but unused
|
||||
2 F401 'os' imported but unused
|
||||
'''
|
||||
assert err == ''
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_show_source_option(tmpdir, capsys):
|
||||
"""Ensure that --show-source and --no-show-source work."""
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('tox.ini').write('[flake8]\nshow_source = true\n')
|
||||
tmpdir.join('t.py').write('import os\n')
|
||||
_call_main(['t.py'], retv=1)
|
||||
tmpdir.join("tox.ini").write("[flake8]\nshow_source = true\n")
|
||||
tmpdir.join("t.py").write("import os\n")
|
||||
_call_main(["t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
expected = """\
|
||||
t.py:1:1: F401 'os' imported but unused
|
||||
import os
|
||||
^
|
||||
'''
|
||||
assert err == ''
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
assert err == ""
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
_call_main(['t.py', '--no-show-source'], retv=1)
|
||||
_call_main(["t.py", "--no-show-source"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
expected = """\
|
||||
t.py:1:1: F401 'os' imported but unused
|
||||
'''
|
||||
assert err == ''
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_extend_exclude(tmpdir, capsys):
|
||||
"""Ensure that `flake8 --extend-exclude` works."""
|
||||
for d in ['project', 'vendor', 'legacy', '.git', '.tox', '.hg']:
|
||||
tmpdir.mkdir(d).join('t.py').write('import os\nimport sys\n')
|
||||
for d in ["project", "vendor", "legacy", ".git", ".tox", ".hg"]:
|
||||
tmpdir.mkdir(d).join("t.py").write("import os\nimport sys\n")
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
_call_main(['--extend-exclude=vendor,legacy/'], retv=1)
|
||||
_call_main(["--extend-exclude=vendor,legacy/"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
expected_out = '''\
|
||||
expected_out = """\
|
||||
./project/t.py:1:1: F401 'os' imported but unused
|
||||
./project/t.py:2:1: F401 'sys' imported but unused
|
||||
'''
|
||||
assert out == expected_out.replace('/', os.sep)
|
||||
assert err == ''
|
||||
"""
|
||||
assert out == expected_out.replace("/", os.sep)
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_malformed_per_file_ignores_error(tmpdir, capsys):
|
||||
"""Test the error message for malformed `per-file-ignores`."""
|
||||
setup_cfg = '''\
|
||||
setup_cfg = """\
|
||||
[flake8]
|
||||
per-file-ignores =
|
||||
incorrect/*
|
||||
values/*
|
||||
'''
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('setup.cfg').write(setup_cfg)
|
||||
_call_main(['.'], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
"""
|
||||
expected = """\
|
||||
There was a critical error during execution of Flake8:
|
||||
Expected `per-file-ignores` to be a mapping from file exclude patterns to ignore codes.
|
||||
|
||||
|
|
@ -172,50 +169,59 @@ Configured `per-file-ignores` setting:
|
|||
|
||||
incorrect/*
|
||||
values/*
|
||||
''' # noqa: E501
|
||||
""" # noqa: E501
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join("setup.cfg").write(setup_cfg)
|
||||
_call_main(["."], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
|
||||
|
||||
def test_tokenization_error_but_not_syntax_error(tmpdir, capsys):
|
||||
"""Test that flake8 does not crash on tokenization errors."""
|
||||
with tmpdir.as_cwd():
|
||||
# this is a crash in the tokenizer, but not in the ast
|
||||
tmpdir.join('t.py').write("b'foo' \\\n")
|
||||
_call_main(['t.py'], retv=1)
|
||||
tmpdir.join("t.py").write("b'foo' \\\n")
|
||||
_call_main(["t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == 't.py:1:1: E902 TokenError: EOF in multi-line statement\n'
|
||||
assert err == ''
|
||||
assert out == "t.py:1:1: E902 TokenError: EOF in multi-line statement\n"
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_tokenization_error_is_a_syntax_error(tmpdir, capsys):
|
||||
"""Test when tokenize raises a SyntaxError."""
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write('if True:\n pass\n pass\n')
|
||||
_call_main(['t.py'], retv=1)
|
||||
tmpdir.join("t.py").write("if True:\n pass\n pass\n")
|
||||
_call_main(["t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == 't.py:1:1: E902 IndentationError: unindent does not match any outer indentation level\n' # noqa: E501
|
||||
assert err == ''
|
||||
expected = "t.py:1:1: E902 IndentationError: unindent does not match any outer indentation level\n" # noqa: E501
|
||||
assert out == expected
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_bug_report_successful(capsys):
|
||||
"""Test that --bug-report does not crash."""
|
||||
_call_main(['--bug-report'])
|
||||
_call_main(["--bug-report"])
|
||||
out, err = capsys.readouterr()
|
||||
assert json.loads(out)
|
||||
assert err == ''
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_specific_noqa_does_not_clobber_pycodestyle_noqa(tmpdir, capsys):
|
||||
"""See https://github.com/pycqa/flake8/issues/1104."""
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write("test = ('ABC' == None) # noqa: E501\n")
|
||||
_call_main(['t.py'], retv=1)
|
||||
tmpdir.join("t.py").write("test = ('ABC' == None) # noqa: E501\n")
|
||||
_call_main(["t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
expected = """\
|
||||
t.py:1:15: E711 comparison to None should be 'if cond is None:'
|
||||
'''
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
|
||||
|
||||
def test_specific_noqa_on_line_with_continuation(tmpdir, capsys):
|
||||
|
|
@ -230,60 +236,64 @@ x = """
|
|||
'''
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write(t_py_src)
|
||||
_call_main(['t.py'], retv=0)
|
||||
tmpdir.join("t.py").write(t_py_src)
|
||||
_call_main(["t.py"], retv=0)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == err == ''
|
||||
assert out == err == ""
|
||||
|
||||
|
||||
def test_physical_line_file_not_ending_in_newline(tmpdir, capsys):
|
||||
"""See https://github.com/PyCQA/pycodestyle/issues/960."""
|
||||
t_py_src = 'def f():\n\tpass'
|
||||
t_py_src = "def f():\n\tpass"
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write(t_py_src)
|
||||
_call_main(['t.py'], retv=1)
|
||||
tmpdir.join("t.py").write(t_py_src)
|
||||
_call_main(["t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
expected = """\
|
||||
t.py:2:1: W191 indentation contains tabs
|
||||
t.py:2:6: W292 no newline at end of file
|
||||
'''
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
|
||||
|
||||
def test_physical_line_file_not_ending_in_newline_trailing_ws(tmpdir, capsys):
|
||||
"""See https://github.com/PyCQA/pycodestyle/issues/960."""
|
||||
t_py_src = 'x = 1 '
|
||||
t_py_src = "x = 1 "
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
tmpdir.join('t.py').write(t_py_src)
|
||||
_call_main(['t.py'], retv=1)
|
||||
tmpdir.join("t.py").write(t_py_src)
|
||||
_call_main(["t.py"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == '''\
|
||||
expected = """\
|
||||
t.py:1:6: W291 trailing whitespace
|
||||
t.py:1:9: W292 no newline at end of file
|
||||
'''
|
||||
"""
|
||||
out, err = capsys.readouterr()
|
||||
assert out == expected
|
||||
|
||||
|
||||
def test_obtaining_args_from_sys_argv_when_not_explicity_provided(capsys):
|
||||
"""Test that arguments are obtained from 'sys.argv'."""
|
||||
with mock.patch('sys.argv', ['flake8', '--help']):
|
||||
with mock.patch("sys.argv", ["flake8", "--help"]):
|
||||
_call_main(None)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out.startswith('usage: flake8 [options] file file ...\n')
|
||||
assert err == ''
|
||||
assert out.startswith("usage: flake8 [options] file file ...\n")
|
||||
assert err == ""
|
||||
|
||||
|
||||
def test_cli_config_option_respected(tmp_path):
|
||||
"""Test --config is used."""
|
||||
config = tmp_path / "flake8.ini"
|
||||
config.write_text("""\
|
||||
config.write_text(
|
||||
"""\
|
||||
[flake8]
|
||||
ignore = F401
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
py_file = tmp_path / "t.py"
|
||||
py_file.write_text("import os\n")
|
||||
|
|
@ -294,10 +304,12 @@ ignore = F401
|
|||
def test_cli_isolated_overrides_config_option(tmp_path):
|
||||
"""Test --isolated overrides --config."""
|
||||
config = tmp_path / "flake8.ini"
|
||||
config.write_text("""\
|
||||
config.write_text(
|
||||
"""\
|
||||
[flake8]
|
||||
ignore = F401
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
py_file = tmp_path / "t.py"
|
||||
py_file.write_text("import os\n")
|
||||
|
|
@ -316,13 +328,13 @@ def test_file_not_found(tmpdir, capsys):
|
|||
|
||||
def test_output_file(tmpdir, capsys):
|
||||
"""Ensure that --output-file is honored."""
|
||||
tmpdir.join('t.py').write('import os\n')
|
||||
tmpdir.join("t.py").write("import os\n")
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
_call_main(['t.py', '--output-file=f'], retv=1)
|
||||
_call_main(["t.py", "--output-file=f"], retv=1)
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert out == err == ""
|
||||
|
||||
expected = "t.py:1:1: F401 'os' imported but unused\n"
|
||||
assert tmpdir.join('f').read() == expected
|
||||
assert tmpdir.join("f").read() == expected
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
"""Integration tests for plugin loading."""
|
||||
from flake8.main import application
|
||||
|
||||
LOCAL_PLUGIN_CONFIG = 'tests/fixtures/config_files/local-plugin.ini'
|
||||
LOCAL_PLUGIN_PATH_CONFIG = 'tests/fixtures/config_files/local-plugin-path.ini'
|
||||
LOCAL_PLUGIN_CONFIG = "tests/fixtures/config_files/local-plugin.ini"
|
||||
LOCAL_PLUGIN_PATH_CONFIG = "tests/fixtures/config_files/local-plugin-path.ini"
|
||||
|
||||
|
||||
class ExtensionTestPlugin:
|
||||
"""Extension test plugin."""
|
||||
|
||||
name = 'ExtensionTestPlugin'
|
||||
version = '1.0.0'
|
||||
name = "ExtensionTestPlugin"
|
||||
version = "1.0.0"
|
||||
|
||||
def __init__(self, tree):
|
||||
"""Construct an instance of test plugin."""
|
||||
|
|
@ -20,14 +20,14 @@ class ExtensionTestPlugin:
|
|||
@classmethod
|
||||
def add_options(cls, parser):
|
||||
"""Register options."""
|
||||
parser.add_option('--anopt')
|
||||
parser.add_option("--anopt")
|
||||
|
||||
|
||||
class ReportTestPlugin:
|
||||
"""Report test plugin."""
|
||||
|
||||
name = 'ReportTestPlugin'
|
||||
version = '1.0.0'
|
||||
name = "ReportTestPlugin"
|
||||
version = "1.0.0"
|
||||
|
||||
def __init__(self, tree):
|
||||
"""Construct an instance of test plugin."""
|
||||
|
|
@ -39,28 +39,29 @@ class ReportTestPlugin:
|
|||
def test_enable_local_plugin_from_config():
|
||||
"""App can load a local plugin from config file."""
|
||||
app = application.Application()
|
||||
app.initialize(['flake8', '--config', LOCAL_PLUGIN_CONFIG])
|
||||
app.initialize(["flake8", "--config", LOCAL_PLUGIN_CONFIG])
|
||||
|
||||
assert app.check_plugins is not None
|
||||
assert app.check_plugins['XE'].plugin is ExtensionTestPlugin
|
||||
assert app.check_plugins["XE"].plugin is ExtensionTestPlugin
|
||||
assert app.formatting_plugins is not None
|
||||
assert app.formatting_plugins['XR'].plugin is ReportTestPlugin
|
||||
assert app.formatting_plugins["XR"].plugin is ReportTestPlugin
|
||||
|
||||
|
||||
def test_local_plugin_can_add_option():
|
||||
"""A local plugin can add a CLI option."""
|
||||
app = application.Application()
|
||||
app.initialize(
|
||||
['flake8', '--config', LOCAL_PLUGIN_CONFIG, '--anopt', 'foo'])
|
||||
["flake8", "--config", LOCAL_PLUGIN_CONFIG, "--anopt", "foo"]
|
||||
)
|
||||
|
||||
assert app.options is not None
|
||||
assert app.options.anopt == 'foo'
|
||||
assert app.options.anopt == "foo"
|
||||
|
||||
|
||||
def test_enable_local_plugin_at_non_installed_path():
|
||||
"""Can add a paths option in local-plugins config section for finding."""
|
||||
app = application.Application()
|
||||
app.initialize(['flake8', '--config', LOCAL_PLUGIN_PATH_CONFIG])
|
||||
app.initialize(["flake8", "--config", LOCAL_PLUGIN_PATH_CONFIG])
|
||||
|
||||
assert app.check_plugins is not None
|
||||
assert app.check_plugins['XE'].plugin.name == 'ExtensionTestPlugin2'
|
||||
assert app.check_plugins["XE"].plugin.name == "ExtensionTestPlugin2"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue