Ejemplo n.º 1
0
def test_only_returns_a_string_once_from_format():
    """Verify format ignores the second error with the same filename."""
    formatter = default.FilenameOnly(options())
    error = style_guide.Error('code', 'file.py', 1, 1, 'text', '1')

    assert formatter.format(error) == 'file.py'
    assert formatter.format(error) is None
Ejemplo n.º 2
0
def test_show_source_updates_physical_line_appropriately(line, column):
    """Ensure the error column is appropriately indicated."""
    formatter = base.BaseFormatter(options(show_source=True))
    error = style_guide.Error('A000', 'file.py', 1, column, 'error', line)
    output = formatter.show_source(error)
    _, pointer = output.rsplit('\n', 1)
    assert pointer.count(' ') == column
Ejemplo n.º 3
0
def test_caches_filenames_already_printed():
    """Verify we cache filenames when we format them."""
    formatter = default.FilenameOnly(options())
    assert formatter.filenames_already_printed == set()

    formatter.format(style_guide.Error('code', 'file.py', 1, 1, 'text', 'l'))
    assert formatter.filenames_already_printed == {'file.py'}
Ejemplo n.º 4
0
def make_error(**kwargs):
    """Create errors with a bunch of default values."""
    return style_guide.Error(
        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,
    )
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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.Error(
        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')
Ejemplo n.º 9
0
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.Error('A000', 'file.py', 1, 1, 'error text',
                          'line')) is None
Ejemplo n.º 10
0
def test_show_source_returns_nothing():
    """Verify Nothing.show_source returns None."""
    formatter = default.Nothing(options())
    error = style_guide.Error('code', 'file.py', 1, 1, 'text', '1')

    assert formatter.show_source(error) is None
Ejemplo n.º 11
0
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.Error('A000', 'file.py', 1, 1, 'error text', None)) is ''
from flake8 import style_guide
from flake8.formatting import default

from flake8_formatter_abspath import AbsolutePathFormatter

filename = './some/file.py'
absfilename = os.path.abspath(filename)

try:
    # 3.4.0
    error = style_guide.Violation('A000', filename, 1, 1, 'wrong wrong wrong',
                                  'import os')
except AttributeError:
    # 3.3.0
    error = style_guide.Error('A000', filename, 1, 1, 'wrong wrong wrong',
                              'import os')


def options(**kwargs):
    """Create an optparse.Values instance."""
    kwargs.setdefault('output_file', None)
    kwargs.setdefault('tee', False)
    return optparse.Values(kwargs)


def verify_formatter(formatter, filename):
    assert formatter.format(error) == '{}:1:1: A000 wrong wrong wrong'.format(
        filename)


def test_abspath_formatter():