Add a hook for checking parseable json.
diff --git a/hooks.yaml b/hooks.yaml
index b371535..7b69d48 100644
--- a/hooks.yaml
+++ b/hooks.yaml
@@ -1,3 +1,9 @@
+- id: check-json
+ name: Check JSON
+ description: This hook checks json files for parseable syntax.
+ entry: check-json
+ language: python
+ files: \.json$
- id: check-yaml
name: Check Yaml
description: This hook checks yaml files for parseable syntax.
@@ -15,7 +21,7 @@
description: Ensures that a file is either empty, or ends with one newline.
entry: end-of-file-fixer
language: python
- files: \.(js|rb|md|py|scss|sh|tmpl|txt|yaml|yml)$
+ files: \.(js|json|rb|md|py|scss|sh|tmpl|txt|yaml|yml)$
- id: flake8
name: Flake8
description: This hook runs flake8.
@@ -39,4 +45,4 @@
description: This hook trims trailing whitespace.
entry: trailing-whitespace-fixer
language: python
- files: \.(js|rb|md|py|scss|sh|tmpl|txt|yaml|yml)$
+ files: \.(js|json|rb|md|py|scss|sh|tmpl|txt|yaml|yml)$
diff --git a/pre_commit_hooks/check_json.py b/pre_commit_hooks/check_json.py
new file mode 100644
index 0000000..3caaf34
--- /dev/null
+++ b/pre_commit_hooks/check_json.py
@@ -0,0 +1,27 @@
+from __future__ import print_function
+
+import argparse
+import sys
+import simplejson
+
+from pre_commit_hooks.util import entry
+
+
+@entry
+def check_json(argv):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('filenames', nargs='*', help='JSON filenames to check.')
+ args = parser.parse_args(argv)
+
+ retval = 0
+ for filename in args.filenames:
+ try:
+ simplejson.load(open(filename))
+ except simplejson.JSONDecodeError as e:
+ print('{0}: Failed to json encode ({1})'.format(filename, e))
+ retval = 1
+ return retval
+
+
+if __name__ == '__main__':
+ sys.exit(check_json())
diff --git a/pre_commit_hooks/check_yaml.py b/pre_commit_hooks/check_yaml.py
index 456f1f7..1de7de4 100644
--- a/pre_commit_hooks/check_yaml.py
+++ b/pre_commit_hooks/check_yaml.py
@@ -10,7 +10,7 @@
@entry
def check_yaml(argv):
parser = argparse.ArgumentParser()
- parser.add_argument('filenames', nargs='*', help='Filenames to check.')
+ parser.add_argument('filenames', nargs='*', help='Yaml filenames to check.')
args = parser.parse_args(argv)
retval = 0
diff --git a/setup.py b/setup.py
index b5df826..d69c730 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@
name='pre_commit_hooks',
description='Some out-of-the-box hooks for pre-commit.',
url='https://github.com/pre-commit/pre-commit-hooks',
- version='0.1.1',
+ version='0.2.0',
author='Anthony Sottile',
author_email='asottile@umich.edu',
@@ -32,6 +32,7 @@
],
entry_points={
'console_scripts': [
+ 'check-json = pre_commit_hooks.check_json:check_json',
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
'end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:end_of_file_fixer',
diff --git a/testing/resources/bad_json.notjson b/testing/resources/bad_json.notjson
new file mode 100644
index 0000000..66f1111
--- /dev/null
+++ b/testing/resources/bad_json.notjson
@@ -0,0 +1,3 @@
+{
+ "hello": "world",
+}
diff --git a/testing/resources/ok_json.json b/testing/resources/ok_json.json
new file mode 100644
index 0000000..ea50c5d
--- /dev/null
+++ b/testing/resources/ok_json.json
@@ -0,0 +1,3 @@
+{
+ "hello": "world"
+}
diff --git a/tests/check_json_test.py b/tests/check_json_test.py
new file mode 100644
index 0000000..18d1b66
--- /dev/null
+++ b/tests/check_json_test.py
@@ -0,0 +1,13 @@
+import pytest
+
+from pre_commit_hooks.check_json import check_json
+from testing.util import get_resource_path
+
+
+@pytest.mark.parametrize(('filename', 'expected_retval'), (
+ ('bad_json.notjson', 1),
+ ('ok_json.json', 0),
+))
+def test_check_json(filename, expected_retval):
+ ret = check_json([get_resource_path(filename)])
+ assert ret == expected_retval