Skip to content

Commit 039a7a5

Browse files
committed
Remove defaults from pre-commit config schema. Resolves pre-commit#227
1 parent 9515ca0 commit 039a7a5

5 files changed

Lines changed: 36 additions & 4 deletions

File tree

pre_commit/clientlib/validate_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class InvalidConfigError(FatalError):
3333
'properties': {
3434
'id': {'type': 'string'},
3535
'files': {'type': 'string'},
36-
'exclude': {'type': 'string', 'default': '^$'},
36+
'exclude': {'type': 'string'},
3737
'language_version': {'type': 'string'},
3838
'args': {
3939
'type': 'array',
@@ -71,7 +71,7 @@ def validate_config_extra(config):
7171
)
7272
for hook in repo['hooks']:
7373
try_regex(repo, hook['id'], hook.get('files', ''), 'files')
74-
try_regex(repo, hook['id'], hook['exclude'], 'exclude')
74+
try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude')
7575

7676

7777
load_config = get_validator(

pre_commit/clientlib/validate_manifest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class InvalidManifestError(ValueError):
2020
'name': {'type': 'string'},
2121
'description': {'type': 'string', 'default': ''},
2222
'entry': {'type': 'string'},
23+
'exclude': {'type': 'string', 'default': '^$'},
2324
'language': {'type': 'string'},
2425
'language_version': {'type': 'string', 'default': 'default'},
2526
'files': {'type': 'string'},
@@ -52,8 +53,14 @@ def validate_files(hook_config):
5253
if not is_regex_valid(hook_config['files']):
5354
raise InvalidManifestError(
5455
'Invalid files regex at {0}: {1}'.format(
55-
hook_config['id'],
56-
hook_config['files'],
56+
hook_config['id'], hook_config['files'],
57+
)
58+
)
59+
60+
if not is_regex_valid(hook_config.get('exclude', '')):
61+
raise InvalidManifestError(
62+
'Invalid exclude regex at {0}: {1}'.format(
63+
hook_config['id'], hook_config['exclude'],
5764
)
5865
)
5966

tests/clientlib/validate_config_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,23 @@ def test_config_with_local_hooks_definition_passes(config_obj):
174174
jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
175175
config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
176176
validate_config_extra(config)
177+
178+
179+
def test_does_not_contain_defaults():
180+
"""Due to the way our merging works, if this schema has any defaults they
181+
will clobber potentially useful values in the backing manifest. #227
182+
"""
183+
to_process = [(CONFIG_JSON_SCHEMA, ())]
184+
while to_process:
185+
schema, route = to_process.pop()
186+
# Check this value
187+
if isinstance(schema, dict):
188+
if 'default' in schema:
189+
raise AssertionError(
190+
'Unexpected default in schema at {0}'.format(
191+
' => '.join(route),
192+
)
193+
)
194+
195+
for key, value in schema.items():
196+
to_process.append((value, route + (key,)))

tests/clientlib/validate_manifest_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def test_additional_manifest_check_passing(obj):
4646
[{'id': 'a', 'language': 'not a language', 'files': ''}],
4747
[{'id': 'a', 'language': 'python3', 'files': ''}],
4848
[{'id': 'a', 'language': 'python', 'files': 'invalid regex('}],
49+
[{'id': 'a', 'language': 'not a language', 'files': ''}],
50+
[{'id': 'a', 'language': 'python3', 'files': ''}],
51+
[{'id': 'a', 'language': 'python', 'files': '', 'exclude': '('}],
4952
),
5053
)
5154
def test_additional_manifest_failing(obj):

tests/manifest_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_manifest_contents(manifest):
2222
'args': [],
2323
'description': '',
2424
'entry': 'bin/hook.sh',
25+
'exclude': '^$',
2526
'expected_return_value': 0,
2627
'files': '',
2728
'id': 'bash_hook',
@@ -36,6 +37,7 @@ def test_hooks(manifest):
3637
'args': [],
3738
'description': '',
3839
'entry': 'bin/hook.sh',
40+
'exclude': '^$',
3941
'expected_return_value': 0,
4042
'files': '',
4143
'id': 'bash_hook',

0 commit comments

Comments
 (0)