diff --git a/pre_commit/clientlib/validate_base.py b/pre_commit/clientlib/validate_base.py index 75a4cf96..dcf7f115 100644 --- a/pre_commit/clientlib/validate_base.py +++ b/pre_commit/clientlib/validate_base.py @@ -4,6 +4,8 @@ import jsonschema.exceptions import os.path import yaml +from pre_commit.jsonschema_extensions import apply_defaults + def get_validator( json_schema, @@ -39,6 +41,8 @@ def get_validator( 'File {0} is not a valid file'.format(filename), e, ) + obj = apply_defaults(obj, json_schema) + additional_validation_strategy(obj) return obj diff --git a/pre_commit/clientlib/validate_config.py b/pre_commit/clientlib/validate_config.py index e87cf1c8..307b5633 100644 --- a/pre_commit/clientlib/validate_config.py +++ b/pre_commit/clientlib/validate_config.py @@ -28,10 +28,10 @@ CONFIG_JSON_SCHEMA = { 'properties': { 'id': {'type': 'string'}, 'files': {'type': 'string'}, - 'exclude': {'type': 'string'}, + 'exclude': {'type': 'string', 'default': '^$'}, 'args': { 'type': 'array', - 'minItems': 1, + 'default': [], 'items': {'type': 'string'}, }, }, @@ -59,7 +59,7 @@ def validate_config_extra(config): for repo in config: for hook in repo['hooks']: try_regex(repo, hook['id'], hook['files'], 'files') - try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude') + try_regex(repo, hook['id'], hook['exclude'], 'exclude') load_config = get_validator( diff --git a/pre_commit/clientlib/validate_manifest.py b/pre_commit/clientlib/validate_manifest.py index 43200832..3f89cfae 100644 --- a/pre_commit/clientlib/validate_manifest.py +++ b/pre_commit/clientlib/validate_manifest.py @@ -20,10 +20,10 @@ MANIFEST_JSON_SCHEMA = { 'properties': { 'id': {'type': 'string'}, 'name': {'type': 'string'}, - 'description': {'type': 'string'}, + 'description': {'type': 'string', 'default': ''}, 'entry': {'type': 'string'}, 'language': {'type': 'string'}, - 'expected_return_value': {'type': 'number'}, + 'expected_return_value': {'type': 'number', 'default': 0}, }, 'required': ['id', 'name', 'entry', 'language'], }, @@ -32,11 +32,9 @@ MANIFEST_JSON_SCHEMA = { def additional_manifest_check(obj): for hook_config in obj: - language = hook_config.get('language') + language = hook_config['language'] - if language is not None and not any( - language.startswith(lang) for lang in all_languages - ): + if not any(language.startswith(lang) for lang in all_languages): raise InvalidManifestError( 'Expected language {0} for {1} to start with one of {2!r}'.format( hook_config['id'], diff --git a/pre_commit/languages/helpers.py b/pre_commit/languages/helpers.py index 4c05f3e7..fab8e5ae 100644 --- a/pre_commit/languages/helpers.py +++ b/pre_commit/languages/helpers.py @@ -2,7 +2,7 @@ def run_hook(env, hook, file_args): return env.run( - ' '.join(['xargs', hook['entry']] + hook.get('args', [])), + ' '.join(['xargs', hook['entry']] + hook['args']), stdin='\n'.join(list(file_args) + ['']), retcode=None, ) diff --git a/pre_commit/languages/script.py b/pre_commit/languages/script.py index 783743ae..e622a7c8 100644 --- a/pre_commit/languages/script.py +++ b/pre_commit/languages/script.py @@ -6,7 +6,7 @@ def install_environment(repo_cmd_runner): def run_hook(repo_cmd_runner, hook, file_args): return repo_cmd_runner.run( - ['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook.get('args', []), + ['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'], # TODO: this is duplicated in pre_commit/languages/helpers.py stdin='\n'.join(list(file_args) + ['']), retcode=None, diff --git a/pre_commit/languages/system.py b/pre_commit/languages/system.py index 8439c091..39dcf89c 100644 --- a/pre_commit/languages/system.py +++ b/pre_commit/languages/system.py @@ -6,7 +6,7 @@ def install_environment(repo_cmd_runner): def run_hook(repo_cmd_runner, hook, file_args): return repo_cmd_runner.run( - ['xargs', hook['entry']] + hook.get('args', []), + ['xargs', hook['entry']] + hook['args'], # TODO: this is duplicated in pre_commit/languages/helpers.py stdin='\n'.join(list(file_args) + ['']), retcode=None, diff --git a/pre_commit/repository.py b/pre_commit/repository.py index f8f4d1c2..10ff2b6c 100644 --- a/pre_commit/repository.py +++ b/pre_commit/repository.py @@ -27,9 +27,7 @@ class Repository(object): @cached_property def languages(self): - return set(filter(None, ( - hook.get('language') for hook in self.hooks.values() - ))) + return set(hook['language'] for hook in self.hooks.values()) @cached_property def hooks(self): diff --git a/pre_commit/run.py b/pre_commit/run.py index 63247658..1b76e834 100644 --- a/pre_commit/run.py +++ b/pre_commit/run.py @@ -38,7 +38,7 @@ def _run_single_hook(runner, repository, hook_id, all_files=False): get_filenames(hook['files']), ) - if retcode != repository.hooks[hook_id].get('expected_return_value', 0): + if retcode != repository.hooks[hook_id]['expected_return_value']: output = '\n'.join([stdout, stderr]).strip() retcode = 1 color = RED diff --git a/tests/clientlib/validate_config_test.py b/tests/clientlib/validate_config_test.py index 54f4c887..f3ccc4df 100644 --- a/tests/clientlib/validate_config_test.py +++ b/tests/clientlib/validate_config_test.py @@ -7,6 +7,7 @@ from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA from pre_commit.clientlib.validate_config import InvalidConfigError from pre_commit.clientlib.validate_config import run from pre_commit.clientlib.validate_config import validate_config_extra +from pre_commit.jsonschema_extensions import apply_defaults def test_returns_0_for_valid_config(): @@ -78,28 +79,50 @@ 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': '('}] - }]) + config = apply_defaults( + [{ + 'repo': 'foo', + 'sha': 'foo', + 'hooks': [{'id': 'hook_id', 'files': '('}], + }], + CONFIG_JSON_SCHEMA, + ) + validate_config_extra(config) def test_config_with_ok_regexes_passes(): - validate_config_extra([{ - 'repo': 'foo', 'hooks': [{'id': 'hook_id', 'files': '\.py$'}], - }]) + config = apply_defaults( + [{ + 'repo': 'foo', + 'sha': 'foo', + 'hooks': [{'id': 'hook_id', 'files': '\.py$'}], + }], + CONFIG_JSON_SCHEMA, + ) + validate_config_extra(config) 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': '('}], - }]) + # Note the regex '(' is invalid (unbalanced parens) + config = apply_defaults( + [{ + 'repo': 'foo', + 'sha': 'foo', + 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '('}], + }], + CONFIG_JSON_SCHEMA, + ) + validate_config_extra(config) def test_config_with_ok_exclude_regex_passes(): - validate_config_extra([{ - 'repo': 'foo', - 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}], - }]) + config = apply_defaults( + [{ + 'repo': 'foo', + 'sha': 'foo', + 'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}], + }], + CONFIG_JSON_SCHEMA, + ) + validate_config_extra(config) diff --git a/tests/clientlib/validate_manifest_test.py b/tests/clientlib/validate_manifest_test.py index 7d64db28..a1c10580 100644 --- a/tests/clientlib/validate_manifest_test.py +++ b/tests/clientlib/validate_manifest_test.py @@ -27,11 +27,10 @@ def test_additional_manifest_check_raises_for_bad_language(): @pytest.mark.parametrize(('obj'), ( - [{}], [{'language': 'python'}], [{'language': 'ruby'}], )) -def test_additional_manifest_check_is_ok_with_missing_language(obj): +def test_additional_manifest_check_languages(obj): additional_manifest_check(obj) diff --git a/tests/commands_test.py b/tests/commands_test.py index 6dba08d0..21740f2c 100644 --- a/tests/commands_test.py +++ b/tests/commands_test.py @@ -1,5 +1,4 @@ -import jsonschema import os import os.path import pkg_resources @@ -18,6 +17,7 @@ from pre_commit.commands import install from pre_commit.commands import RepositoryCannotBeUpdatedError from pre_commit.commands import uninstall from pre_commit.commands import _update_repository +from pre_commit.jsonschema_extensions import apply_defaults from pre_commit.ordereddict import OrderedDict from pre_commit.runner import Runner from pre_commit.yaml_extensions import ordered_dump @@ -60,8 +60,9 @@ def up_to_date_repo(python_hooks_repo): ('sha', git.get_head_sha(python_hooks_repo)), ('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]), )) - jsonschema.validate([config], CONFIG_JSON_SCHEMA) - validate_config_extra([config]) + wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA) + validate_config_extra(wrapped_config) + config = wrapped_config[0] with open(os.path.join(python_hooks_repo, C.CONFIG_FILE), 'w') as file_obj: file_obj.write( @@ -96,8 +97,9 @@ def out_of_date_repo(python_hooks_repo): ('sha', git.get_head_sha(python_hooks_repo)), ('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]), )) - jsonschema.validate([config], CONFIG_JSON_SCHEMA) - validate_config_extra([config]) + config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) + validate_config_extra(config_wrapped) + config = config_wrapped[0] local['git']['commit', '--allow-empty', '-m', 'foo']() head_sha = git.get_head_sha(python_hooks_repo) @@ -135,8 +137,9 @@ def hook_disappearing_repo(python_hooks_repo): ('sha', git.get_head_sha(python_hooks_repo)), ('hooks', [OrderedDict((('id', 'foo'), ('files', '')))]), )) - jsonschema.validate([config], CONFIG_JSON_SCHEMA) - validate_config_extra([config]) + config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) + validate_config_extra(config_wrapped) + config = config_wrapped[0] shutil.copy(get_resource_path('manifest_without_foo.yaml'), C.MANIFEST_FILE) local['git']['add', '.']() local['git']['commit', '-m', 'Remove foo']() diff --git a/tests/conftest.py b/tests/conftest.py index 70e3a9a1..feecc6da 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,5 @@ from __future__ import absolute_import -import jsonschema import pytest import time from plumbum import local @@ -8,6 +7,7 @@ from plumbum import local from pre_commit import git from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA from pre_commit.clientlib.validate_config import validate_config_extra +from pre_commit.jsonschema_extensions import apply_defaults from testing.util import copy_tree_to_path from testing.util import get_resource_path @@ -69,9 +69,9 @@ def _make_config(path, hook_id, file_regex): 'sha': git.get_head_sha(path), 'hooks': [{'id': hook_id, 'files': file_regex}], } - jsonschema.validate([config], CONFIG_JSON_SCHEMA) - validate_config_extra([config]) - return config + config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) + validate_config_extra(config_wrapped) + return config_wrapped[0] @pytest.yield_fixture diff --git a/tests/repository_test.py b/tests/repository_test.py index 0b4011e7..2276b224 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -1,11 +1,11 @@ import os -import jsonschema import pytest import pre_commit.constants as C from pre_commit import git from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA from pre_commit.clientlib.validate_config import validate_config_extra +from pre_commit.jsonschema_extensions import apply_defaults from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.repository import Repository @@ -111,9 +111,9 @@ def mock_repo_config(): 'files': '\.py$', }], } - jsonschema.validate([config], CONFIG_JSON_SCHEMA) - validate_config_extra([config]) - return config + config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA) + validate_config_extra(config_wrapped) + return config_wrapped[0] def test_repo_url(mock_repo_config):