Started implementing manifest validating

This commit is contained in:
Anthony Sottile
2014-03-13 12:36:25 -07:00
parent e06392134b
commit 3945f84e3f
11 changed files with 237 additions and 0 deletions

31
example_manifest.yaml Normal file
View File

@@ -0,0 +1,31 @@
# Hooks are set up as follows
# hooks:
# -
# id: hook_id
# name: 'Readable name'
# entry: my_hook_executable
#
# # Optional
# description: 'Longer description of the hook'
#
# # Optional, for now 'python[optional version]', 'ruby #.#.#', 'node'
# language: 'python'
#
# # Optional, defaults to zero
# expected_return_value: 0
hooks:
-
id: my_hook
name: My Simple Hook
description: This is my simple hook that does blah
entry: my-simple-hook.py
language: python
expected_return_value: 0
-
id: my_grep_based_hook
name: My Bash Based Hook
description: This is a hook that uses grep to validate some stuff
entry: ./my_grep_based_hook.sh
expected_return_value: 1

View File

View File

@@ -0,0 +1,98 @@
from __future__ import print_function
import argparse
import jsonschema
import jsonschema.exceptions
import os.path
import yaml
import pre_commit.constants as C
class InvalidManifestError(ValueError): pass
MANIFEST_JSON_SCHEMA = {
'type': 'object',
'properties': {
'hooks': {
'type': 'array',
'minItems': 1,
'items': {
'type': 'object',
'properties': {
'id': {'type': 'string'},
'name': {'type': 'string'},
'description': {'type': 'string'},
'entry': {'type': 'string'},
'language': {'type': 'string'},
'expected_return_value': {'type': 'number'},
},
'required': ['id', 'name', 'entry'],
},
},
},
'required': ['hooks'],
}
def check_is_valid_manifest(file_contents):
file_objects = yaml.load(file_contents)
jsonschema.validate(file_objects, MANIFEST_JSON_SCHEMA)
for hook_config in file_objects['hooks']:
language = hook_config.get('language')
if language is not None and not any(
language.startswith(lang) for lang in C.SUPPORTED_LANGUAGES
):
raise InvalidManifestError(
'Expected language {0} for {1} to start with one of {2!r}'.format(
hook_config['id'],
hook_config['language'],
C.SUPPORTED_LANGUAGES,
)
)
def run(argv):
parser = argparse.ArgumentParser()
parser.add_argument(
'--filename',
required=False, default=None,
help='Manifest filename. Defaults to {0} at root of git repo'.format(
C.MANIFEST_FILE,
)
)
args = parser.parse_args(argv)
if args.filename is None:
# TODO: filename = git.get_root() + C.MANIFEST_FILE
raise NotImplementedError
else:
filename = args.filename
if not os.path.exists(filename):
print('File {0} does not exist'.format(filename))
return 1
file_contents = open(filename, 'r').read()
try:
yaml.load(file_contents)
except Exception as e:
print('File {0} is not a valid yaml file'.format(filename))
print(str(e))
return 1
try:
check_is_valid_manifest(file_contents)
except (jsonschema.exceptions.ValidationError, InvalidManifestError) as e:
print('File {0} is not a valid manifest file'.format(filename))
print(str(e))
return 1
return 0

View File

@@ -2,3 +2,11 @@
PRE_COMMIT_FILE = '.pre-commit-config.yaml'
PRE_COMMIT_DIR = '.pre-commit-files'
MANIFEST_FILE = 'manifest.yaml'
SUPPORTED_LANGUAGES = [
'python',
'ruby',
'node',
]

View File

@@ -1,4 +1,5 @@
argparse
jsonschema
pyyaml
simplejson

8
scripts/validate-manifest.py Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env python
if __name__ == '__main__':
import sys
from pre_commit.clientlib.validate_manifest import run
sys.exit(run(sys.argv[1:]))

View File

@@ -7,9 +7,12 @@ setup(
packages=find_packages('.', exclude=('tests*', 'testing*')),
install_requires=[
'argparse',
'jsonschema',
'pyyaml',
'simplejson',
],
scripts=[
'scripts/pre-commit.py',
'scripts/validate-manifest.py',
],
)

View File

View File

@@ -0,0 +1,86 @@
import __builtin__
import jsonschema
import pytest
import mock
from pre_commit.clientlib.validate_manifest import check_is_valid_manifest
from pre_commit.clientlib.validate_manifest import InvalidManifestError
from pre_commit.clientlib.validate_manifest import run
@pytest.yield_fixture
def print_mock():
with mock.patch.object(__builtin__, 'print', autospec=True) as print_mock_obj:
yield print_mock_obj
def test_run_returns_1_for_non_existent_module(print_mock):
non_existent_filename = 'file_that_does_not_exist'
ret = run(['--filename', non_existent_filename])
assert ret == 1
print_mock.assert_called_once_with(
'File {0} does not exist'.format(non_existent_filename),
)
def test_run_returns_1_for_non_yaml_file(print_mock):
non_parseable_filename = 'tests/data/non_parseable_yaml_file.yaml'
ret = run(['--filename', non_parseable_filename])
assert ret == 1
print_mock.assert_any_call(
'File {0} is not a valid yaml file'.format(non_parseable_filename),
)
def test_returns_1_for_valid_yaml_file_but_invalid_manifest(print_mock):
invalid_manifest = 'tests/data/valid_yaml_but_invalid_manifest.yaml'
ret = run(['--filename', invalid_manifest])
assert ret == 1
print_mock.assert_any_call(
'File {0} is not a valid manifest file'.format(invalid_manifest)
)
def test_returns_0_for_valid_manifest():
valid_manifest = 'example_manifest.yaml'
ret = run(['--filename', valid_manifest])
assert ret == 0
@pytest.mark.parametrize(('manifest', 'expected_exception_type'), (
(
"""
hooks:
-
id: foo
entry: foo
""",
jsonschema.exceptions.ValidationError,
),
(
"""
hooks:
-
id: foo
name: Foo
language: Not a Language lol
entry: foo
""",
InvalidManifestError,
),
))
def test_check_invalid_manifests(manifest, expected_exception_type):
with pytest.raises(expected_exception_type):
check_is_valid_manifest(manifest)
def test_valid_manifest_is_valid():
check_is_valid_manifest("""
hooks:
-
id: foo
name: Foo
entry: foo
language: python>2.6
""")

View File

@@ -0,0 +1 @@
foo: "

View File

@@ -0,0 +1 @@
foo: bar