Bug 1392787 - [mozlint] Fix bugs in path filtering and add a test, r=jmaher

MozReview-Commit-ID: LG47ASBMA17

--HG--
extra : rebase_source : d782ad2f17716f857f3da2d43e1370ba4db2f2bd
This commit is contained in:
Andrew Halberstadt 2017-08-23 06:33:06 -04:00
parent dd07703803
commit 9fc7ff1913
11 changed files with 98 additions and 4 deletions

View File

@ -42,7 +42,7 @@ class FilterPath(object):
return os.path.isdir(self.path)
def join(self, *args):
return FilterPath(os.path.join(self, *args))
return FilterPath(os.path.join(self.path, *args))
def match(self, patterns):
return any(mozpath.match(self.path, pattern.path) for pattern in patterns)
@ -82,7 +82,7 @@ def filterpaths(paths, linter, **lintargs):
return paths
def normalize(path):
if not os.path.isabs(path):
if '*' not in path and not os.path.isabs(path):
path = os.path.join(root, path)
return FilterPath(path)
@ -139,16 +139,18 @@ def filterpaths(paths, linter, **lintargs):
# If the specified path is a file it must be both
# matched by an include directive and not matched
# by an exclude directive.
if not path.match(includeglobs):
if not path.match(includeglobs) or any(e.contains(path) for e in excludepaths):
continue
keep.add(path)
elif path.isdir:
# If the specified path is a directory, use a
# FileFinder to resolve all relevant globs.
path.exclude = [e.path for e in excludeglobs]
path.exclude = [os.path.relpath(e.path, root) for e in exclude]
for pattern in includeglobs:
for p, f in path.finder.find(pattern.path):
if extensions and os.path.splitext(p)[1][1:] not in extensions:
continue
keep.add(path.join(p))
# Only pass paths we couldn't exclude here to the underlying linter

View File

View File

View File

View File

View File

View File

View File

@ -2,6 +2,7 @@
subsuite = mozlint, os == "linux"
[test_cli.py]
[test_filterpaths.py]
[test_formatters.py]
[test_parser.py]
[test_roller.py]

View File

@ -0,0 +1,91 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import pytest
from mozlint import pathutils
here = os.path.abspath(os.path.dirname(__file__))
@pytest.fixture
def filterpaths():
lintargs = {
'root': os.path.join(here, 'filter'),
'use_filters': True,
}
os.chdir(lintargs['root'])
def inner(paths, include, exclude, extensions=None, **kwargs):
linter = {
'include': include,
'exclude': exclude,
'extensions': extensions,
}
lintargs.update(kwargs)
return pathutils.filterpaths(paths, linter, **lintargs)
return inner
def assert_paths(a, b):
assert set(a) == set([os.path.normpath(t) for t in b])
def test_no_filter(filterpaths):
args = {
'paths': ['a.py', 'subdir1/b.py'],
'include': ['.'],
'exclude': ['**/*.py'],
'use_filters': False,
}
paths = filterpaths(**args)
assert set(paths) == set(args['paths'])
def test_extensions(filterpaths):
args = {
'paths': ['a.py', 'a.js', 'subdir2'],
'include': ['**'],
'exclude': [],
'extensions': ['py'],
}
paths = filterpaths(**args)
assert_paths(paths, ['a.py', 'subdir2/c.py'])
def test_include_exclude(filterpaths):
args = {
'paths': ['a.js', 'subdir1/subdir3/d.js'],
'include': ['.'],
'exclude': ['subdir1'],
}
paths = filterpaths(**args)
assert_paths(paths, ['a.js'])
args['include'] = ['subdir1/subdir3']
paths = filterpaths(**args)
assert_paths(paths, ['subdir1/subdir3/d.js'])
args = {
'paths': ['.'],
'include': ['**/*.py'],
'exclude': ['**/c.py', 'subdir1/subdir3'],
}
paths = filterpaths(**args)
assert_paths(paths, ['a.py', 'subdir1/b.py'])
args['paths'] = ['a.py', 'a.js', 'subdir1/b.py', 'subdir2/c.py', 'subdir1/subdir3/d.py']
paths = filterpaths(**args)
assert_paths(paths, ['a.py', 'subdir1/b.py'])
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))