Bug 1289805 - Refactor filterpaths to accept all lintargs, r?smacleod draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Tue, 09 Aug 2016 15:29:49 -0400
changeset 401282 73facf8f23450ffa8a674849cda880542ae9d969
parent 401281 2fee365b3914effa0765facf196b0c8f74f24c04
child 401283 7c0e2aad2004f41cd7a317dc8055b6d06099f24b
push id26420
push userahalberstadt@mozilla.com
push dateTue, 16 Aug 2016 20:20:10 +0000
reviewerssmacleod
bugs1289805
milestone51.0a1
Bug 1289805 - Refactor filterpaths to accept all lintargs, r?smacleod This makes it easier to pass configuration to the filterpaths method. In the future, 'root' and 'extensions' will both be used here. MozReview-Commit-ID: KYhC6SEySC3
python/mozlint/mozlint/pathutils.py
python/mozlint/mozlint/types.py
--- a/python/mozlint/mozlint/pathutils.py
+++ b/python/mozlint/mozlint/pathutils.py
@@ -53,30 +53,32 @@ class FilterPath(object):
         if b.startswith(a):
             return True
         return False
 
     def __repr__(self):
         return repr(self.path)
 
 
-def filterpaths(paths, include=None, exclude=None):
+def filterpaths(paths, linter, **lintargs):
     """Filters a list of paths.
 
-    Given a list of paths, and a list of include and exclude
-    directives, return the set of paths that should be linted.
+    Given a list of paths, and a linter definition plus extra
+    arguments, return the set of paths that should be linted.
 
     :param paths: A starting list of paths to possibly lint.
-    :param include: A list of include directives. May contain glob patterns.
-    :param exclude: A list of exclude directives. May contain glob patterns.
-    :returns: A tuple containing a list of file paths to lint, and a list
-              of file paths that should be excluded (but that the algorithm
-              was unable to apply).
+    :param linter: A linter definition.
+    :param lintargs: Extra arguments passed to the linter.
+    :returns: A list of file paths to lint.
     """
-    if not include and not exclude:
+    include = linter.get('include', [])
+    exclude = lintargs.get('exclude', [])
+    exclude.extend(linter.get('exclude', []))
+
+    if not lintargs.get('use_filters', True) or (not include and not exclude):
         return paths
 
     include = map(FilterPath, include or [])
     exclude = map(FilterPath, exclude or [])
 
     # Paths with and without globs will be handled separately,
     # pull them apart now.
     includepaths = [p for p in include if p.exists]
@@ -127,9 +129,11 @@ def filterpaths(paths, include=None, exc
         elif path.isdir:
             # If the specified path is a directory, use a
             # FileFinder to resolve all relevant globs.
             path.exclude = excludeglobs
             for pattern in includeglobs:
                 for p, f in path.finder.find(pattern.path):
                     keep.add(path.join(p))
 
-    return ([f.path for f in keep], [f.path for f in discard])
+    # Only pass paths we couldn't exclude here to the underlying linter
+    lintargs['exclude'] = [f.path for f in discard]
+    return [f.path for f in keep]
--- a/python/mozlint/mozlint/types.py
+++ b/python/mozlint/mozlint/types.py
@@ -20,27 +20,21 @@ class BaseType(object):
         """Run `linter` against `paths` with `lintargs`.
 
         :param paths: Paths to lint. Can be a file or directory.
         :param linter: Linter definition paths are being linted against.
         :param lintargs: External arguments to the linter not defined in
                          the definition, but passed in by a consumer.
         :returns: A list of :class:`~result.ResultContainer` objects.
         """
-        exclude = lintargs.get('exclude', [])
-        exclude.extend(linter.get('exclude', []))
-
-        if lintargs.get('use_filters', True):
-            paths, exclude = filterpaths(paths, linter.get('include'), exclude)
-
+        paths = filterpaths(paths, linter, **lintargs)
         if not paths:
             print("{}: no files to lint in specified paths".format(linter['name']))
             return
 
-        lintargs['exclude'] = exclude
         if self.batch:
             return self._lint(paths, linter, **lintargs)
 
         errors = []
         try:
             for p in paths:
                 result = self._lint(p, linter, **lintargs)
                 if result: