예제 #1
0
def test_violation_is_in_diff(violation_file, violation_line, diff, expected):
    """Verify that we find violations within a diff."""
    violation = style_guide.Violation(
        'E001', violation_file, violation_line, 1, 'warning', 'line',
    )

    assert violation.is_in(diff) is expected
예제 #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.Violation('A000', 'file.py', 1, column, 'error', line)
    output = formatter.show_source(error)
    _, pointer = output.rsplit('\n', 1)
    assert pointer.count(' ') == (column - 1)
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.Violation("code", "file.py", 1, 1, "text", "1")

    assert formatter.format(error) == "file.py"
    assert formatter.format(error) is None
예제 #4
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.Violation('code', 'file.py', 1, 1, 'text', '1')

    assert formatter.format(error) == 'file.py'
    assert formatter.format(error) is None
예제 #5
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.Violation('code', 'file.py', 1, 1, 'text', 'l'))
    assert formatter.filenames_already_printed == {'file.py'}
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.Violation("code", "file.py", 1, 1, "text", "l")
    )
    assert formatter.filenames_already_printed == {"file.py"}
예제 #7
0
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
예제 #8
0
def test_is_inline_ignored(error_code, physical_line, expected_result):
    """Verify that we detect inline usage of ``# noqa``."""
    error = style_guide.Violation(
        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 error.is_inline_ignored(False) is expected_result
예제 #9
0
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
예제 #10
0
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,
    )
예제 #11
0
def test_violation_is_in_diff(violation_file, violation_line, diff, expected):
    """Verify that we find violations within a diff."""
    violation = style_guide.Violation(
        "E001",
        violation_file,
        violation_line,
        1,
        "warning",
        "line",
    )

    assert violation.is_in(diff) is expected
예제 #12
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.Violation(
        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)
예제 #13
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.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")
예제 #14
0
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
예제 #15
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.Violation("A000", "file.py", 1, 1, "error text",
                              None)) == "")
예제 #16
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.Violation("A000", "file.py", 1, 1, "error text",
                              "line")) == "")
예제 #17
0
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))
예제 #18
0
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
예제 #19
0
import unittest
import optparse
import os
import tempfile
from flake8_formatter_junit_xml import JUnitXmlFormatter
from flake8 import style_guide
from junit_xml import TestSuite, TestCase

filename = 'some/filename.py'
error = style_guide.Violation('A000', filename, 2, 1, 'wrong wrong wrong',
                              'import os')


def create_formatter(**kwargs):
    kwargs.setdefault('output_file', None)
    kwargs.setdefault('tee', False)
    return JUnitXmlFormatter(optparse.Values(kwargs))


class TestJunitXmlFormatter(unittest.TestCase):
    def test_beginning(self):
        f = create_formatter()
        f.beginning(filename)
        self.assertIsInstance(f.test_suites[filename], TestSuite)
        self.assertEqual('flake8.some/filename_py',
                         f.test_suites[filename].name)

    def test_handle(self):
        f = create_formatter()
        f.beginning(filename)
        f.handle(error)
예제 #20
0
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
예제 #21
0
def test_format_returns_nothing():
    """Verify Nothing.format returns None."""
    formatter = default.Nothing(options())
    error = style_guide.Violation('code', 'file.py', 1, 1, 'text', '1')

    assert formatter.format(error) is None
예제 #22
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.Violation('A000', 'file.py', 1, 1, 'error text',
                              None)) == ''
예제 #23
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.Violation('A000', 'file.py', 1, 1, 'error text',
                              'line')) == ''