Bug 1623339 - [lint.flake8] Fix path filtering bug when specifying subdirectory of excluded path, r=linter-reviewers,sylvestre

Differential Revision: https://phabricator.services.mozilla.com/D67335

--HG--
rename : tools/lint/test/files/flake8/subdir/exclude/bad.py => tools/lint/test/files/flake8/subdir/exclude/exclude_subdir/bad.py
extra : moz-landing-system : lando
This commit is contained in:
Andrew Halberstadt 2020-03-18 17:02:34 +00:00
parent 5712219535
commit f4ff3c2181
4 changed files with 23 additions and 20 deletions

View File

@ -1,7 +1,7 @@
Flake8
======
`Flake8 <https://flake8.readthedocs.io/en/latest/>`__ is a popular lint wrapper for python. Under the hood, it runs three other tools and
`Flake8 <https://flake8.pycqa.org/en/latest/index.html>`__ is a popular lint wrapper for python. Under the hood, it runs three other tools and
combines their results:
* `pep8 <http://pep8.readthedocs.io/en/latest/>`__ for checking style
@ -25,22 +25,12 @@ flake8.
Configuration
-------------
Only directories explicitly whitelisted will have flake8 run against them. To enable flake8 linting
in a source directory, it must be added to the ``include`` directive in ```tools/lint/flake8.lint``.
If you wish to exclude a subdirectory of an included one, you can add it to the ``exclude``
directive.
Path configuration is defined in the root `.flake8`_ file. Please update this file rather than
``tools/lint/flake8.yml`` if you need to exclude a new path. For an overview of the supported
configuration, see `flake8's documentation`_.
The default configuration file lives in ``topsrcdir/.flake8``. The default configuration can be
overridden for a given subdirectory by creating a new ``.flake8`` file in the subdirectory. Be warned
that ``.flake8`` files cannot inherit from one another, so all configuration you wish to keep must
be re-defined.
.. warning::
Only ``.flake8`` files that live in a directory that is explicitly included in the ``include``
directive will be considered. See `bug 1277851 <https://bugzilla.mozilla.org/show_bug.cgi?id=1277851>`__ for more details.
For an overview of the supported configuration, see `flake8's documentation <https://flake8.readthedocs.io/en/latest/config.html>`__.
.. _.flake8: https://searchfox.org/mozilla-central/source/.flake8
.. _flake8's documentation: https://flake8.pycqa.org/en/latest/user/configuration.html
Autofix
-------

View File

@ -118,11 +118,19 @@ def lint(paths, config, **lintargs):
tools/lint/mach_commands.py.
"""
# Ignore exclude rules if `--no-filter` was passed in.
config.setdefault('exclude', [])
if lintargs.get('use_filters', True):
config.setdefault('exclude', []).extend(self.options.exclude)
config['exclude'].extend(self.options.exclude)
# Since we use the root .flake8 file to store exclusions, we haven't
# properly filtered the paths through mozlint's `filterpaths` function
# yet. This mimics that though there could be other edge cases that are
# different. Maybe we should call `filterpaths` directly, though for
# now that doesn't appear to be necessary.
filtered = [p for p in paths if not any(p.startswith(e) for e in config['exclude'])]
self.options.exclude = None
self.args = self.args + list(expand_exclusions(paths, config, root))
self.args = self.args + list(expand_exclusions(filtered, config, root))
if not self.args:
raise NothingToLint

View File

@ -0,0 +1,5 @@
# Unused import
import distutils
print("This is a line that is over 80 characters but under 100. It shouldn't fail.")
print("This is a line that is over not only 80, but 100 characters. It should most certainly cause a failure.")

View File

@ -65,7 +65,7 @@ foo = ['A list of strings', 'that go over 80 characters', 'to test if autopep8 f
def test_lint_excluded_file(lint, paths, config):
# First file is globally excluded, second one is from .flake8 config.
files = paths('bad.py', 'subdir/exclude/bad.py')
files = paths('bad.py', 'subdir/exclude/bad.py', 'subdir/exclude/exclude_subdir')
config['exclude'] = paths('bad.py')
results = lint(files, config)
print(results)
@ -97,7 +97,7 @@ def test_lint_excluded_file_with_glob(lint, paths, config):
def test_lint_excluded_file_with_no_filter(lint, paths, config):
results = lint(paths('subdir/exclude'), use_filters=False)
print(results)
assert len(results) == 2
assert len(results) == 4
def test_lint_uses_custom_extensions(lint, paths):