From a68c1ab0d2a263563b551b9a96f98cca7fe67144 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 1 Jul 2017 19:03:04 -0700 Subject: [PATCH] Add 'types' to the schema --- pre_commit/clientlib.py | 14 ++++++++++++-- pre_commit/commands/run.py | 2 +- setup.py | 1 + tests/clientlib_test.py | 7 +++++++ tests/manifest_test.py | 2 ++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index bceecaa6..fea9e306 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -5,6 +5,7 @@ import argparse import functools from aspy.yaml import ordered_load +from identify.identify import ALL_TAGS import pre_commit.constants as C from pre_commit import schema @@ -19,6 +20,14 @@ def check_language(v): ) +def check_type_tag(tag): + if tag not in ALL_TAGS: + raise schema.ValidationError( + 'Type tag {!r} is not recognized. ' + 'Try upgrading identify and pre-commit?'.format(tag), + ) + + def _make_argparser(filenames_help): parser = argparse.ArgumentParser() parser.add_argument('filenames', nargs='*', help=filenames_help) @@ -36,10 +45,11 @@ MANIFEST_HOOK_DICT = schema.Map( 'language', schema.check_and(schema.check_string, check_language), ), - schema.Conditional( + schema.Optional( 'files', schema.check_and(schema.check_string, schema.check_regex), - condition_key='always_run', condition_value=False, + '', ), + schema.Optional('types', schema.check_array(check_type_tag), ['file']), schema.Optional( 'additional_dependencies', schema.check_array(schema.check_string), [], diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index a8e61193..d30b4472 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -58,7 +58,7 @@ NO_FILES = '(no files to check)' def _run_single_hook(hook, repo, args, skips, cols): - filenames = get_filenames(args, hook.get('files', '^$'), hook['exclude']) + filenames = get_filenames(args, hook['files'], hook['exclude']) if hook['id'] in skips: output.write(get_hook_message( _hook_msg_start(hook, args.verbose), diff --git a/setup.py b/setup.py index 0b8bcb7d..1ec3c6ff 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ setup( install_requires=[ 'aspy.yaml', 'cached-property', + 'identify>=1.0.0', 'nodeenv>=0.11.1', 'pyyaml', 'six', diff --git a/tests/clientlib_test.py b/tests/clientlib_test.py index 454824a9..65209a64 100644 --- a/tests/clientlib_test.py +++ b/tests/clientlib_test.py @@ -4,6 +4,7 @@ import pytest from pre_commit import schema from pre_commit.clientlib import check_language +from pre_commit.clientlib import check_type_tag from pre_commit.clientlib import CONFIG_HOOK_DICT from pre_commit.clientlib import CONFIG_SCHEMA from pre_commit.clientlib import is_local_repo @@ -27,6 +28,12 @@ def test_check_language_failures(value): check_language(value) +@pytest.mark.parametrize('value', ('definitely-not-a-tag', 'fiel')) +def test_check_type_tag_failures(value): + with pytest.raises(schema.ValidationError): + check_type_tag(value) + + @pytest.mark.parametrize('value', ('python', 'node', 'pcre')) def test_check_language_ok(value): check_language(value) diff --git a/tests/manifest_test.py b/tests/manifest_test.py index 47e7fa32..3a31a812 100644 --- a/tests/manifest_test.py +++ b/tests/manifest_test.py @@ -34,6 +34,7 @@ def test_manifest_contents(manifest): 'name': 'Bash hook', 'pass_filenames': True, 'stages': [], + 'types': ['file'], }] @@ -54,6 +55,7 @@ def test_hooks(manifest): 'name': 'Bash hook', 'pass_filenames': True, 'stages': [], + 'types': ['file'], }