Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f49966

Browse files
committed
Allow exclude pattern to be parsed.
1 parent 770e48a commit 5f49966

2 files changed

Lines changed: 62 additions & 29 deletions

File tree

pre_commit/clientlib/validate_config.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class InvalidConfigError(ValueError): pass
2828
'properties': {
2929
'id': {'type': 'string'},
3030
'files': {'type': 'string'},
31+
'exclude': {'type': 'string'},
3132
'args': {
3233
'type': 'array',
3334
'minItems': 1,
@@ -43,17 +44,22 @@ class InvalidConfigError(ValueError): pass
4344
}
4445

4546

47+
def try_regex(repo, hook, value, field_name):
48+
try:
49+
re.compile(value)
50+
except re.error:
51+
raise InvalidConfigError(
52+
'Invalid {0} regex at {1}, {2}: {3}'.format(
53+
field_name, repo, hook, value,
54+
)
55+
)
56+
57+
4658
def validate_config_extra(config):
4759
for repo in config:
4860
for hook in repo['hooks']:
49-
try:
50-
re.compile(hook['files'])
51-
except re.error:
52-
raise InvalidConfigError(
53-
'Invalid file regex at {0}, {1}: {2}'.format(
54-
repo['repo'], hook['id'], hook['files'],
55-
)
56-
)
61+
try_regex(repo, hook['id'], hook['files'], 'files')
62+
try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude')
5763

5864

5965
load_config = get_validator(

tests/clientlib/validate_config_test.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,40 @@ def is_valid_according_to_schema(obj, schema):
3535
[{
3636
'repo': '[email protected]:pre-commit/pre-commit-hooks',
3737
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
38-
'hooks': [
39-
{
40-
'id': 'pyflakes',
41-
'files': '*.py',
42-
}
43-
]
38+
'hooks': [{'id': 'pyflakes', 'files': '\.py$'}],
4439
}],
4540
True,
4641
),
4742
(
4843
[{
49-
'repo': '[email protected]:pre-commit/pre-commit-hooks',
50-
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
51-
'hooks': [
52-
{
53-
'id': 'pyflakes',
54-
'files': '*.py',
55-
'args': ['foo', 'bar', 'baz'],
56-
}
57-
]
44+
'repo': '[email protected]:pre-commit/pre-commit-hooks',
45+
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
46+
'hooks': [
47+
{
48+
'id': 'pyflakes',
49+
'files': '\.py$',
50+
'args': ['foo', 'bar', 'baz'],
51+
},
52+
],
5853
}],
5954
True,
6055
),
56+
(
57+
[{
58+
'repo': '[email protected]:pre-commit/pre-commit-hooks',
59+
'sha': 'cd74dc150c142c3be70b24eaf0b02cae9d235f37',
60+
'hooks': [
61+
{
62+
'id': 'pyflakes',
63+
'files': '\.py$',
64+
# Exclude pattern must be a string
65+
'exclude': 0,
66+
'args': ['foo', 'bar', 'baz'],
67+
},
68+
],
69+
}],
70+
False,
71+
),
6172
))
6273
def test_is_valid_according_to_schema(manifest_obj, expected):
6374
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):
6778
def test_config_with_failing_regexes_fails():
6879
with pytest.raises(InvalidConfigError):
6980
# Note the regex '(' is invalid (unbalanced parens)
70-
validate_config_extra(
71-
[{'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '('}]}]
72-
)
81+
validate_config_extra([{
82+
'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '('}]
83+
}])
7384

7485

7586
def test_config_with_ok_regexes_passes():
76-
validate_config_extra(
77-
[{'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\.py$'}]}]
78-
)
87+
validate_config_extra([{
88+
'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\.py$'}],
89+
}])
90+
91+
92+
def test_config_with_invalid_exclude_regex_fails():
93+
with pytest.raises(InvalidConfigError):
94+
# NOte the regex '(' is invalid (unbalanced parens)
95+
validate_config_extra([{
96+
'repo': 'foo',
97+
'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '('}],
98+
}])
99+
100+
101+
def test_config_with_ok_exclude_regex_passes():
102+
validate_config_extra([{
103+
'repo': 'foo',
104+
'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}],
105+
}])

0 commit comments

Comments
 (0)