Example #1
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
Example #2
0
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"
    filemock = mock.Mock()

    formatter = base.BaseFormatter(options(tee=tee))
    formatter.output_fd = filemock

    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"),
            ]
        else:
            assert not print_func.called

    assert filemock.write.called is True
    assert filemock.write.call_count == 2
    assert filemock.write.mock_calls == [
        mock.call(line + formatter.newline),
        mock.call(source + formatter.newline),
    ]
Example #3
0
def test_stop():
    """Verify we close open file objects."""
    filemock = mock.Mock()
    formatter = base.BaseFormatter(options())
    formatter.output_fd = filemock
    formatter.stop()

    filemock.close.assert_called_once_with()
    assert formatter.output_fd is None
Example #4
0
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):
        formatter.start()

    if filename is None:
        assert mock_open.called is False
    else:
        mock_open.assert_called_once_with(filename, "a")
Example #5
0
def test_write_uses_print(print_function):
    """Verify that we use the print function without an output file."""
    line = "Something to write"
    source = "source"

    formatter = base.BaseFormatter(options())
    formatter.write(line, source)

    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"),
    ]
Example #6
0
def test_write_uses_an_output_file():
    """Verify that we use the output file when it's present."""
    line = 'Something to write'
    source = 'source'
    filemock = mock.Mock()

    formatter = base.BaseFormatter(options())
    formatter.output_fd = filemock
    formatter.write(line, source)

    assert filemock.write.called is True
    assert filemock.write.call_count == 2
    assert filemock.write.mock_calls == [
        mock.call(line + formatter.newline),
        mock.call(source + formatter.newline),
    ]
Example #7
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
Example #8
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)) == "")
Example #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.Violation("A000", "file.py", 1, 1, "error text",
                              "line")) == "")
Example #10
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))
Example #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.Violation('A000', 'file.py', 1, 1, 'error text',
                              None)) == ''
Example #12
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')) == ''
Example #13
0
def test_format_needs_to_be_implemented():
    """Ensure BaseFormatter#format raises a NotImplementedError."""
    formatter = base.BaseFormatter(options())
    with pytest.raises(NotImplementedError):
        formatter.format('foo')