Add a hook for checking parseable json.
This commit is contained in:
parent
7ec4853521
commit
243fe50bc1
10
hooks.yaml
10
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)$
|
||||
|
|
|
@ -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())
|
|
@ -10,7 +10,7 @@ from pre_commit_hooks.util import entry
|
|||
@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
|
||||
|
|
3
setup.py
3
setup.py
|
@ -6,7 +6,7 @@ setup(
|
|||
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 @@ setup(
|
|||
],
|
||||
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',
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"hello": "world",
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"hello": "world"
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue