Esempio n. 1
0
def test_config_with_local_hooks_definition_fails(config_obj):
    with pytest.raises((
        jsonschema.exceptions.ValidationError, InvalidConfigError
    )):
        jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
        config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
        validate_config_extra(config)
Esempio n. 2
0
    def validate(filename, load_strategy=yaml.load):
        if not os.path.exists(filename):
            raise exception_type('File {} does not exist'.format(filename))

        file_contents = open(filename, 'r').read()

        try:
            obj = load_strategy(file_contents)
        except Exception as e:
            raise exception_type(
                'Invalid yaml: {}\n{}'.format(os.path.relpath(filename), e),
            )

        try:
            jsonschema.validate(obj, json_schema)
        except jsonschema.exceptions.ValidationError as e:
            raise exception_type(
                'Invalid content: {}\n{}'.format(
                    os.path.relpath(filename), e
                ),
            )

        obj = apply_defaults(obj, json_schema)

        additional_validation_strategy(obj)

        return obj
Esempio n. 3
0
    def validate(filename, load_strategy=yaml.load):
        if not os.path.exists(filename):
            raise exception_type('File {0} does not exist'.format(filename))

        file_contents = open(filename, 'r').read()

        try:
            obj = load_strategy(file_contents)
        except Exception as e:
            raise exception_type(
                'Invalid yaml: {0}\n{1}'.format(os.path.relpath(filename),
                                                e), )

        try:
            jsonschema.validate(obj, json_schema)
        except jsonschema.exceptions.ValidationError as e:
            raise exception_type(
                'Invalid content: {0}\n{1}'.format(os.path.relpath(filename),
                                                   e), )

        obj = apply_defaults(obj, json_schema)

        additional_validation_strategy(obj)

        return obj
Esempio n. 4
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_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
        validate_config_extra(wrapped_config)
        return wrapped_config[0]
    else:
        return config
Esempio n. 5
0
def mock_repo_config():
    config = {
        "repo": "[email protected]:pre-commit/pre-commit-hooks",
        "sha": "5e713f8878b7d100c0e059f8cc34be4fc2e8f897",
        "hooks": [{"id": "pyflakes", "files": "\\.py$"}],
    }
    config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
    validate_config_extra(config_wrapped)
    return config_wrapped[0]
Esempio n. 6
0
 def hooks(self):
     return tuple((
         hook['id'],
         _validate_minimum_version(
             apply_defaults(
                 hook,
                 MANIFEST_JSON_SCHEMA['items'],
             )),
     ) for hook in self.repo_config['hooks'])
Esempio n. 7
0
def test_config_with_ok_exclude_regex_passes():
    config = apply_defaults(
        [{
            'repo': 'foo',
            'sha': 'foo',
            'hooks': [{'id': 'hook_id', 'files': '', 'exclude': '^vendor/'}],
        }],
        CONFIG_JSON_SCHEMA,
    )
    validate_config_extra(config)
Esempio n. 8
0
def test_config_with_ok_regexes_passes():
    config = apply_defaults(
        [{
            'repo': 'foo',
            'sha': 'foo',
            'hooks': [{'id': 'hook_id', 'files': '\\.py$'}],
        }],
        CONFIG_JSON_SCHEMA,
    )
    validate_config_extra(config)
Esempio n. 9
0
def mock_repo_config():
    config = {
        'repo': '[email protected]:pre-commit/pre-commit-hooks',
        'sha': '5e713f8878b7d100c0e059f8cc34be4fc2e8f897',
        'hooks': [{
            'id': 'pyflakes',
            'files': '\\.py$',
        }],
    }
    config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
    validate_config_extra(config_wrapped)
    return config_wrapped[0]
Esempio n. 10
0
def mock_repo_config():
    config = {
        'repo': '[email protected]:pre-commit/pre-commit-hooks',
        'sha': '5e713f8878b7d100c0e059f8cc34be4fc2e8f897',
        'hooks': [{
            'id': 'pyflakes',
            'files': '\\.py$',
        }],
    }
    config_wrapped = apply_defaults([config], CONFIG_JSON_SCHEMA)
    validate_config_extra(config_wrapped)
    return config_wrapped[0]
