add types_or

This commit is contained in:
Marco Gorelli
2020-11-02 15:35:42 +00:00
parent 3112e08088
commit 62f668fc3f
13 changed files with 45 additions and 10 deletions

View File

@@ -61,6 +61,7 @@ MANIFEST_HOOK_DICT = cfgv.Map(
cfgv.Optional('files', check_string_regex, ''),
cfgv.Optional('exclude', check_string_regex, '^$'),
cfgv.Optional('types', cfgv.check_array(check_type_tag), ['file']),
cfgv.Optional('types_or', cfgv.check_array(check_type_tag), ['file']),
cfgv.Optional('exclude_types', cfgv.check_array(check_type_tag), []),
cfgv.Optional(

View File

@@ -83,20 +83,28 @@ class Classifier:
self,
names: Sequence[str],
types: Collection[str],
types_or: Collection[str],
exclude_types: Collection[str],
) -> List[str]:
types, exclude_types = frozenset(types), frozenset(exclude_types)
types = frozenset(types)
types_or = frozenset(types_or)
exclude_types = frozenset(exclude_types)
ret = []
for filename in names:
tags = self._types_for_file(filename)
if tags >= types and not tags & exclude_types:
if tags >= types and tags & types_or and not tags & exclude_types:
ret.append(filename)
return ret
def filenames_for_hook(self, hook: Hook) -> Tuple[str, ...]:
names = self.filenames
names = filter_by_include_exclude(names, hook.files, hook.exclude)
names = self.by_types(names, hook.types, hook.exclude_types)
names = self.by_types(
names,
hook.types,
hook.types_or,
hook.exclude_types,
)
return tuple(names)
@classmethod

View File

@@ -22,6 +22,7 @@ class Hook(NamedTuple):
files: str
exclude: str
types: Sequence[str]
types_or: Sequence[str]
exclude_types: Sequence[str]
additional_dependencies: Sequence[str]
args: Sequence[str]

View File

@@ -47,8 +47,10 @@ def check_useless_excludes(config_file: str) -> int:
# the defaults applied during runtime
hook = apply_defaults(hook, MANIFEST_HOOK_DICT)
names = classifier.filenames
types, exclude_types = hook['types'], hook['exclude_types']
names = classifier.by_types(names, types, exclude_types)
types = hook['types']
types_or = hook['types_or']
exclude_types = hook['exclude_types']
names = classifier.by_types(names, types, types_or, exclude_types)
include, exclude = hook['files'], hook['exclude']
if not exclude_matches_any(names, include, exclude):
print(

View File

@@ -1,3 +1,3 @@
#!/usr/bin/env bash
echo $@
echo "$@"
exit 1

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env bash
echo 'Fail'
echo $@
echo "$@"
exit 1

View File

@@ -1,2 +1,2 @@
#!/usr/bin/env bash
echo $@
echo "$@"

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env bash
echo $@
echo "$@"
echo 'Hello World'

View File

@@ -0,0 +1,6 @@
- id: python-cython-files
name: Python and Cython files
entry: bin/hook.sh
language: script
types: [file]
types_or: [python, cython]

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "$@"
exit 1

View File

@@ -1,3 +1,3 @@
#!/usr/bin/env bash
echo $@
echo "$@"
exit 1

View File

@@ -219,6 +219,19 @@ def test_types_hook_repository(cap_out, store, tempdir_factory):
assert b'bar.notpy' not in printed
def test_types_or_hook_repository(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'types_or_repo')
with cwd(git_path):
stage_a_file('bar.notpy')
stage_a_file('bar.pxd')
stage_a_file('bar.py')
ret, printed = _do_run(cap_out, store, git_path, run_opts())
assert ret == 1
assert b'bar.notpy' not in printed
assert b'bar.pxd' in printed
assert b'bar.py' in printed
def test_exclude_types_hook_repository(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo')
with cwd(git_path):

View File

@@ -901,6 +901,7 @@ def test_manifest_hooks(tempdir_factory, store):
'post-commit', 'manual', 'post-checkout', 'push',
),
types=['file'],
types_or=['file'],
verbose=False,
)