Merge pull request #2 from pre-commit/yaml_hook

Add a hook for yaml files.
This commit is contained in:
Anthony Sottile 2014-04-03 21:43:18 -07:00
commit cd4ce488aa
8 changed files with 59 additions and 3 deletions

View File

@ -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

View File

@ -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())

View File

@ -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
testing/__init__.py Normal file
View File

View File

@ -0,0 +1,2 @@
# It's surprisingly hard to make invalid yaml
a: "

View File

@ -0,0 +1 @@
im: ok yaml

9
testing/util.py Normal file
View File

@ -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)

14
tests/check_yaml_test.py Normal file
View File

@ -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