Esempio n. 11
0
def test_config_with_invalid_exclude_regex_fails():
    with pytest.raises(InvalidConfigError):
        # 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_apply_defaults_applies_defaults():
    ret = apply_defaults(
        {'foo': 'bar'},
        {
            'type': 'object',
            'properties': {
                'foo': {'default': 'biz'},
                'baz': {'default': 'herp'},
            }
        }
    )
    assert ret == {'foo': 'bar', 'baz': 'herp'}
def test_config_with_ok_regexes_passes():
    config = apply_defaults(
        [{
            'repo': 'foo',
            'sha': 'foo',
            'hooks': [{
                'id': 'hook_id',
                'files': '\\.py$'
            }],
        }],
        CONFIG_JSON_SCHEMA,
    )
    validate_config_extra(config)
def test_config_with_ok_exclude_regex_passes():
    config = apply_defaults(
        [{
            'repo': 'foo',
            'sha': 'foo',
            'hooks': [{
                'id': 'hook_id',
                'files': '',
                'exclude': '^vendor/'
            }],
        }],
        CONFIG_JSON_SCHEMA,
    )
    validate_config_extra(config)
def test_config_with_failing_regexes_fails():
    with pytest.raises(InvalidConfigError):
        # Note the regex '(' is invalid (unbalanced parens)
        config = apply_defaults(
            [{
                'repo': 'foo',
                'sha': 'foo',
                'hooks': [{
                    'id': 'hook_id',
                    'files': '('
                }],
            }],
            CONFIG_JSON_SCHEMA,
        )
        validate_config_extra(config)
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', repo_path),
        ('sha', sha or get_head_sha(repo_path)),
        (
            'hooks',
            hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
        ),
    ))

    if check:
        wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
        validate_config_extra(wrapped_config)
        return wrapped_config[0]
    else:
        return config
def test_apply_defaults_deep():
    ret = apply_defaults(
        {'foo': {'bar': {}}},
        {
            'type': 'object',
            'properties': {
                'foo': {
                    'type': 'object',
                    'properties': {
                        'bar': {
                            'type': 'object',
                            'properties': {'baz': {'default': 'herp'}},
                        },
                    },
                },
            },
        },
    )
    assert ret == {'foo': {'bar': {'baz': 'herp'}}}
def test_config_with_local_hooks_definition_fails(config_obj):
    with pytest.raises(
        (jsonschema.exceptions.ValidationError, InvalidConfigError)):
        jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
        config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
        validate_config_extra(config)
Esempio n. 19
0
 def hooks(self):
     return tuple(
         (hook['id'], apply_defaults(hook, MANIFEST_JSON_SCHEMA['items']))
         for hook in self.repo_config['hooks']
     )
def test_apply_defaults_copies():
    schema = {'properties': {'foo': {'default': []}}}
    ret1 = apply_defaults({}, schema)
    ret2 = apply_defaults({}, schema)
    assert ret1['foo'] is not ret2['foo']
Esempio n. 21
0
def test_config_with_local_hooks_definition_passes(config_obj):
    jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
    config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
    validate_config_extra(config)
def test_apply_defaults_copies_object():
    input = {}
    ret = apply_defaults(input, {})
    assert ret is not input
def test_apply_default_does_not_touch_schema_without_defaults():
    ret = apply_defaults(
        {'foo': 'bar'},
        {'type': 'object', 'properties': {'foo': {}, 'baz': {}}},
    )
    assert ret == {'foo': 'bar'}
def test_config_with_local_hooks_definition_passes(config_obj):
    jsonschema.validate(config_obj, CONFIG_JSON_SCHEMA)
    config = apply_defaults(config_obj, CONFIG_JSON_SCHEMA)
    validate_config_extra(config)