From 3e9db01775648dc08689dd1bff3da2d54fc8ffda Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Thu, 15 Aug 2019 12:32:33 -0400 Subject: [PATCH 01/13] Add logic to print line number of JSON errors This commit makes the pretty JSON check more verbose when it encounters errors, that way developers can see which lines are causing errors in order to debug. --- pre_commit_hooks/pretty_format_json.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index e734ca8..f42630a 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -115,6 +115,17 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int if contents != pretty_contents: print('File {} is not pretty-formatted'.format(json_file)) + contents_by_line = ''.join(contents).split('\n') + pretty_contents_by_line = ''.join(pretty_contents).split('\n') + + diff = len(contents_by_line) - len(pretty_contents_by_line) + if diff > 0: + pretty_contents_by_line.extend([''] * diff) + + for line_num, line in enumerate(contents_by_line): + if line != pretty_contents_by_line[line_num]: + print('{}:{}'.format(json_file, line_num)) + if args.autofix: _autofix(json_file, pretty_contents) From 780f20249f8fdf4a1c3cb53eb8c5d02f40dace2a Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Fri, 16 Aug 2019 12:38:41 -0400 Subject: [PATCH 02/13] Add option to show expected output This prints a diff between the given json file and the expected (pretty) output, with this functionality hidden behind a cli flag --- pre_commit_hooks/pretty_format_json.py | 28 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index f42630a..475bf1c 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -4,6 +4,7 @@ import argparse import io import json import sys +import difflib from collections import OrderedDict from typing import List from typing import Mapping @@ -55,6 +56,14 @@ def parse_topkeys(s): # type: (str) -> List[str] return s.split(',') +def get_diff(source, target): + source_lines = ''.join(source).split('\n') + target_lines = ''.join(target).split('\n') + d = difflib.Differ() + diff = d.compare(source_lines, target_lines) + return '\n'.join(diff) + + def main(argv=None): # type: (Optional[Sequence[str]]) -> int parser = argparse.ArgumentParser() parser.add_argument( @@ -96,6 +105,13 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int default=[], help='Ordered list of keys to keep at the top of JSON hashes', ) + parser.add_argument( + '--show-expected', + action='store_true', + dest='show_expected', + default=False, + help='Show a diff between the input file and expected (pretty) output', + ) parser.add_argument('filenames', nargs='*', help='Filenames to fix') args = parser.parse_args(argv) @@ -115,16 +131,8 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int if contents != pretty_contents: print('File {} is not pretty-formatted'.format(json_file)) - contents_by_line = ''.join(contents).split('\n') - pretty_contents_by_line = ''.join(pretty_contents).split('\n') - - diff = len(contents_by_line) - len(pretty_contents_by_line) - if diff > 0: - pretty_contents_by_line.extend([''] * diff) - - for line_num, line in enumerate(contents_by_line): - if line != pretty_contents_by_line[line_num]: - print('{}:{}'.format(json_file, line_num)) + if args.show_expected: + print(get_diff(contents, pretty_contents)) if args.autofix: _autofix(json_file, pretty_contents) From b28837a0389dca2fe401a13aabc7e668450ce968 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Fri, 23 Aug 2019 14:14:10 -0400 Subject: [PATCH 03/13] Add test case to test diffing function --- pre_commit_hooks/pretty_format_json.py | 6 +- tests/pretty_format_json_test.py | 76 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 475bf1c..2e04064 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -1,10 +1,10 @@ from __future__ import print_function import argparse +import difflib import io import json import sys -import difflib from collections import OrderedDict from typing import List from typing import Mapping @@ -56,7 +56,7 @@ def parse_topkeys(s): # type: (str) -> List[str] return s.split(',') -def get_diff(source, target): +def get_diff(source, target): # type: (List[str], List[str]) -> str source_lines = ''.join(source).split('\n') target_lines = ''.join(target).split('\n') d = difflib.Differ() @@ -132,7 +132,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int print('File {} is not pretty-formatted'.format(json_file)) if args.show_expected: - print(get_diff(contents, pretty_contents)) + print(get_diff(contents, list(pretty_contents))) if args.autofix: _autofix(json_file, pretty_contents) diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 3b7b9a2..bd84431 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -3,6 +3,7 @@ import shutil import pytest from six import PY2 +from pre_commit_hooks.pretty_format_json import get_diff from pre_commit_hooks.pretty_format_json import main from pre_commit_hooks.pretty_format_json import parse_num_to_int from testing.util import get_resource_path @@ -105,3 +106,78 @@ def test_top_sorted_get_pretty_format(): def test_badfile_main(): ret = main([get_resource_path('ok_yaml.yaml')]) assert ret == 1 + + +def test_diffing_output(): + table_tests = [ + { + 'name': 'diff_test_1', + 'source': """ +{ + "key1": "val1", + "key2": 2, +"array_key": [1, 2, 3], + "object":{ + "bool_key": true + } +} +""", + 'target': """ +{ + "array_key": [ + 1, + 2, + 3 + ], + "key1": "val1", + "key2": 2, + "object": { + "bool_key": true + } +} +""", + 'expected': """ +{ ++ "array_key": [ ++ 1, ++ 2, ++ 3 ++ ], +- "key1": "val1", +? -- + ++ "key1": "val1", +- "key2": 2, +? -- -- + ++ "key2": 2, +- "array_key": [1, 2, 3], +- "object":{ +? ------ + ++ "object": { +? + + +- "bool_key": true +? ---- + ++ "bool_key": true +- } ++ } + } +""", + }, + { + 'name': 'diff_test_2', + 'source': '', + 'target': '', + 'expected': '', + }, + + ] + for test in table_tests: + s = list(test['source']) + t = list(test['target']) + expected = test['expected'].strip() + actual = get_diff(s, t).strip() + assert actual == expected From 0ff23d4e9fcaf66565206aa2a30cfdea53477f9d Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Fri, 13 Sep 2019 14:30:52 -0400 Subject: [PATCH 04/13] Remove extra cli flag, and update test case This commit uses capsys to test the output of the diff, which is now hidden behind the autofix flag if it's disabled --- pre_commit_hooks/pretty_format_json.py | 25 ++---- tests/pretty_format_json_test.py | 114 ++++++++++--------------- 2 files changed, 52 insertions(+), 87 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 2e04064..3626332 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -56,12 +56,12 @@ def parse_topkeys(s): # type: (str) -> List[str] return s.split(',') -def get_diff(source, target): # type: (List[str], List[str]) -> str - source_lines = ''.join(source).split('\n') - target_lines = ''.join(target).split('\n') - d = difflib.Differ() - diff = d.compare(source_lines, target_lines) - return '\n'.join(diff) +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)) + print(diff) + return diff def main(argv=None): # type: (Optional[Sequence[str]]) -> int @@ -105,14 +105,6 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int default=[], help='Ordered list of keys to keep at the top of JSON hashes', ) - parser.add_argument( - '--show-expected', - action='store_true', - dest='show_expected', - default=False, - help='Show a diff between the input file and expected (pretty) output', - ) - parser.add_argument('filenames', nargs='*', help='Filenames to fix') args = parser.parse_args(argv) @@ -131,11 +123,10 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int if contents != pretty_contents: print('File {} is not pretty-formatted'.format(json_file)) - if args.show_expected: - print(get_diff(contents, list(pretty_contents))) - if args.autofix: _autofix(json_file, pretty_contents) + else: + print(get_diff(''.join(contents), pretty_contents)) status = 1 except ValueError: diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index bd84431..3aca165 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -3,7 +3,6 @@ import shutil import pytest from six import PY2 -from pre_commit_hooks.pretty_format_json import get_diff from pre_commit_hooks.pretty_format_json import main from pre_commit_hooks.pretty_format_json import parse_num_to_int from testing.util import get_resource_path @@ -108,76 +107,51 @@ def test_badfile_main(): assert ret == 1 -def test_diffing_output(): - table_tests = [ - { - 'name': 'diff_test_1', - 'source': """ -{ - "key1": "val1", - "key2": 2, -"array_key": [1, 2, 3], - "object":{ - "bool_key": true - } -} -""", - 'target': """ -{ - "array_key": [ - 1, - 2, - 3 - ], - "key1": "val1", - "key2": 2, - "object": { - "bool_key": true - } -} -""", - 'expected': """ -{ -+ "array_key": [ -+ 1, +def test_diffing_output(capsys): + resource_path = get_resource_path('not_pretty_formatted_json.json') + expected_retval = 1 + expected_diff = ''' + { +- "foo": +- "bar", +- "alist": [2, 34, 234], ++ "alist": [ + 2, -+ 3 ++ 34, ++ 234 + ], -- "key1": "val1", -? -- - -+ "key1": "val1", -- "key2": 2, -? -- -- - -+ "key2": 2, -- "array_key": [1, 2, 3], -- "object":{ -? ------ - -+ "object": { -? + - -- "bool_key": true -? ---- - -+ "bool_key": true -- } -+ } +- "blah": null ++ "blah": null, +? + ++ "foo": "bar" } -""", - }, - { - 'name': 'diff_test_2', - 'source': '', - 'target': '', - 'expected': '', - }, - ] - for test in table_tests: - s = list(test['source']) - t = list(test['target']) - expected = test['expected'].strip() - actual = get_diff(s, t).strip() - assert actual == expected + { +- "foo": +- "bar", +- "alist": [2, 34, 234], ++ "alist": [ ++ 2, ++ 34, ++ 234 ++ ], +- "blah": null ++ "blah": null, +? + ++ "foo": "bar" + } + + +''' + # output should include a line with the filepath, build it here + file_output_line = 'File {} is not pretty-formatted'.format(resource_path) + # prepend the above line to the diff + expected_output = file_output_line + expected_diff + + actual_retval = main([resource_path]) + actual_output = capsys.readouterr() + + assert actual_retval == expected_retval + + actual_output = '\n'.join(actual_output) + assert actual_output == expected_output From 93b7b66cda7e00b2d9c7dd503a4fedbfa9802eaf Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Fri, 13 Sep 2019 14:38:40 -0400 Subject: [PATCH 05/13] Remove erroneoous print statement --- pre_commit_hooks/pretty_format_json.py | 1 - tests/pretty_format_json_test.py | 15 --------------- 2 files changed, 16 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 3626332..8bf61c0 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -60,7 +60,6 @@ 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)) - print(diff) return diff diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 3aca165..d95465d 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -126,21 +126,6 @@ def test_diffing_output(capsys): + "foo": "bar" } - { -- "foo": -- "bar", -- "alist": [2, 34, 234], -+ "alist": [ -+ 2, -+ 34, -+ 234 -+ ], -- "blah": null -+ "blah": null, -? + -+ "foo": "bar" - } - ''' # output should include a line with the filepath, build it here From 831d2a99abca73bfe216617bb478723a1e2ed301 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Sun, 15 Sep 2019 12:51:03 -0400 Subject: [PATCH 06/13] Print file line stderr --- pre_commit_hooks/pretty_format_json.py | 5 ++++- tests/pretty_format_json_test.py | 17 ++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 8bf61c0..b9c0aa4 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -120,7 +120,10 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int ) if contents != pretty_contents: - print('File {} is not pretty-formatted'.format(json_file)) + print( + 'File {} is not pretty-formatted'.format(json_file), + file=sys.stderr, + ) if args.autofix: _autofix(json_file, pretty_contents) diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index d95465d..9c380a0 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -108,10 +108,8 @@ def test_badfile_main(): def test_diffing_output(capsys): - resource_path = get_resource_path('not_pretty_formatted_json.json') expected_retval = 1 - expected_diff = ''' - { + expected_out = ''' { - "foo": - "bar", - "alist": [2, 34, 234], @@ -126,17 +124,10 @@ def test_diffing_output(capsys): + "foo": "bar" } - ''' - # output should include a line with the filepath, build it here - file_output_line = 'File {} is not pretty-formatted'.format(resource_path) - # prepend the above line to the diff - expected_output = file_output_line + expected_diff - actual_retval = main([resource_path]) - actual_output = capsys.readouterr() + actual_retval = main([get_resource_path('not_pretty_formatted_json.json')]) + out, err = capsys.readouterr() assert actual_retval == expected_retval - - actual_output = '\n'.join(actual_output) - assert actual_output == expected_output + assert out == expected_out From 35c76c4f33ec1d9fa4fafeedc90798d6338200fd Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Sun, 15 Sep 2019 12:54:03 -0400 Subject: [PATCH 07/13] Add test for std err --- tests/pretty_format_json_test.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 9c380a0..3263b76 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -108,6 +108,7 @@ def test_badfile_main(): def test_diffing_output(capsys): + resource_path = get_resource_path('not_pretty_formatted_json.json') expected_retval = 1 expected_out = ''' { - "foo": @@ -125,9 +126,11 @@ def test_diffing_output(capsys): } ''' + expected_err = 'File {} is not pretty-formatted\n'.format(resource_path) - actual_retval = main([get_resource_path('not_pretty_formatted_json.json')]) - out, err = capsys.readouterr() + actual_retval = main([resource_path]) + actual_out, actual_err = capsys.readouterr() assert actual_retval == expected_retval - assert out == expected_out + assert actual_out == expected_out + assert actual_err == expected_err From 27cd688c8eceae8946a1ea18ed5e8255fc9ed926 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Sun, 15 Sep 2019 13:29:52 -0400 Subject: [PATCH 08/13] Flush stderr, and formatting --- pre_commit_hooks/pretty_format_json.py | 1 + tests/pretty_format_json_test.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index b9c0aa4..3967c30 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -124,6 +124,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int 'File {} is not pretty-formatted'.format(json_file), file=sys.stderr, ) + sys.stderr.flush() if args.autofix: _autofix(json_file, pretty_contents) diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 3263b76..04e4614 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -110,7 +110,8 @@ def test_badfile_main(): def test_diffing_output(capsys): resource_path = get_resource_path('not_pretty_formatted_json.json') expected_retval = 1 - expected_out = ''' { + expected_out = '''\ + { - "foo": - "bar", - "alist": [2, 34, 234], From 31e740ed055dabb84c743d1e390d56b62f6e0264 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Sun, 15 Sep 2019 13:48:00 -0400 Subject: [PATCH 09/13] 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]) From 7d878b55ff2a0ddeb277b0545ee0c7f506b2a886 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Tue, 24 Sep 2019 15:21:45 -0400 Subject: [PATCH 10/13] Fix test cases for unified_diff --- tests/pretty_format_json_test.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 396c5e4..d31fe91 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -111,9 +111,7 @@ def test_diffing_output(capsys): resource_path = get_resource_path('not_pretty_formatted_json.json') expected_retval = 1 expected_out = '''\ ---- -+++ -@@ -1,6 +1,9 @@ +--- \n+++ \n@@ -1,6 +1,9 @@ { - "foo": - "bar", @@ -128,7 +126,7 @@ def test_diffing_output(capsys): + "foo": "bar" } -''' # noqa: W291 +''' expected_err = 'File {} is not pretty-formatted\n'.format(resource_path) actual_retval = main([resource_path]) From ec6c39ee621461aa95e0cbcdd99189f331e31ce5 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Tue, 24 Sep 2019 15:42:24 -0400 Subject: [PATCH 11/13] Print filenames in the diff --- pre_commit_hooks/pretty_format_json.py | 16 +++++++++++----- tests/pretty_format_json_test.py | 13 +++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index f5476a5..fa83305 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -1,11 +1,11 @@ from __future__ import print_function import argparse -import difflib import io import json import sys from collections import OrderedDict +from difflib import unified_diff from typing import List from typing import Mapping from typing import Optional @@ -56,11 +56,11 @@ def parse_topkeys(s): # type: (str) -> List[str] return s.split(',') -def get_diff(source, target): # type: (str, str) -> str +def get_diff(source, target, file): # type: (str, str, str) -> str source_lines = source.splitlines(True) target_lines = target.splitlines(True) - diff = ''.join(difflib.unified_diff(source_lines, target_lines)) - return diff + diff = unified_diff(source_lines, target_lines, fromfile=file, tofile=file) + return ''.join(diff) def main(argv=None): # type: (Optional[Sequence[str]]) -> int @@ -129,7 +129,13 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int if args.autofix: _autofix(json_file, pretty_contents) else: - print(get_diff(''.join(contents), pretty_contents)) + print( + get_diff( + ''.join(contents), + pretty_contents, + json_file, + ), + ) status = 1 except ValueError: diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index d31fe91..7020c4c 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -1,3 +1,4 @@ +import os import shutil import pytest @@ -110,9 +111,13 @@ def test_badfile_main(): def test_diffing_output(capsys): resource_path = get_resource_path('not_pretty_formatted_json.json') expected_retval = 1 + a = os.path.join('a', resource_path) + b = os.path.join('b', resource_path) expected_out = '''\ ---- \n+++ \n@@ -1,6 +1,9 @@ - { +--- {} ++++ {} +@@ -1,6 +1,9 @@ + {{ - "foo": - "bar", - "alist": [2, 34, 234], @@ -124,9 +129,9 @@ def test_diffing_output(capsys): + ], + "blah": null, + "foo": "bar" - } + }} -''' +'''.format(a, b) expected_err = 'File {} is not pretty-formatted\n'.format(resource_path) actual_retval = main([resource_path]) From ea8fbf9cf0d132a91af13729b766a3a31b8bfdac Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Tue, 24 Sep 2019 15:47:53 -0400 Subject: [PATCH 12/13] Remove unneeded string join --- pre_commit_hooks/pretty_format_json.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index fa83305..642ed65 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -129,13 +129,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int if args.autofix: _autofix(json_file, pretty_contents) else: - print( - get_diff( - ''.join(contents), - pretty_contents, - json_file, - ), - ) + print(get_diff(contents, pretty_contents, json_file)) status = 1 except ValueError: From d6c0aa5a3540a55e0fddb18870a308f96922e402 Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Tue, 24 Sep 2019 16:27:34 -0400 Subject: [PATCH 13/13] Remove useless newline --- pre_commit_hooks/pretty_format_json.py | 5 ++++- tests/pretty_format_json_test.py | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 642ed65..cbc1b19 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -129,7 +129,10 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int if args.autofix: _autofix(json_file, pretty_contents) else: - print(get_diff(contents, pretty_contents, json_file)) + print( + get_diff(contents, pretty_contents, json_file), + end='', + ) status = 1 except ValueError: diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 7020c4c..6cfe772 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py @@ -130,7 +130,6 @@ def test_diffing_output(capsys): + "blah": null, + "foo": "bar" }} - '''.format(a, b) expected_err = 'File {} is not pretty-formatted\n'.format(resource_path)