From 1b496c5fc37298d029c97782486ff420c99a4797 Mon Sep 17 00:00:00 2001 From: "George Y. Kussumoto" Date: Tue, 2 Oct 2018 12:17:46 -0300 Subject: [PATCH] Fix `check-useless-exclude` to consider types filter --- .../meta_hooks/check_useless_excludes.py | 5 ++- .../meta_hooks/check_useless_excludes_test.py | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pre_commit/meta_hooks/check_useless_excludes.py b/pre_commit/meta_hooks/check_useless_excludes.py index cdc556df..18b9f163 100644 --- a/pre_commit/meta_hooks/check_useless_excludes.py +++ b/pre_commit/meta_hooks/check_useless_excludes.py @@ -9,6 +9,7 @@ import pre_commit.constants as C from pre_commit import git from pre_commit.clientlib import load_config from pre_commit.clientlib import MANIFEST_HOOK_DICT +from pre_commit.commands.run import _filter_by_types def exclude_matches_any(filenames, include, exclude): @@ -39,8 +40,10 @@ def check_useless_excludes(config_file): # Not actually a manifest dict, but this more accurately reflects # the defaults applied during runtime hook = apply_defaults(hook, MANIFEST_HOOK_DICT) + types, exclude_types = hook['types'], hook['exclude_types'] + filtered_by_types = _filter_by_types(files, types, exclude_types) include, exclude = hook['files'], hook['exclude'] - if not exclude_matches_any(files, include, exclude): + if not exclude_matches_any(filtered_by_types, include, exclude): print( 'The exclude pattern {!r} for {} does not match any files' .format(exclude, hook['id']), diff --git a/tests/meta_hooks/check_useless_excludes_test.py b/tests/meta_hooks/check_useless_excludes_test.py index 1a03fb08..b2cc1873 100644 --- a/tests/meta_hooks/check_useless_excludes_test.py +++ b/tests/meta_hooks/check_useless_excludes_test.py @@ -51,6 +51,37 @@ def test_useless_exclude_for_hook(capsys, tempdir_factory): assert expected == out +def test_useless_exclude_with_types_filter(capsys, tempdir_factory): + config = { + 'repos': [ + { + 'repo': 'meta', + 'hooks': [ + { + 'id': 'check-useless-excludes', + 'exclude': '.pre-commit-config.yaml', + 'types': ['python'], + }, + ], + }, + ], + } + + repo = git_dir(tempdir_factory) + add_config_to_repo(repo, config) + + with cwd(repo): + assert check_useless_excludes.main(()) == 1 + + out, _ = capsys.readouterr() + out = out.strip() + expected = ( + "The exclude pattern '.pre-commit-config.yaml' for " + "check-useless-excludes does not match any files" + ) + assert expected == out + + def test_no_excludes(capsys, tempdir_factory): config = { 'repos': [