From 5f4996645cfa40757c2033f20bad502027e18ba1 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 31 Mar 2014 21:43:26 -0700 Subject: [PATCH] Allow exclude pattern to be parsed. --- pre_commit/clientlib/validate_config.py | 22 +++++--- tests/clientlib/validate_config_test.py | 69 +++++++++++++++++-------- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/pre_commit/clientlib/validate_config.py b/pre_commit/clientlib/validate_config.py index 0f783a6e..e87cf1c8 100644 --- a/pre_commit/clientlib/validate_config.py +++ b/pre_commit/clientlib/validate_config.py @@ -28,6 +28,7 @@ CONFIG_JSON_SCHEMA = { 'properties': { 'id': {'type': 'string'}, 'files': {'type': 'string'}, + 'exclude': {'type': 'string'}, 'args': { 'type': 'array', 'minItems': 1, @@ -43,17 +44,22 @@ CONFIG_JSON_SCHEMA = { } +def try_regex(repo, hook, value, field_name): + try: + re.compile(value) + except re.error: + raise InvalidConfigError( + 'Invalid {0} regex at {1}, {2}: {3}'.format( + field_name, repo, hook, value, + ) + ) + + def validate_config_extra(config): for repo in config: for hook in repo['hooks']: - try: - re.compile(hook['files']) - except re.error: - raise InvalidConfigError( - 'Invalid file regex at {0}, {1}: {2}'.format( - repo['repo'], hook['id'], hook['files'], - ) - ) + try_regex(repo, hook['id'], hook['files'], 'files') + try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude') load_config = get_validator( diff --git a/tests/clientlib/validate_config_test.py b/tests/clientlib/validate_config_test.py index bf80793f..54f4c887 100644 --- a/tests/clientlib/validate_config_test.py +++ b/tests/clientlib/validate_config_test.py @@ -35,29 +35,40 @@ def is_valid_according_to_schema(obj, schema): [{ 'repo': 'git@github.com:pre-commit/pre-commit-hooks', 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', - 'hooks': [ - { - 'id': 'pyflakes', - 'files': '*.py', - } - ] + 'hooks': [{'id': 'pyflakes', 'files': '\.py$'}], }], True, ), ( [{ - 'repo': 'git@github.com:pre-commit/pre-commit-hooks', - 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', - 'hooks': [ - { - 'id': 'pyflakes', - 'files': '*.py', - 'args': ['foo', 'bar', 'baz'], - } - ] + 'repo': 'git@github.com:pre-commit/pre-commit-hooks', + 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', + 'hooks': [ + { + 'id': 'pyflakes', + 'files': '\.py$', + 'args': ['foo', 'bar', 'baz'], + }, + ], }], True, ), + ( + [{ + 'repo': 'git@github.com:pre-commit/pre-commit-hooks', + 'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37', + 'hooks': [ + { + 'id': 'pyflakes', + 'files': '\.py$', + # Exclude pattern must be a string + 'exclude': 0, + 'args': ['foo', 'bar', 'baz'], + }, + ], + }], + False, + ), )) def test_is_valid_according_to_schema(manifest_obj, expected): ret = is_valid_according_to_schema(manifest_obj, CONFIG_JSON_SCHEMA) @@ -67,12 +78,28 @@ def test_is_valid_according_to_schema(manifest_obj, expected): def test_config_with_failing_regexes_fails(): with pytest.raises(InvalidConfigError): # Note the regex '(' is invalid (unbalanced parens) - validate_config_extra( - [{'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '('}]}] - ) + validate_config_extra([{ + 'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '('}] + }]) def test_config_with_ok_regexes_passes(): - validate_config_extra( - [{'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\.py$'}]}] - ) + validate_config_extra([{ + 'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\.py$'}], + }]) + + +def test_config_with_invalid_exclude_regex_fails(): + with pytest.raises(InvalidConfigError): + # NOte the regex '(' is invalid (unbalanced parens) + validate_config_extra([{ + 'repo': 'foo', + 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '('}], + }]) + + +def test_config_with_ok_exclude_regex_passes(): + validate_config_extra([{ + 'repo': 'foo', + 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}], + }])