Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_error(**kwargs):
"""Create errors with a bunch of default values."""
return style_guide.Violation(
code=kwargs.pop('code', DEFAULT_ERROR_CODE),
filename=kwargs.pop('filename', DEFAULT_FILENAME),
line_number=kwargs.pop('line_number', 1),
column_number=kwargs.pop('column_number', 1),
text=kwargs.pop('text', DEFAULT_TEXT),
physical_line=None,
)
def test_show_source_returns_nothing():
"""Verify show_source returns nothing."""
formatter = default.FilenameOnly(options())
error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')
assert formatter.show_source(error) is None
def test_format_needs_to_be_implemented():
"""Ensure BaseFormatter#format raises a NotImplementedError."""
formatter = base.BaseFormatter(options())
with pytest.raises(NotImplementedError):
formatter.format(
style_guide.Violation('A000', 'file.py', 1, 1, 'error text', None)
)
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)
) == ''
def test_disable_is_inline_ignored():
"""Verify that is_inline_ignored exits immediately if disabling NoQA."""
error = style_guide.Violation(
'E121', 'filename.py', 1, 1, 'error text', 'line')
with mock.patch('linecache.getline') as getline:
assert error.is_inline_ignored(True) is False
assert getline.called is False
def test_show_source_returns_nothing():
"""Verify Nothing.show_source returns None."""
formatter = default.Nothing(options())
error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')
assert formatter.show_source(error) is None
def test_handle_formats_the_error():
"""Verify that a formatter will call format from handle."""
formatter = FormatFormatter(options(show_source=False))
filemock = formatter.output_fd = mock.Mock()
error = style_guide.Violation(
code='A001',
filename='example.py',
line_number=1,
column_number=1,
text='Fake error',
physical_line='a = 1',
)
formatter.handle(error)
filemock.write.assert_called_once_with(repr(error) + '\n')
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)
output = formatter.show_source(error)
assert output == line1 + line2
def is_inline_ignored(style_guide, checker, result):
code, lineno, colno, text, line = result
error = Violation(code, checker.display_name,
lineno, (colno or 0) + 1, text, line)
if hasattr(error, 'is_inline_ignored'):
return error.is_inline_ignored(style_guide.options.disable_noqa)
# flake8 <3.4.0
return style_guide.is_inline_ignored(error)
def is_inline_ignored(self, disable_noqa):
return Violation.is_inline_ignored(self, disable_noqa)