Merge pull request #2 from pre-commit/yaml_hook
Add a hook for yaml files.
This commit is contained in:
commit
cd4ce488aa
4
Makefile
4
Makefile
|
@ -3,6 +3,8 @@ TEST_TARGETS =
|
|||
ITEST_TARGETS = -m integration
|
||||
UTEST_TARGETS = -m "not(integration)"
|
||||
|
||||
DEBUG=
|
||||
|
||||
all: _tests
|
||||
|
||||
integration:
|
||||
|
@ -19,7 +21,7 @@ itests: itest
|
|||
itest: integration _tests
|
||||
|
||||
_tests: py_env
|
||||
bash -c 'source py_env/bin/activate && py.test tests $(TEST_TARGETS)'
|
||||
bash -c 'source py_env/bin/activate && py.test tests $(TEST_TARGETS) $(DEBUG)'
|
||||
|
||||
ucoverage: unit coverage
|
||||
icoverage: integration coverage
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
import argparse
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
from pre_commit_hooks.util import entry
|
||||
|
||||
|
||||
@entry
|
||||
def check_yaml(argv):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='+', help='Filenames to check.')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retval = 0
|
||||
for filename in args.filenames:
|
||||
try:
|
||||
yaml.load(open(filename))
|
||||
except yaml.YAMLError as e:
|
||||
print e
|
||||
retval = 1
|
||||
return retval
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(check_yaml())
|
6
setup.py
6
setup.py
|
@ -9,14 +9,16 @@ setup(
|
|||
'argparse',
|
||||
'plumbum',
|
||||
'pyflakes',
|
||||
'pyyaml',
|
||||
'simplejson',
|
||||
],
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
|
||||
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
|
||||
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
|
||||
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
|
||||
'end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:end_of_file_fixer',
|
||||
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
|
||||
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
|
||||
],
|
||||
},
|
||||
)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
# It's surprisingly hard to make invalid yaml
|
||||
a: "
|
|
@ -0,0 +1 @@
|
|||
im: ok yaml
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
import os.path
|
||||
|
||||
|
||||
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
def get_resource_path(path):
|
||||
return os.path.join(TESTING_DIR, 'resources', path)
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.check_yaml import check_yaml
|
||||
from testing.util import get_resource_path
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('filename', 'expected_retval'), (
|
||||
('bad_yaml.notyaml', 1),
|
||||
('ok_yaml.yaml', 0),
|
||||
))
|
||||
def test_check_yaml(filename, expected_retval):
|
||||
ret = check_yaml([get_resource_path(filename)])
|
||||
assert ret == expected_retval
|
Loading…
Reference in New Issue