Merge pull request #2 from pre-commit/yaml_hook
Add a hook for yaml files.
diff --git a/Makefile b/Makefile
index 640e30d..08bd8b3 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,8 @@
ITEST_TARGETS = -m integration
UTEST_TARGETS = -m "not(integration)"
+DEBUG=
+
all: _tests
integration:
@@ -19,7 +21,7 @@
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
diff --git a/pre_commit_hooks/check_yaml.py b/pre_commit_hooks/check_yaml.py
new file mode 100644
index 0000000..210c6d6
--- /dev/null
+++ b/pre_commit_hooks/check_yaml.py
@@ -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())
diff --git a/setup.py b/setup.py
index 96d8d3a..4234288 100644
--- a/setup.py
+++ b/setup.py
@@ -9,14 +9,16 @@
'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',
],
},
)
diff --git a/testing/__init__.py b/testing/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testing/__init__.py
diff --git a/testing/resources/bad_yaml.notyaml b/testing/resources/bad_yaml.notyaml
new file mode 100644
index 0000000..3767076
--- /dev/null
+++ b/testing/resources/bad_yaml.notyaml
@@ -0,0 +1,2 @@
+# It's surprisingly hard to make invalid yaml
+a: "
diff --git a/testing/resources/ok_yaml.yaml b/testing/resources/ok_yaml.yaml
new file mode 100644
index 0000000..1679c64
--- /dev/null
+++ b/testing/resources/ok_yaml.yaml
@@ -0,0 +1 @@
+im: ok yaml
diff --git a/testing/util.py b/testing/util.py
new file mode 100644
index 0000000..c52a8cf
--- /dev/null
+++ b/testing/util.py
@@ -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)
diff --git a/tests/check_yaml_test.py b/tests/check_yaml_test.py
new file mode 100644
index 0000000..8985e8c
--- /dev/null
+++ b/tests/check_yaml_test.py
@@ -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