mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 12:06:53 +00:00
Merge pull request #149 from Lucas-C/master
Displaying the filename when the check-json hook fails with a UnicodeDecodeError - fix #148
This commit is contained in:
commit
d71b52859b
3 changed files with 9 additions and 2 deletions
|
|
@ -15,7 +15,7 @@ def check_json(argv=None):
|
||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
try:
|
try:
|
||||||
simplejson.load(open(filename))
|
simplejson.load(open(filename))
|
||||||
except simplejson.JSONDecodeError as exc:
|
except (simplejson.JSONDecodeError, UnicodeDecodeError) as exc:
|
||||||
print('{0}: Failed to json encode ({1})'.format(filename, exc))
|
print('{0}: Failed to json encode ({1})'.format(filename, exc))
|
||||||
retval = 1
|
retval = 1
|
||||||
return retval
|
return retval
|
||||||
|
|
|
||||||
3
testing/resources/bad_json_latin1.nonjson
Executable file
3
testing/resources/bad_json_latin1.nonjson
Executable file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"И": 1,
|
||||||
|
}
|
||||||
|
|
@ -6,8 +6,12 @@ from testing.util import get_resource_path
|
||||||
|
|
||||||
@pytest.mark.parametrize(('filename', 'expected_retval'), (
|
@pytest.mark.parametrize(('filename', 'expected_retval'), (
|
||||||
('bad_json.notjson', 1),
|
('bad_json.notjson', 1),
|
||||||
|
('bad_json_latin1.nonjson', 1),
|
||||||
('ok_json.json', 0),
|
('ok_json.json', 0),
|
||||||
))
|
))
|
||||||
def test_check_json(filename, expected_retval):
|
def test_check_json(capsys, filename, expected_retval):
|
||||||
ret = check_json([get_resource_path(filename)])
|
ret = check_json([get_resource_path(filename)])
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
if expected_retval == 1:
|
||||||
|
stdout, _ = capsys.readouterr()
|
||||||
|
assert filename in stdout
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue