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('files', check_string_regex, ''),
cfgv.Optional('exclude', check_string_regex, '^$'), cfgv.Optional('exclude', check_string_regex, '^$'),
cfgv.Optional('types', cfgv.check_array(check_type_tag), ['file']), 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('exclude_types', cfgv.check_array(check_type_tag), []),
cfgv.Optional( cfgv.Optional(

View file

@ -83,20 +83,28 @@ class Classifier:
self, self,
names: Sequence[str], names: Sequence[str],
types: Collection[str], types: Collection[str],
types_or: Collection[str],
exclude_types: Collection[str], exclude_types: Collection[str],
) -> List[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 = [] ret = []
for filename in names: for filename in names:
tags = self._types_for_file(filename) 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) ret.append(filename)
return ret return ret
def filenames_for_hook(self, hook: Hook) -> Tuple[str, ...]: def filenames_for_hook(self, hook: Hook) -> Tuple[str, ...]:
names = self.filenames names = self.filenames
names = filter_by_include_exclude(names, hook.files, hook.exclude) 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) return tuple(names)
@classmethod @classmethod

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo $@ echo "$@"
echo 'Hello World' 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 #!/usr/bin/env bash
echo $@ echo "$@"
exit 1 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 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): def test_exclude_types_hook_repository(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo') git_path = make_consuming_repo(tempdir_factory, 'exclude_types_repo')
with cwd(git_path): with cwd(git_path):

View file

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