Esempio n. 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'),
    )
Esempio n. 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'),
    )
Esempio n. 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'),
    )
Esempio n. 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'),
    )
Esempio n. 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'),
    )
Esempio n. 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',
        ),
    )
Esempio n. 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',
        ),
    )
Esempio n. 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',
        ),
    )
Esempio n. 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',
        ),
    )
Esempio n. 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',
        ),
    )
Esempio n. 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)
Esempio n. 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
Esempio n. 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'])
Esempio n. 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)
Esempio n. 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']
     )
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 19
0
def test_ok_conditional_schemas(v, schema):
    validate(v, schema)
Esempio n. 20
0
def test_optional_key_missing(schema):
    validate({}, schema)
Esempio n. 21
0
def test_ok_conditional_schemas(v, schema):
    validate(v, schema)
Esempio n. 22
0
def test_config_with_local_hooks_definition_passes(config_obj):
    schema.validate(config_obj, CONFIG_SCHEMA)
Esempio n. 23
0
def test_config_with_local_hooks_definition_fails(config_obj):
    with pytest.raises(schema.ValidationError):
        schema.validate(config_obj, CONFIG_SCHEMA)
Esempio n. 24
0
def test_config_with_local_hooks_definition_passes(config_obj):
    schema.validate(config_obj, CONFIG_SCHEMA)
Esempio n. 25
0
def is_valid_according_to_schema(obj, obj_schema):
    try:
        schema.validate(obj, obj_schema)
        return True
    except schema.ValidationError:
        return False
Esempio n. 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'"
Esempio n. 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'"
Esempio n. 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'),
    )
Esempio n. 29
0
def test_map_value_correct_type(schema):
    validate({'key': True}, schema)
Esempio n. 30
0
def test_optional_key_missing(schema):
    validate({}, schema)
Esempio n. 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'"
Esempio n. 32
0
def test_ok_both_types(v):
    validate(v, trivial_array_schema)
Esempio n. 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'"
Esempio n. 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'
Esempio n. 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)
Esempio n. 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'
Esempio n. 37
0
def _hook_from_manifest_dct(dct):
    dct = validate(apply_defaults(dct, MANIFEST_HOOK_DICT), MANIFEST_HOOK_DICT)
    dct = _hook(dct)
    return dct
Esempio n. 38
0
def test_ok_both_types(v):
    validate(v, trivial_array_schema)
Esempio n. 39
0
def is_valid_according_to_schema(obj, obj_schema):
    try:
        schema.validate(obj, obj_schema)
        return True
    except schema.ValidationError:
        return False
Esempio n. 40
0
def test_map_value_correct_type(schema):
    validate({'key': True}, schema)
Esempio n. 41
0
def test_config_with_local_hooks_definition_fails(config_obj):
    with pytest.raises(schema.ValidationError):
        schema.validate(config_obj, CONFIG_SCHEMA)
Esempio n. 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)