From 31e740ed055dabb84c743d1e390d56b62f6e0264 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Sun, 15 Sep 2019 13:48:00 -0400 Subject: [PATCH] Use unified_diff --- pre_commit_hooks/pretty_format_json.py | 2 +- tests/pretty_format_json_test.py | 32 ++++++++++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 3967c30..f5476a5 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -59,7 +59,7 @@ def parse_topkeys(s): # type: (str) -> List[str] def get_diff(source, target): # type: (str, str) -> str source_lines = source.splitlines(True) target_lines = target.splitlines(True) - diff = ''.join(difflib.ndiff(source_lines, target_lines)) + diff = ''.join(difflib.unified_diff(source_lines, target_lines)) return diff diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 04e4614..396c5e4 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -111,22 +111,24 @@ def test_diffing_output(capsys): resource_path = get_resource_path('not_pretty_formatted_json.json') expected_retval = 1 expected_out = '''\ - { -- "foo": -- "bar", -- "alist": [2, 34, 234], -+ "alist": [ -+ 2, -+ 34, -+ 234 -+ ], -- "blah": null -+ "blah": null, -? + -+ "foo": "bar" - } +--- ++++ +@@ -1,6 +1,9 @@ + { +- "foo": +- "bar", +- "alist": [2, 34, 234], +- "blah": null ++ "alist": [ ++ 2, ++ 34, ++ 234 ++ ], ++ "blah": null, ++ "foo": "bar" + } -''' +''' # noqa: W291 expected_err = 'File {} is not pretty-formatted\n'.format(resource_path) actual_retval = main([resource_path])