Skip to content

Commit 54e83f7

Browse files
committed
Displaying the filename when the check-json hook fails with a UnicodeDecodeError - fix pre-commit#148
1 parent 4ef03c4 commit 54e83f7

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎pre_commit_hooks/check_json.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def check_json(argv=None):
1515
for filename in args.filenames:
1616
try:
1717
simplejson.load(open(filename))
18-
except simplejson.JSONDecodeError as exc:
18+
except (simplejson.JSONDecodeError, UnicodeDecodeError) as exc:
1919
print('{0}: Failed to json encode ({1})'.format(filename, exc))
2020
retval = 1
2121
return retval
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"�": 1,
3+
}

‎tests/check_json_test.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
@pytest.mark.parametrize(('filename', 'expected_retval'), (
88
('bad_json.notjson', 1),
9+
('bad_json_latin1.nonjson', 1),
910
('ok_json.json', 0),
1011
))
11-
def test_check_json(filename, expected_retval):
12+
def test_check_json(capsys, filename, expected_retval):
1213
ret = check_json([get_resource_path(filename)])
1314
assert ret == expected_retval
15+
if expected_retval == 1:
16+
stdout, _ = capsys.readouterr()
17+
assert filename in stdout

0 commit comments

Comments
 (0)