Ejemplo n.º 1
0
def test_read_lines_ignores_empty_display_name(
    stdin_get_value,
    default_options,
):
    """Verify that when processing stdin we use a display name if present."""
    stdin_get_value.return_value = ""
    default_options.stdin_display_name = ""
    file_processor = processor.FileProcessor("-", default_options)
    assert file_processor.filename == "stdin"
Ejemplo n.º 2
0
def test_read_lines_ignores_empty_display_name(stdin_get_value):
    """Verify that when processing stdin we use a display name if present."""
    stdin_value = mock.Mock()
    stdin_value.splitlines.return_value = []
    stdin_get_value.return_value = stdin_value
    file_processor = processor.FileProcessor('-', options_from(
        stdin_display_name=''
    ))
    assert file_processor.filename == 'stdin'
Ejemplo n.º 3
0
def test_visited_new_blank_line(default_options):
    """Verify we update the number of blank lines seen."""
    file_processor = processor.FileProcessor('-',
                                             default_options,
                                             lines=['a = 1\n'])

    assert file_processor.blank_lines == 0
    file_processor.visited_new_blank_line()
    assert file_processor.blank_lines == 1
Ejemplo n.º 4
0
def test_noqa_line_for_no_eol_at_end_of_file(default_options):
    """Verify that we properly handle noqa line at the end of the file."""
    src = 'from foo \\\nimport bar'  # no end of file newline
    lines = src.splitlines(True)
    file_processor = processor.FileProcessor('-', default_options, lines=lines)

    l_1_2 = 'from foo \\\nimport bar'
    assert file_processor.noqa_line_for(1) == l_1_2
    assert file_processor.noqa_line_for(2) == l_1_2
Ejemplo n.º 5
0
def test_keyword_arguments_for_does_not_handle_attribute_errors(
        default_options,
):
    """Verify we re-raise AttributeErrors."""
    file_processor = processor.FileProcessor('-', default_options, lines=[
        'Line 1',
    ])

    with pytest.raises(AttributeError):
        file_processor.keyword_arguments_for({'fake': True})
Ejemplo n.º 6
0
def test_split_line(unsplit_line, expected_lines, default_options):
    """Verify the token line spliting."""
    file_processor = processor.FileProcessor('-', default_options, lines=[
        'Line 1',
    ])

    actual_lines = list(file_processor.split_line((1, unsplit_line)))
    assert expected_lines == actual_lines

    assert len(actual_lines) == file_processor.line_number
Ejemplo n.º 7
0
def test_check_physical_error(error_code, line, expected_indent_char):
    """Verify we update the indet char for the appropriate error code."""
    file_processor = processor.FileProcessor('-',
                                             options_from(),
                                             lines=[
                                                 'Line 1',
                                             ])

    file_processor.check_physical_error(error_code, line)
    assert file_processor.indent_char == expected_indent_char
Ejemplo n.º 8
0
def test_keyword_arguments_for(params, args, expected_kwargs):
    """Verify the keyword args are generated properly."""
    file_processor = processor.FileProcessor('-',
                                             options_from(),
                                             lines=[
                                                 'Line 1',
                                             ])
    kwargs_for = file_processor.keyword_arguments_for

    assert kwargs_for(params, args) == expected_kwargs
Ejemplo n.º 9
0
def test_keyword_arguments_for_does_not_handle_attribute_errors():
    """Verify we re-raise AttributeErrors."""
    file_processor = processor.FileProcessor('-',
                                             options_from(),
                                             lines=[
                                                 'Line 1',
                                             ])

    with pytest.raises(AttributeError):
        file_processor.keyword_arguments_for(['fake'])
Ejemplo n.º 10
0
def test_line_for(default_options):
    """Verify we grab the correct line from the cached lines."""
    file_processor = processor.FileProcessor('-', default_options, lines=[
        'Line 1',
        'Line 2',
        'Line 3',
    ])

    for i in range(1, 4):
        assert file_processor.line_for(i) == 'Line {0}'.format(i)
