From c11c5483d69995bb8ae462b75f5532ce6ae02a77 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 14 May 2020 16:29:55 -0700 Subject: [PATCH] check-json: resolve TODO --- pre_commit_hooks/check_json.py | 3 +-- tests/check_json_test.py | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pre_commit_hooks/check_json.py b/pre_commit_hooks/check_json.py index 25dbfd9..6026270 100644 --- a/pre_commit_hooks/check_json.py +++ b/pre_commit_hooks/check_json.py @@ -14,8 +14,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: with open(filename, 'rb') as f: try: json.load(f) - # TODO: need UnicodeDecodeError? - except (ValueError, UnicodeDecodeError) as exc: + except ValueError as exc: print(f'{filename}: Failed to json decode ({exc})') retval = 1 return retval diff --git a/tests/check_json_test.py b/tests/check_json_test.py index 6654ed1..c63dc4c 100644 --- a/tests/check_json_test.py +++ b/tests/check_json_test.py @@ -17,3 +17,9 @@ def test_main(capsys, filename, expected_retval): if expected_retval == 1: stdout, _ = capsys.readouterr() assert filename in stdout + + +def test_non_utf8_file(tmpdir): + f = tmpdir.join('t.json') + f.write_binary(b'\xa9\xfe\x12') + assert main((str(f),))