Skip to content

Commit ac67af2

Browse files
committed
Apply defaults to all of the configs. Much fewer .get()s
1 parent b23ad5d commit ac67af2

File tree

13 files changed

+73
-48
lines changed

13 files changed

+73
-48
lines changed

pre_commit/clientlib/validate_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import os.path
55
import yaml
66

7+
from pre_commit.jsonschema_extensions import apply_defaults
8+
79

810
def get_validator(
911
json_schema,
@@ -39,6 +41,8 @@ def validate(filename, load_strategy=yaml.load):
3941
'File {0} is not a valid file'.format(filename), e,
4042
)
4143

44+
obj = apply_defaults(obj, json_schema)
45+
4246
additional_validation_strategy(obj)
4347

4448
return obj

pre_commit/clientlib/validate_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class InvalidConfigError(ValueError): pass
2828
'properties': {
2929
'id': {'type': 'string'},
3030
'files': {'type': 'string'},
31-
'exclude': {'type': 'string'},
31+
'exclude': {'type': 'string', 'default': '^$'},
3232
'args': {
3333
'type': 'array',
34-
'minItems': 1,
34+
'default': [],
3535
'items': {'type': 'string'},
3636
},
3737
},
@@ -59,7 +59,7 @@ def validate_config_extra(config):
5959
for repo in config:
6060
for hook in repo['hooks']:
6161
try_regex(repo, hook['id'], hook['files'], 'files')
62-
try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude')
62+
try_regex(repo, hook['id'], hook['exclude'], 'exclude')
6363

6464

6565
load_config = get_validator(

pre_commit/clientlib/validate_manifest.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class InvalidManifestError(ValueError): pass
2020
'properties': {
2121
'id': {'type': 'string'},
2222
'name': {'type': 'string'},
23-
'description': {'type': 'string'},
23+
'description': {'type': 'string', 'default': ''},
2424
'entry': {'type': 'string'},
2525
'language': {'type': 'string'},
26-
'expected_return_value': {'type': 'number'},
26+
'expected_return_value': {'type': 'number', 'default': 0},
2727
},
2828
'required': ['id', 'name', 'entry', 'language'],
2929
},
@@ -32,11 +32,9 @@ class InvalidManifestError(ValueError): pass
3232

3333
def additional_manifest_check(obj):
3434
for hook_config in obj:
35-
language = hook_config.get('language')
35+
language = hook_config['language']
3636

37-
if language is not None and not any(
38-
language.startswith(lang) for lang in all_languages
39-
):
37+
if not any(language.startswith(lang) for lang in all_languages):
4038
raise InvalidManifestError(
4139
'Expected language {0} for {1} to start with one of {2!r}'.format(
4240
hook_config['id'],

pre_commit/languages/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
def run_hook(env, hook, file_args):
44
return env.run(
5-
' '.join(['xargs', hook['entry']] + hook.get('args', [])),
5+
' '.join(['xargs', hook['entry']] + hook['args']),
66
stdin='\n'.join(list(file_args) + ['']),
77
retcode=None,
88
)

pre_commit/languages/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def install_environment(repo_cmd_runner):
66

77
def run_hook(repo_cmd_runner, hook, file_args):
88
return repo_cmd_runner.run(
9-
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook.get('args', []),
9+
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
1010
# TODO: this is duplicated in pre_commit/languages/helpers.py
1111
stdin='\n'.join(list(file_args) + ['']),
1212
retcode=None,

pre_commit/languages/system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def install_environment(repo_cmd_runner):
66

77
def run_hook(repo_cmd_runner, hook, file_args):
88
return repo_cmd_runner.run(
9-
['xargs', hook['entry']] + hook.get('args', []),
9+
['xargs', hook['entry']] + hook['args'],
1010
# TODO: this is duplicated in pre_commit/languages/helpers.py
1111
stdin='\n'.join(list(file_args) + ['']),
1212
retcode=None,

pre_commit/repository.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ def sha(self):
2727

2828
@cached_property
2929
def languages(self):
30-
return set(filter(None, (
31-
hook.get('language') for hook in self.hooks.values()
32-
)))
30+
return set(hook['language'] for hook in self.hooks.values())
3331

3432
@cached_property
3533
def hooks(self):

pre_commit/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _run_single_hook(runner, repository, hook_id, all_files=False):
3838
get_filenames(hook['files']),
3939
)
4040

41-
if retcode != repository.hooks[hook_id].get('expected_return_value', 0):
41+
if retcode != repository.hooks[hook_id]['expected_return_value']:
4242
output = '\n'.join([stdout, stderr]).strip()
4343
retcode = 1
4444
color = RED

tests/clientlib/validate_config_test.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pre_commit.clientlib.validate_config import InvalidConfigError
88
from pre_commit.clientlib.validate_config import run
99
from pre_commit.clientlib.validate_config import validate_config_extra
10+
from pre_commit.jsonschema_extensions import apply_defaults
1011

1112

1213
def test_returns_0_for_valid_config():
@@ -78,28 +79,50 @@ def test_is_valid_according_to_schema(manifest_obj, expected):
7879
def test_config_with_failing_regexes_fails():
7980
with pytest.raises(InvalidConfigError):
8081
# Note the regex '(' is invalid (unbalanced parens)
81-
validate_config_extra([{
82-
'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '('}]
83-
}])
82+
config = apply_defaults(
83+
[{
84+
'repo': 'foo',
85+
'sha': 'foo',
86+
'hooks': [{'id': 'hook_id', 'files': '('}],
87+
}],
88+
CONFIG_JSON_SCHEMA,
89+
)
90+
validate_config_extra(config)
8491

8592

8693
def test_config_with_ok_regexes_passes():
87-
validate_config_extra([{
88-
'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\.py$'}],
89-
}])
94+
config = apply_defaults(
95+
[{
96+
'repo': 'foo',
97+
'sha': 'foo',
98+
'hooks': [{'id': 'hook_id', 'files': '\.py$'}],
99+
}],
100+
CONFIG_JSON_SCHEMA,
101+
)
102+
validate_config_extra(config)
90103

91104

92105
def test_config_with_invalid_exclude_regex_fails():
93106
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-
}])
107+
# Note the regex '(' is invalid (unbalanced parens)
108+
config = apply_defaults(
109+
[{
110+
'repo': 'foo',
111+
'sha': 'foo',
112+
'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '('}],
113+
}],
114+
CONFIG_JSON_SCHEMA,
115+
)
116+
validate_config_extra(config)
99117

100118

101119
def test_config_with_ok_exclude_regex_passes():
102-
validate_config_extra([{
103-
'repo': 'foo',
104-
'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}],
105-
}])
120+
config = apply_defaults(
121+
[{
122+
'repo': 'foo',
123+
'sha': 'foo',
124+
'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}],
125+
}],
126+
CONFIG_JSON_SCHEMA,
127+
)
128+
validate_config_extra(config)

tests/clientlib/validate_manifest_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ def test_additional_manifest_check_raises_for_bad_language():
2727

2828

2929
@pytest.mark.parametrize(('obj'), (
30-
[{}],
3130
[{'language': 'python'}],
3231
[{'language': 'ruby'}],
3332
))
34-
def test_additional_manifest_check_is_ok_with_missing_language(obj):
33+
def test_additional_manifest_check_languages(obj):
3534
additional_manifest_check(obj)
3635

3736

0 commit comments

Comments
 (0)