Bug 1533152 - [flake8] Make sure glob patterns are honoured when passing files directly, r=andi
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Tue, 12 Mar 2019 15:08:41 +0000
changeset 524564 aaf573c47a9c2edf5eba678974d9745e21260acd
parent 524563 406c9fd1f0b19d2fd47cfd096ab3de2e2c846bf9
child 524565 3aa21d7302e79b1e9324ea4933c719f200de2ab1
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersandi
bugs1533152
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1533152 - [flake8] Make sure glob patterns are honoured when passing files directly, r=andi Flake8 ignores the 'exclude' section of the .flake8.yml if you pass in a direct path to a file. To get around this we have some custom logic to handle these exclusions for us, but this custom logic didn't account for globs. Differential Revision: https://phabricator.services.mozilla.com/D23145
python/mozlint/mozlint/pathutils.py
tools/lint/test/conftest.py
tools/lint/test/test_flake8.py
--- a/python/mozlint/mozlint/pathutils.py
+++ b/python/mozlint/mozlint/pathutils.py
@@ -266,19 +266,23 @@ def expand_exclusions(paths, config, roo
         if os.path.isabs(path):
             return path
         return mozpath.join(root, path)
 
     exclude = map(normalize, config.get('exclude', []))
     for path in paths:
         path = mozpath.normsep(path)
         if os.path.isfile(path):
-            if not any(path.startswith(e) for e in exclude):
-                yield path
-            continue
+            if any(path.startswith(e) for e in exclude if '*' not in e):
+                continue
+
+            if any(mozpath.match(path, e) for e in exclude if '*' in e):
+                continue
+
+            yield path
 
         ignore = [e[len(path):].lstrip('/') for e in exclude
                   if mozpath.commonprefix((path, e)) == path]
         finder = FileFinder(path, ignore=ignore)
 
         _, ext = os.path.splitext(path)
         ext.lstrip('.')
 
--- a/tools/lint/test/conftest.py
+++ b/tools/lint/test/conftest.py
@@ -36,17 +36,17 @@ def paths(root):
     """
     def _inner(*paths):
         if not paths:
             return [root]
         return [os.path.normpath(os.path.join(root, p)) for p in paths]
     return _inner
 
 
-@pytest.fixture(scope='module')
+@pytest.fixture
 def config(request):
     """Finds, loads and returns the config for the linter name specified by the
     LINTER global variable in the calling module.
 
     This implies that each test file (that uses this fixture) should only be
     used to test a single linter. If no LINTER variable is defined, the test
     will fail.
     """
@@ -55,29 +55,29 @@ def config(request):
 
     name = request.module.LINTER
     config_path = os.path.join(lintdir, '{}.yml'.format(name))
     parser = Parser(build.topsrcdir)
     # TODO Don't assume one linter per yaml file
     return parser.parse(config_path)[0]
 
 
-@pytest.fixture(scope='module', autouse=True)
+@pytest.fixture(autouse=True)
 def run_setup(config):
     """Make sure that if the linter named in the LINTER global variable has a
     setup function, it gets called before running the tests.
     """
     if 'setup' not in config:
         return
 
     func = findobject(config['setup'])
     func(build.topsrcdir)
 
 
-@pytest.fixture(scope='module')
+@pytest.fixture
 def lint(config, root):
     """Find and return the 'lint' function for the external linter named in the
     LINTER global variable.
 
     This will automatically pass in the 'config' and 'root' arguments if not
     specified.
     """
     try:
--- a/tools/lint/test/test_flake8.py
+++ b/tools/lint/test/test_flake8.py
@@ -74,15 +74,29 @@ def test_lint_excluded_file(lint, paths,
     # Make sure excludes also apply when running from a different cwd.
     cwd = paths('subdir')[0]
     os.chdir(cwd)
 
     results = lint(paths('subdir/exclude'))
     assert len(results) == 0
 
 
+def test_lint_excluded_file_with_glob(lint, paths, config):
+    config['exclude'] = paths('ext/*.configure')
+
+    files = paths('ext')
+    results = lint(files, config)
+    print(results)
+    assert len(results) == 0
+
+    files = paths('ext/bad.configure')
+    results = lint(files, config)
+    print(results)
+    assert len(results) == 0
+
+
 def test_lint_uses_custom_extensions(lint, paths):
     assert len(lint(paths('ext'))) == 1
     assert len(lint(paths('ext/bad.configure'))) == 1
 
 
 if __name__ == '__main__':
     mozunit.main()