mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-09 22:34:17 +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
|
|
@ -10,23 +10,23 @@ from flake8.formatting import base
|
|||
|
||||
def options(**kwargs):
|
||||
"""Create an argparse.Namespace instance."""
|
||||
kwargs.setdefault('output_file', None)
|
||||
kwargs.setdefault('tee', False)
|
||||
kwargs.setdefault("output_file", None)
|
||||
kwargs.setdefault("tee", False)
|
||||
return argparse.Namespace(**kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('filename', [None, 'out.txt'])
|
||||
@pytest.mark.parametrize("filename", [None, "out.txt"])
|
||||
def test_start(filename):
|
||||
"""Verify we open a new file in the start method."""
|
||||
mock_open = mock.mock_open()
|
||||
formatter = base.BaseFormatter(options(output_file=filename))
|
||||
with mock.patch('flake8.formatting.base.open', mock_open):
|
||||
with mock.patch("flake8.formatting.base.open", mock_open):
|
||||
formatter.start()
|
||||
|
||||
if filename is None:
|
||||
assert mock_open.called is False
|
||||
else:
|
||||
mock_open.assert_called_once_with(filename, 'a')
|
||||
mock_open.assert_called_once_with(filename, "a")
|
||||
|
||||
|
||||
def test_stop():
|
||||
|
|
@ -45,68 +45,79 @@ def test_format_needs_to_be_implemented():
|
|||
formatter = base.BaseFormatter(options())
|
||||
with pytest.raises(NotImplementedError):
|
||||
formatter.format(
|
||||
style_guide.Violation('A000', 'file.py', 1, 1, 'error text', None)
|
||||
style_guide.Violation("A000", "file.py", 1, 1, "error text", None)
|
||||
)
|
||||
|
||||
|
||||
def test_show_source_returns_nothing_when_not_showing_source():
|
||||
"""Ensure we return nothing when users want nothing."""
|
||||
formatter = base.BaseFormatter(options(show_source=False))
|
||||
assert formatter.show_source(
|
||||
style_guide.Violation('A000', 'file.py', 1, 1, 'error text', 'line')
|
||||
) == ''
|
||||
assert (
|
||||
formatter.show_source(
|
||||
style_guide.Violation(
|
||||
"A000", "file.py", 1, 1, "error text", "line"
|
||||
)
|
||||
)
|
||||
== ""
|
||||
)
|
||||
|
||||
|
||||
def test_show_source_returns_nothing_when_there_is_source():
|
||||
"""Ensure we return nothing when there is no line."""
|
||||
formatter = base.BaseFormatter(options(show_source=True))
|
||||
assert formatter.show_source(
|
||||
style_guide.Violation('A000', 'file.py', 1, 1, 'error text', None)
|
||||
) == ''
|
||||
assert (
|
||||
formatter.show_source(
|
||||
style_guide.Violation("A000", "file.py", 1, 1, "error text", None)
|
||||
)
|
||||
== ""
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('line1', 'line2', 'column'), [
|
||||
(
|
||||
'x=1\n',
|
||||
' ^',
|
||||
2,
|
||||
),
|
||||
(
|
||||
' x=(1\n +2)\n',
|
||||
' ^',
|
||||
5,
|
||||
),
|
||||
(
|
||||
'\tx\t=\ty\n',
|
||||
'\t \t \t^',
|
||||
6,
|
||||
),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
("line1", "line2", "column"),
|
||||
[
|
||||
(
|
||||
"x=1\n",
|
||||
" ^",
|
||||
2,
|
||||
),
|
||||
(
|
||||
" x=(1\n +2)\n",
|
||||
" ^",
|
||||
5,
|
||||
),
|
||||
(
|
||||
"\tx\t=\ty\n",
|
||||
"\t \t \t^",
|
||||
6,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_show_source_updates_physical_line_appropriately(line1, line2, column):
|
||||
"""Ensure the error column is appropriately indicated."""
|
||||
formatter = base.BaseFormatter(options(show_source=True))
|
||||
error = style_guide.Violation('A000', 'file.py', 1, column, 'error', line1)
|
||||
error = style_guide.Violation("A000", "file.py", 1, column, "error", line1)
|
||||
output = formatter.show_source(error)
|
||||
assert output == line1 + line2
|
||||
|
||||
|
||||
@pytest.mark.parametrize('tee', [False, True])
|
||||
@pytest.mark.parametrize("tee", [False, True])
|
||||
def test_write_uses_an_output_file(tee):
|
||||
"""Verify that we use the output file when it's present."""
|
||||
line = 'Something to write'
|
||||
source = 'source'
|
||||
line = "Something to write"
|
||||
source = "source"
|
||||
filemock = mock.Mock()
|
||||
|
||||
formatter = base.BaseFormatter(options(tee=tee))
|
||||
formatter.output_fd = filemock
|
||||
|
||||
with mock.patch('flake8.formatting.base.print') as print_func:
|
||||
with mock.patch("flake8.formatting.base.print") as print_func:
|
||||
formatter.write(line, source)
|
||||
if tee:
|
||||
assert print_func.called
|
||||
assert print_func.mock_calls == [
|
||||
mock.call(line, end='\n'),
|
||||
mock.call(source, end='\n'),
|
||||
mock.call(line, end="\n"),
|
||||
mock.call(source, end="\n"),
|
||||
]
|
||||
else:
|
||||
assert not print_func.called
|
||||
|
|
@ -119,11 +130,11 @@ def test_write_uses_an_output_file(tee):
|
|||
]
|
||||
|
||||
|
||||
@mock.patch('flake8.formatting.base.print')
|
||||
@mock.patch("flake8.formatting.base.print")
|
||||
def test_write_uses_print(print_function):
|
||||
"""Verify that we use the print function without an output file."""
|
||||
line = 'Something to write'
|
||||
source = 'source'
|
||||
line = "Something to write"
|
||||
source = "source"
|
||||
|
||||
formatter = base.BaseFormatter(options())
|
||||
formatter.write(line, source)
|
||||
|
|
@ -131,8 +142,8 @@ def test_write_uses_print(print_function):
|
|||
assert print_function.called is True
|
||||
assert print_function.call_count == 2
|
||||
assert print_function.mock_calls == [
|
||||
mock.call(line, end='\n'),
|
||||
mock.call(source, end='\n'),
|
||||
mock.call(line, end="\n"),
|
||||
mock.call(source, end="\n"),
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -163,14 +174,14 @@ def test_handle_formats_the_error():
|
|||
formatter = FormatFormatter(options(show_source=False))
|
||||
filemock = formatter.output_fd = mock.Mock()
|
||||
error = style_guide.Violation(
|
||||
code='A001',
|
||||
filename='example.py',
|
||||
code="A001",
|
||||
filename="example.py",
|
||||
line_number=1,
|
||||
column_number=1,
|
||||
text='Fake error',
|
||||
physical_line='a = 1',
|
||||
text="Fake error",
|
||||
physical_line="a = 1",
|
||||
)
|
||||
|
||||
formatter.handle(error)
|
||||
|
||||
filemock.write.assert_called_once_with(repr(error) + '\n')
|
||||
filemock.write.assert_called_once_with(repr(error) + "\n")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue