Skip to content

Commit 277f875

Browse files
authored
Merge pull request #408 from joepin/pretty-json-print-lines
Add logic to print diff between expected and actual JSON
2 parents 0408045 + d6c0aa5 commit 277f875

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import sys
77
from collections import OrderedDict
8+
from difflib import unified_diff
89
from typing import List
910
from typing import Mapping
1011
from typing import Optional
@@ -55,6 +56,13 @@ def parse_topkeys(s): # type: (str) -> List[str]
5556
return s.split(',')
5657

5758

59+
def get_diff(source, target, file): # type: (str, str, str) -> str
60+
source_lines = source.splitlines(True)
61+
target_lines = target.splitlines(True)
62+
diff = unified_diff(source_lines, target_lines, fromfile=file, tofile=file)
63+
return ''.join(diff)
64+
65+
5866
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
5967
parser = argparse.ArgumentParser()
6068
parser.add_argument(
@@ -96,7 +104,6 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
96104
default=[],
97105
help='Ordered list of keys to keep at the top of JSON hashes',
98106
)
99-
100107
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
101108
args = parser.parse_args(argv)
102109

@@ -113,10 +120,19 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
113120
)
114121

115122
if contents != pretty_contents:
116-
print('File {} is not pretty-formatted'.format(json_file))
123+
print(
124+
'File {} is not pretty-formatted'.format(json_file),
125+
file=sys.stderr,
126+
)
127+
sys.stderr.flush()
117128

118129
if args.autofix:
119130
_autofix(json_file, pretty_contents)
131+
else:
132+
print(
133+
get_diff(contents, pretty_contents, json_file),
134+
end='',
135+
)
120136

121137
status = 1
122138
except ValueError:

tests/pretty_format_json_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import shutil
23

34
import pytest
@@ -105,3 +106,36 @@ def test_top_sorted_get_pretty_format():
105106
def test_badfile_main():
106107
ret = main([get_resource_path('ok_yaml.yaml')])
107108
assert ret == 1
109+
110+
111+
def test_diffing_output(capsys):
112+
resource_path = get_resource_path('not_pretty_formatted_json.json')
113+
expected_retval = 1
114+
a = os.path.join('a', resource_path)
115+
b = os.path.join('b', resource_path)
116+
expected_out = '''\
117+
--- {}
118+
+++ {}
119+
@@ -1,6 +1,9 @@
120+
{{
121+
- "foo":
122+
- "bar",
123+
- "alist": [2, 34, 234],
124+
- "blah": null
125+
+ "alist": [
126+
+ 2,
127+
+ 34,
128+
+ 234
129+
+ ],
130+
+ "blah": null,
131+
+ "foo": "bar"
132+
}}
133+
'''.format(a, b)
134+
expected_err = 'File {} is not pretty-formatted\n'.format(resource_path)
135+
136+
actual_retval = main([resource_path])
137+
actual_out, actual_err = capsys.readouterr()
138+
139+
assert actual_retval == expected_retval
140+
assert actual_out == expected_out
141+
assert actual_err == expected_err

0 commit comments

Comments
 (0)