Merge branch 'extend-exclude' into 'master'

support extend-exclude Fixes #535

Closes #535

See merge request pycqa/flake8!315
This commit is contained in:
Anthony Sottile 2019-07-08 20:12:47 +00:00
commit 65262dc597
6 changed files with 70 additions and 9 deletions

View file

@ -257,6 +257,38 @@ Options and their Descriptions
__pycache__ __pycache__
.. option:: --extend-exclude=<patterns>
:ref:`Go back to index <top>`
.. versionadded:: 3.8.0
Provide a comma-separated list of glob patterns to add to the list of excluded ones.
Similar considerations as in :option:`--exclude` apply here with regard to the
value.
The difference to the :option:`--exclude` option is, that this option can be
used to selectively add individual patterns without overriding the default
list entirely.
Command-line example:
.. prompt:: bash
flake8 --extend-exclude=legacy/,vendor/ dir/
This **can** be specified in config files.
Example config file usage:
.. code-block:: ini
extend-exclude =
legacy/,
vendor/
extend-exclude = legacy/,vendor/
.. option:: --filename=<patterns> .. option:: --filename=<patterns>
:ref:`Go back to index <top>` :ref:`Go back to index <top>`

View file

@ -1,6 +1,7 @@
"""Checker Manager and Checker classes.""" """Checker Manager and Checker classes."""
import collections import collections
import errno import errno
import itertools
import logging import logging
import signal import signal
import tokenize import tokenize
@ -79,6 +80,9 @@ class Manager(object):
"physical lines": 0, "physical lines": 0,
"tokens": 0, "tokens": 0,
} }
self.exclude = tuple(
itertools.chain(self.options.exclude, self.options.extend_exclude),
)
def _process_statistics(self): def _process_statistics(self):
for checker in self.checkers: for checker in self.checkers:
@ -187,7 +191,7 @@ class Manager(object):
return utils.matches_filename( return utils.matches_filename(
path, path,
patterns=self.options.exclude, patterns=self.exclude,
log_message='"%(path)s" has %(whether)sbeen excluded', log_message='"%(path)s" has %(whether)sbeen excluded',
logger=LOG, logger=LOG,
) )

View file

@ -14,6 +14,7 @@ def register_default_options(option_manager):
- ``--count`` - ``--count``
- ``--diff`` - ``--diff``
- ``--exclude`` - ``--exclude``
- ``--extend-exclude``
- ``--filename`` - ``--filename``
- ``--format`` - ``--format``
- ``--hang-closing`` - ``--hang-closing``
@ -85,6 +86,16 @@ def register_default_options(option_manager):
" (Default: %default)", " (Default: %default)",
) )
add_option(
"--extend-exclude",
metavar="patterns",
default="",
parse_from_config=True,
comma_separated_list=True,
help="Comma-separated list of files or directories to add to the list"
" of excluded ones."
)
add_option( add_option(
"--filename", "--filename",
metavar="patterns", metavar="patterns",

View file

@ -206,8 +206,7 @@ def test_report_order(results, expected_order):
file_checker.results = results file_checker.results = results
file_checker.display_name = 'placeholder' file_checker.display_name = 'placeholder'
style_guide = mock.Mock(spec=['options']) style_guide = mock.MagicMock(spec=['options', 'processing_file'])
style_guide.processing_file = mock.MagicMock()
# Create a placeholder manager without arguments or plugins # Create a placeholder manager without arguments or plugins
# Just add one custom file checker which just provides the results # Just add one custom file checker which just provides the results

View file

@ -60,6 +60,22 @@ t.py:2:1: F401 'sys' imported but unused
assert err == '' assert err == ''
def test_extend_exclude(tmpdir, capsys):
"""Ensure that `flake8 --extend-exclude` works."""
for d in ['project', 'vendor', 'legacy', '.git', '.tox', '.hg']:
tmpdir.mkdir(d).join('t.py').write('import os\nimport sys\n')
with tmpdir.as_cwd():
application.Application().run(['--extend-exclude=vendor,legacy'])
out, err = capsys.readouterr()
assert out == '''\
./project/t.py:1:1: F401 'os' imported but unused
./project/t.py:2:1: F401 'sys' imported but unused
'''
assert err == ''
def test_malformed_per_file_ignores_error(tmpdir, capsys): def test_malformed_per_file_ignores_error(tmpdir, capsys):
"""Test the error message for malformed `per-file-ignores`.""" """Test the error message for malformed `per-file-ignores`."""
setup_cfg = '''\ setup_cfg = '''\

View file

@ -7,13 +7,12 @@ import pytest
from flake8 import checker from flake8 import checker
def style_guide_mock(**kwargs): def style_guide_mock():
"""Create a mock StyleGuide object.""" """Create a mock StyleGuide object."""
kwargs.setdefault('diff', False) return mock.MagicMock(**{
kwargs.setdefault('jobs', '4') 'options.diff': False,
style_guide = mock.Mock() 'options.jobs': '4',
style_guide.options = mock.Mock(**kwargs) })
return style_guide
def _parallel_checker_manager(): def _parallel_checker_manager():