Ejemplo n.º 11
0
def test_next_line(default_options):
    """Verify we update the file_processor state for each new line."""
    file_processor = processor.FileProcessor('-', default_options, lines=[
        'Line 1',
        'Line 2',
        'Line 3',
    ])

    for i in range(1, 4):
        assert file_processor.next_line() == 'Line {}'.format(i)
        assert file_processor.line_number == i
Ejemplo n.º 12
0
def test_keyword_arguments_for(params, args, expected_kwargs, default_options):
    """Verify the keyword args are generated properly."""
    file_processor = processor.FileProcessor(
        "-",
        default_options,
        lines=[
            "Line 1",
        ],
    )
    kwargs_for = file_processor.keyword_arguments_for

    assert kwargs_for(params, args) == expected_kwargs
Ejemplo n.º 13
0
 def _make_processor(self) -> Optional[processor.FileProcessor]:
     try:
         return processor.FileProcessor(self.filename, self.options)
     except OSError as e:
         # If we can not read the file due to an IOError (e.g., the file
         # does not exist or we do not have the permissions to open it)
         # then we need to format that exception for the user.
         # NOTE(sigmavirus24): Historically, pep8 has always reported this
         # as an E902. We probably *want* a better error code for this
         # going forward.
         self.report("E902", 0, 0, f"{type(e).__name__}: {e}")
         return None
Ejemplo n.º 14
0
def test_noqa_line_for(default_options):
    """Verify we grab the correct line from the cached lines."""
    file_processor = processor.FileProcessor('-',
                                             default_options,
                                             lines=[
                                                 'Line 1\n',
                                                 'Line 2\n',
                                                 'Line 3\n',
                                             ])

    for i in range(1, 4):
        assert file_processor.noqa_line_for(i) == f'Line {i}\n'
Ejemplo n.º 15
0
def test_inside_multiline():
    """Verify we update the line number and reset multiline."""
    file_processor = processor.FileProcessor('-',
                                             options_from(),
                                             lines=['a = 1\n'])

    assert file_processor.multiline is False
    assert file_processor.line_number == 0
    with file_processor.inside_multiline(10):
        assert file_processor.multiline is True
        assert file_processor.line_number == 10

    assert file_processor.multiline is False
Ejemplo n.º 16
0
def test_inside_multiline(default_options):
    """Verify we update the line number and reset multiline."""
    file_processor = processor.FileProcessor("-",
                                             default_options,
                                             lines=["a = 1\n"])

    assert file_processor.multiline is False
    assert file_processor.line_number == 0
    with file_processor.inside_multiline(10):
        assert file_processor.multiline is True
        assert file_processor.line_number == 10

    assert file_processor.multiline is False
Ejemplo n.º 17
0
def test_next_logical_line_updates_the_previous_logical_line():
    """Verify that we update our tracking of the previous logical line."""
    file_processor = processor.FileProcessor('-',
                                             options_from(),
                                             lines=['a = 1\n'])

    file_processor.indent_level = 1
    file_processor.logical_line = 'a = 1'
    assert file_processor.previous_logical == ''
    assert file_processor.previous_indent_level is 0

    file_processor.next_logical_line()
    assert file_processor.previous_logical == 'a = 1'
    assert file_processor.previous_indent_level == 1
Ejemplo n.º 18
0
def test_next_logical_line_updates_the_previous_logical_line(default_options):
    """Verify that we update our tracking of the previous logical line."""
    file_processor = processor.FileProcessor("-",
                                             default_options,
                                             lines=["a = 1\n"])

    file_processor.indent_level = 1
    file_processor.logical_line = "a = 1"
    assert file_processor.previous_logical == ""
    assert file_processor.previous_indent_level == 0

    file_processor.next_logical_line()
    assert file_processor.previous_logical == "a = 1"
    assert file_processor.previous_indent_level == 1
