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"
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'
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
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
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})
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
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
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
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'])
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)
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
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
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
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'
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
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
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
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
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
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
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
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'
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
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()
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)
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
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'
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
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."""'
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'