Example #1
0
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'),
    )
Example #2
0
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'),
    )
Example #3
0
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'),
    )
Example #4
0
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'),
    )
Example #5
0
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'),
    )
Example #6
0
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',
        ),
    )
Example #7
0
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',
        ),
    )
Example #8
0
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',
        ),
    )
Example #9
0
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',
        ),
    )
Example #10
0
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',
        ),
    )
Example #11
0
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)
Example #12
0
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
Example #13
0
 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'])
Example #14
0
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)
Example #15
0
 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']
     )
Example #16
0
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
Example #17
0
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
Example #18
0
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
Example #19
0
def test_ok_conditional_schemas(v, schema):
    validate(v, schema)
Example #20
0
def test_optional_key_missing(schema):
    validate({}, schema)
Example #21
0
def test_ok_conditional_schemas(v, schema):
    validate(v, schema)
Example #22
0
def test_config_with_local_hooks_definition_passes(config_obj):
    schema.validate(config_obj, CONFIG_SCHEMA)
Example #23
0
def test_config_with_local_hooks_definition_fails(config_obj):
    with pytest.raises(schema.ValidationError):
        schema.validate(config_obj, CONFIG_SCHEMA)
Example #24
0
def test_config_with_local_hooks_definition_passes(config_obj):
    schema.validate(config_obj, CONFIG_SCHEMA)
Example #25
0
def is_valid_according_to_schema(obj, obj_schema):
    try:
        schema.validate(obj, obj_schema)
        return True
    except schema.ValidationError:
        return False
Example #26
0
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'"
Example #27
0
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'"
Example #28
0
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'),
    )
Example #29
0
def test_map_value_correct_type(schema):
    validate({'key': True}, schema)
Example #30
0
def test_optional_key_missing(schema):
    validate({}, schema)
Example #31
0
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'"
Example #32
0
def test_ok_both_types(v):
    validate(v, trivial_array_schema)
Example #33
0
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'"
Example #34
0
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'
Example #35
0
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)
Example #36
0
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'
Example #37
0
def _hook_from_manifest_dct(dct):
    dct = validate(apply_defaults(dct, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT)
    dct = _hook(dct)
    return dct
Example #38
0
def test_ok_both_types(v):
    validate(v, trivial_array_schema)
Example #39
0
def is_valid_according_to_schema(obj, obj_schema):
    try:
        schema.validate(obj, obj_schema)
        return True
    except schema.ValidationError:
        return False
Example #40
0
def test_map_value_correct_type(schema):
    validate({'key': True}, schema)
Example #41
0
def test_config_with_local_hooks_definition_fails(config_obj):
    with pytest.raises(schema.ValidationError):
        schema.validate(config_obj, CONFIG_SCHEMA)
Example #42
0
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)