Ejemplo n.º 19
0
 def _make_processor(self):
     try:
         return processor.FileProcessor(self.filename, self.options)
     except IOError:
         # If we can not read the file due to an IOError (e.g., the file
         # does not exist or we do not have the permissions to open it)
         # then we need to format that exception for the user.
         # NOTE(sigmavirus24): Historically, pep8 has always reported this
         # as an E902. We probably *want* a better error code for this
         # going forward.
         (exc_type, exception) = sys.exc_info()[:2]
         message = "{0}: {1}".format(exc_type.__name__, exception)
         self.report("E902", 0, 0, message)
         return None
Ejemplo n.º 20
0
def test_next_line(default_options):
    """Verify we update the file_processor state for each new line."""
    file_processor = processor.FileProcessor(
        "-",
        default_options,
        lines=[
            "Line 1",
            "Line 2",
            "Line 3",
        ],
    )

    for i in range(1, 4):
        assert file_processor.next_line() == f"Line {i}"
        assert file_processor.line_number == i
Ejemplo n.º 21
0
def test_split_line(unsplit_line, expected_lines, default_options):
    """Verify the token line splitting."""
    file_processor = processor.FileProcessor(
        "-",
        default_options,
        lines=[
            "Line 1",
        ],
    )

    token = (1, unsplit_line, (0, 0), (0, 0), "")
    actual_lines = list(file_processor.split_line(token))
    assert expected_lines == actual_lines

    assert len(actual_lines) == file_processor.line_number
Ejemplo n.º 22
0
def test_strip_utf_bom(first_line):
    r"""Verify that we strip '\xEF\xBB\xBF' from the first line."""
    lines = [first_line]
    file_processor = processor.FileProcessor('-', options_from(), lines[:])
    assert file_processor.lines != lines
    assert file_processor.lines[0] == '"""Module docstring."""\n'
Ejemplo n.º 23
0
def test_read_lines_splits_lines():
    """Verify that read_lines splits the lines of the file."""
    file_processor = processor.FileProcessor(__file__, options_from())
    lines = file_processor.lines
    assert len(lines) > 5
    assert '"""Tests for the FileProcessor class."""\n' in lines
Ejemplo n.º 24
0
def test_read_lines_from_stdin(stdin_get_value, default_options):
    """Verify that we use our own utility function to retrieve stdin."""
    stdin_get_value.return_value = ''
    processor.FileProcessor('-', default_options)
    stdin_get_value.assert_called_once_with()
Ejemplo n.º 25
0
def test_nonexistent_file(default_options):
    """Verify that FileProcessor raises IOError when a file does not exist."""
    with pytest.raises(IOError):
        processor.FileProcessor("foobar.py", default_options)
Ejemplo n.º 26
0
def _lines_from_file(tmpdir, contents, options):
    f = tmpdir.join('f.py')
    # be careful to write the bytes exactly to avoid newline munging
    f.write_binary(contents)
    return processor.FileProcessor(f.strpath, options).lines
Ejemplo n.º 27
0
def test_read_lines_uses_display_name(stdin_get_value, default_options):
    """Verify that when processing stdin we use a display name if present."""
    default_options.stdin_display_name = 'display_name.py'
    stdin_get_value.return_value = ''
    file_processor = processor.FileProcessor('-', default_options)
    assert file_processor.filename == 'display_name.py'
Ejemplo n.º 28
0
def test_should_ignore_file(lines, expected):
    """Verify that we ignore a file if told to."""
    file_processor = processor.FileProcessor('-', options_from(), lines)
    assert file_processor.should_ignore_file() is expected
Ejemplo n.º 29
0
def test_read_lines_splits_lines(default_options):
    """Verify that read_lines splits the lines of the file."""
    file_processor = processor.FileProcessor(__file__, default_options)
    lines = file_processor.lines
    assert len(lines) > 5
    assert lines[0].strip() == '"""Tests for the FileProcessor class."""'
Ejemplo n.º 30
0
def test_stdin_filename_attribute(stdin_get_value, default_options):
    """Verify that we update the filename attribute."""
    stdin_get_value.return_value = ''
    file_processor = processor.FileProcessor('-', default_options)
    assert file_processor.filename == 'stdin'