def test_map_value_wrong_type(schema): with pytest.raises(ValidationError) as excinfo: validate({'key': 5}, schema) _assert_exception_trace( excinfo.value, ('At foo(key=5)', 'At key: key', 'Expected bool got int'), )
def test_not_ok_conditional_schemas(schema): with pytest.raises(ValidationError) as excinfo: validate({'key': True, 'key2': 5}, schema) _assert_exception_trace( excinfo.value, ('At foo(key=True)', 'At key: key2', 'Expected bool got int'), )
def test_map_value_wrong_type(schema): with pytest.raises(ValidationError) as excinfo: validate({'key': 5}, schema) _assert_exception_trace( excinfo.value, ('At foo(key=5)', 'At key: key', 'Expected bool got int'), )
def test_required_missing_key(): with pytest.raises(ValidationError) as excinfo: validate({}, map_required) _assert_exception_trace( excinfo.value, ('At foo(key=MISSING)', 'Missing required key: key'), )
def test_not_ok_conditional_schemas(schema): with pytest.raises(ValidationError) as excinfo: validate({'key': True, 'key2': 5}, schema) _assert_exception_trace( excinfo.value, ('At foo(key=True)', 'At key: key2', 'Expected bool got int'), )
def test_validate_failure_nested(): with pytest.raises(ValidationError) as excinfo: validate({'repo': 1, 'hooks': [{}]}, nested_schema_required) _assert_exception_trace( excinfo.value, ( 'At Repository(repo=1)', 'At key: hooks', 'At foo(key=MISSING)', 'Missing required key: key', ), )
def test_ensure_absent_conditional_not(): with pytest.raises(ValidationError) as excinfo: validate({'key': True, 'key2': True}, map_conditional_absent_not) _assert_exception_trace( excinfo.value, ( 'At foo(key=True)', 'Expected key2 to be absent when key is True, ' 'found key2: True', ), )
def test_ensure_absent_conditional(): with pytest.raises(ValidationError) as excinfo: validate({'key': False, 'key2': True}, map_conditional_absent) _assert_exception_trace( excinfo.value, ( 'At foo(key=False)', 'Expected key2 to be absent when key is not True, ' 'found key2: True', ), )
def test_ensure_absent_conditional_not_in(): with pytest.raises(ValidationError) as excinfo: validate({'key': 1, 'key2': True}, map_conditional_absent_not_in) _assert_exception_trace( excinfo.value, ( 'At foo(key=1)', 'Expected key2 to be absent when key is any of (1, 2), ' 'found key2: True', ), )
def test_validate_failure_nested(): with pytest.raises(ValidationError) as excinfo: validate({'repo': 1, 'hooks': [{}]}, nested_schema_required) _assert_exception_trace( excinfo.value, ( 'At Repository(repo=1)', 'At key: hooks', 'At foo(key=MISSING)', 'Missing required key: key', ), )
def test_config_with_local_hooks_definition_fails(): config_obj = {'repos': [{ 'repo': 'local', 'sha': 'foo', 'hooks': [{ 'id': 'do_not_commit', 'name': 'Block if "DO NOT COMMIT" is found', 'entry': 'DO NOT COMMIT', 'language': 'pcre', 'files': '^(.*)$', }], }]} with pytest.raises(schema.ValidationError): schema.validate(config_obj, CONFIG_SCHEMA)
def make_config_from_repo( repo_path, sha=None, hooks=None, check=True, legacy=False, ): filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE manifest = load_manifest(os.path.join(repo_path, filename)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']), )) for hook in manifest], ), )) if check: wrapped = validate([config], CONFIG_SCHEMA) config, = apply_defaults(wrapped, CONFIG_SCHEMA) return config else: return config
def hooks(self): return tuple(( hook['id'], _validate_minimum_version( apply_defaults( validate(hook, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT, ), ), ) for hook in self.repo_config['hooks'])
def test_config_with_local_hooks_definition_fails(): config_obj = { 'repos': [{ 'repo': 'local', 'sha': 'foo', 'hooks': [{ 'id': 'do_not_commit', 'name': 'Block if "DO NOT COMMIT" is found', 'entry': 'DO NOT COMMIT', 'language': 'pcre', 'files': '^(.*)$', }], }] } with pytest.raises(schema.ValidationError): schema.validate(config_obj, CONFIG_SCHEMA)
def hooks(self): return tuple( ( hook['id'], _validate_minimum_version( apply_defaults( validate(hook, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT, ), ), ) for hook in self.repo_config['hooks'] )
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = OrderedDict(( ('repo', 'file://{}'.format(repo_path)), ('sha', sha or git.head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped = validate({'repos': [config]}, CONFIG_SCHEMA) wrapped = apply_defaults(wrapped, CONFIG_SCHEMA) config, = wrapped['repos'] return config else: return config
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = OrderedDict(( ('repo', 'file://{}'.format(repo_path)), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped = validate({'repos': [config]}, CONFIG_SCHEMA) wrapped = apply_defaults(wrapped, CONFIG_SCHEMA) config, = wrapped['repos'] return config else: return config
def make_config_from_repo( repo_path, sha=None, hooks=None, check=True, legacy=False, ): filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE manifest = load_manifest(os.path.join(repo_path, filename)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped = validate([config], CONFIG_SCHEMA) config, = apply_defaults(wrapped, CONFIG_SCHEMA) return config else: return config
def test_ok_conditional_schemas(v, schema): validate(v, schema)
def test_optional_key_missing(schema): validate({}, schema)
def test_ok_conditional_schemas(v, schema): validate(v, schema)
def test_config_with_local_hooks_definition_passes(config_obj): schema.validate(config_obj, CONFIG_SCHEMA)
def test_config_with_local_hooks_definition_fails(config_obj): with pytest.raises(schema.ValidationError): schema.validate(config_obj, CONFIG_SCHEMA)
def test_config_with_local_hooks_definition_passes(config_obj): schema.validate(config_obj, CONFIG_SCHEMA)
def is_valid_according_to_schema(obj, obj_schema): try: schema.validate(obj, obj_schema) return True except schema.ValidationError: return False
def test_validate_top_level_array_no_objects(): with pytest.raises(ValidationError) as excinfo: validate([], trivial_array_schema) assert excinfo.value.error_msg == "Expected at least 1 'foo'"
def test_validate_top_level_array_not_an_array(): with pytest.raises(ValidationError) as excinfo: validate({}, trivial_array_schema) assert excinfo.value.error_msg == "Expected array but got 'dict'"
def test_required_missing_key(): with pytest.raises(ValidationError) as excinfo: validate({}, map_required) _assert_exception_trace( excinfo.value, ('At foo(key=MISSING)', 'Missing required key: key'), )
def test_map_value_correct_type(schema): validate({'key': True}, schema)
def test_optional_key_missing(schema): validate({}, schema)
def test_validate_top_level_array_no_objects(): with pytest.raises(ValidationError) as excinfo: validate([], trivial_array_schema) assert excinfo.value.error_msg == "Expected at least 1 'foo'"
def test_ok_both_types(v): validate(v, trivial_array_schema)
def test_validate_top_level_array_not_an_array(): with pytest.raises(ValidationError) as excinfo: validate({}, trivial_array_schema) assert excinfo.value.error_msg == "Expected array but got 'dict'"
def test_map_wrong_type(): with pytest.raises(ValidationError) as excinfo: validate([], map_required) assert excinfo.value.error_msg == 'Expected a foo map but got a list'
def test_no_error_conditional_absent(): validate({}, map_conditional_absent) validate({}, map_conditional_absent_not) validate({'key2': True}, map_conditional_absent) validate({'key2': True}, map_conditional_absent_not)
def test_map_wrong_type(): with pytest.raises(ValidationError) as excinfo: validate([], map_required) assert excinfo.value.error_msg == 'Expected a foo map but got a list'
def _hook_from_manifest_dct(dct): dct = validate(apply_defaults(dct, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT) dct = _hook(dct) return dct
def test_ok_both_types(v): validate(v, trivial_array_schema)
def is_valid_according_to_schema(obj, obj_schema): try: schema.validate(obj, obj_schema) return True except schema.ValidationError: return False
def test_map_value_correct_type(schema): validate({'key': True}, schema)
def test_config_with_local_hooks_definition_fails(config_obj): with pytest.raises(schema.ValidationError): schema.validate(config_obj, CONFIG_SCHEMA)
def test_no_error_conditional_absent(): validate({}, map_conditional_absent) validate({}, map_conditional_absent_not) validate({'key2': True}, map_conditional_absent) validate({'key2': True}, map_conditional_absent_not)