flake8/tests/unit/test_file_processor.py

60 lines
2.1 KiB
Python

"""Tests for the FileProcessor class."""
import optparse
from flake8 import processor
import mock
import pytest
def options_from(**kwargs):
"""Generate a Values instances with our kwargs."""
kwargs.setdefault('hang_closing', True)
kwargs.setdefault('max_line_length', 79)
kwargs.setdefault('verbose', False)
return optparse.Values(kwargs)
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
@pytest.mark.parametrize('first_line', [
'\xEF\xBB\xBF"""Module docstring."""\n',
u'\uFEFF"""Module docstring."""\n',
])
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'
@pytest.mark.parametrize('lines, expected', [
(['\xEF\xBB\xBF"""Module docstring."""\n'], False),
([u'\uFEFF"""Module docstring."""\n'], False),
(['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False),
(['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True),
(['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True),
(['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True),
])
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
@mock.patch('flake8.utils.stdin_get_value')
def test_read_lines_from_stdin(stdin_get_value):
"""Verify that we use our own utility function to retrieve stdin."""
stdin_value = mock.Mock()
stdin_value.splitlines.return_value = []
stdin_get_value.return_value = stdin_value
file_processor = processor.FileProcessor('-', options_from())
stdin_get_value.assert_called_once_with()
stdin_value.splitlines.assert_called_once_with(True)