Allow exclude pattern to be parsed.

This commit is contained in:
Anthony Sottile 2014-03-31 21:43:26 -07:00
parent 770e48a8f7
commit 5f4996645c
2 changed files with 62 additions and 29 deletions

View file

@ -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(

View file

@ -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/'}],
}])