Fix types bug by using FrozenSet#intersection

Fixes a bug where files are not being recognized based on their `types`
using configuration file.

In the case of using `types: ['bash', 'sh', 'shell']` in
pre-commit-config, those filetypes will not be found, resulting in "no
files found" being reported with pre-commit. This persists even when
using --all-files flag.

Intersection produces predictable results in Python 2.7 and 3.x.
This commit is contained in:
Zander Hill 2018-05-03 12:08:34 +01:00
parent 29d66f470d
commit ff66c20c6e
2 changed files with 7 additions and 4 deletions

View file

@ -53,10 +53,12 @@ def _filter_by_types(filenames,
exclude_types, exclude_types,
get_tags=tags_from_path): get_tags=tags_from_path):
types, exclude_types = frozenset(types), frozenset(exclude_types) types, exclude_types = frozenset(types), frozenset(exclude_types)
valid_types = types - exclude_types
ret = [] ret = []
for filename in filenames: for filename in filenames:
tags = get_tags(filename) tags = frozenset(get_tags(filename))
if tags >= types and not tags & exclude_types: if len(valid_types.intersection(tags)) > 0:
ret.append(filename) ret.append(filename)
return tuple(ret) return tuple(ret)

View file

@ -834,15 +834,16 @@ def test_include_exclude_exclude_removes_files(some_filenames):
ret = _filter_by_include_exclude(some_filenames, '', r'\.py$') ret = _filter_by_include_exclude(some_filenames, '', r'\.py$')
assert ret == {'.pre-commit-hooks.yaml'} assert ret == {'.pre-commit-hooks.yaml'}
def get_tags_stub(interpreter): def get_tags_stub(interpreter):
return lambda x: tags_from_interpreter(interpreter) return lambda x: tags_from_interpreter(interpreter)
@pytest.mark.current
def test_filter_by_types_for_bash_by_interpreter(): def test_filter_by_types_for_bash_by_interpreter():
ret = _filter_by_types(['bash_script'], ['shell', 'sh', 'bash'], [], get_tags=get_tags_stub('bash')) ret = _filter_by_types(['bash_script'], ['shell', 'sh', 'bash'], [], get_tags=get_tags_stub('bash'))
assert ret == ('bash_script',) assert ret == ('bash_script',)
@pytest.mark.current
def test_filter_by_types_for_python_by_interpreter(): def test_filter_by_types_for_python_by_interpreter():
ret = _filter_by_types(['script.py'], ['python'], [], get_tags=get_tags_stub('python')) ret = _filter_by_types(['script.py'], ['python'], [], get_tags=get_tags_stub('python'))
assert ret == ('script.py',) assert ret == ('script.py',)