Lint Python files with a shebang

When a pattern is not passed to flake8 (--filename), look for all files
that end with .py as well as extension-less files that start with a
Python shebang. Helps project lint scripts that may not have an
extension.

Fixes #409
This commit is contained in:
Jon Dufresne 2018-06-06 12:25:26 -07:00
parent a2b7a7e4c5
commit 36a70fd110
4 changed files with 79 additions and 3 deletions

View file

@ -1,5 +1,6 @@
"""Tests for the Manager object for FileCheckers."""
import errno
import os
import mock
import pytest
@ -70,3 +71,57 @@ def test_make_checkers():
for file_checker in manager.checkers:
assert file_checker.filename in files
def test_make_checkers_shebang():
"""Verify that extension-less files with a Python shebang are checked."""
style_guide = style_guide_mock(
filename=[],
exclude=[],
)
checkplugins = mock.Mock()
checkplugins.to_dictionary.return_value = {
'ast_plugins': [],
'logical_line_plugins': [],
'physical_line_plugins': [],
}
with mock.patch('flake8.checker.multiprocessing', None):
manager = checker.Manager(style_guide, [], checkplugins)
path = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', 'fixtures')
)
manager.make_checkers([path])
filenames = [
os.path.relpath(file_checker.filename, path)
for file_checker in manager.checkers
]
assert 'example-code/script' in filenames
def test_make_checkers_explicit_pattern_ignore_shebang():
"""Verify that shebangs are ignored when passing a pattern."""
style_guide = style_guide_mock(
filename=['*.py'],
exclude=[],
)
checkplugins = mock.Mock()
checkplugins.to_dictionary.return_value = {
'ast_plugins': [],
'logical_line_plugins': [],
'physical_line_plugins': [],
}
with mock.patch('flake8.checker.multiprocessing', None):
manager = checker.Manager(style_guide, [], checkplugins)
path = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', 'fixtures')
)
manager.make_checkers([path])
filenames = [
os.path.relpath(file_checker.filename, path)
for file_checker in manager.checkers
]
assert 'example-code/script' not in filenames