Skip to content

Commit 0ff23d4

Browse files
committed
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
1 parent b28837a commit 0ff23d4

File tree

2 files changed

+48
-83
lines changed

2 files changed

+48
-83
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def parse_topkeys(s): # type: (str) -> List[str]
5656
return s.split(',')
5757

5858

59-
def get_diff(source, target): # type: (List[str], List[str]) -> str
60-
source_lines = ''.join(source).split('\n')
61-
target_lines = ''.join(target).split('\n')
62-
d = difflib.Differ()
63-
diff = d.compare(source_lines, target_lines)
64-
return '\n'.join(diff)
59+
def get_diff(source, target): # type: (str, str) -> str
60+
source_lines = source.splitlines(True)
61+
target_lines = target.splitlines(True)
62+
diff = ''.join(difflib.ndiff(source_lines, target_lines))
63+
print(diff)
64+
return diff
6565

6666

6767
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
@@ -105,14 +105,6 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
105105
default=[],
106106
help='Ordered list of keys to keep at the top of JSON hashes',
107107
)
108-
parser.add_argument(
109-
'--show-expected',
110-
action='store_true',
111-
dest='show_expected',
112-
default=False,
113-
help='Show a diff between the input file and expected (pretty) output',
114-
)
115-
116108
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
117109
args = parser.parse_args(argv)
118110

@@ -131,11 +123,10 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
131123
if contents != pretty_contents:
132124
print('File {} is not pretty-formatted'.format(json_file))
133125

134-
if args.show_expected:
135-
print(get_diff(contents, list(pretty_contents)))
136-
137126
if args.autofix:
138127
_autofix(json_file, pretty_contents)
128+
else:
129+
print(get_diff(''.join(contents), pretty_contents))
139130

140131
status = 1
141132
except ValueError:

tests/pretty_format_json_test.py

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
from six import PY2
55

6-
from pre_commit_hooks.pretty_format_json import get_diff
76
from pre_commit_hooks.pretty_format_json import main
87
from pre_commit_hooks.pretty_format_json import parse_num_to_int
98
from testing.util import get_resource_path
@@ -108,76 +107,51 @@ def test_badfile_main():
108107
assert ret == 1
109108

110109

111-
def test_diffing_output():
112-
table_tests = [
113-
{
114-
'name': 'diff_test_1',
115-
'source': """
116-
{
117-
"key1": "val1",
118-
"key2": 2,
119-
"array_key": [1, 2, 3],
120-
"object":{
121-
"bool_key": true
122-
}
123-
}
124-
""",
125-
'target': """
126-
{
127-
"array_key": [
128-
1,
129-
2,
130-
3
131-
],
132-
"key1": "val1",
133-
"key2": 2,
134-
"object": {
135-
"bool_key": true
110+
def test_diffing_output(capsys):
111+
resource_path = get_resource_path('not_pretty_formatted_json.json')
112+
expected_retval = 1
113+
expected_diff = '''
114+
{
115+
- "foo":
116+
- "bar",
117+
- "alist": [2, 34, 234],
118+
+ "alist": [
119+
+ 2,
120+
+ 34,
121+
+ 234
122+
+ ],
123+
- "blah": null
124+
+ "blah": null,
125+
? +
126+
+ "foo": "bar"
136127
}
137-
}
138-
""",
139-
'expected': """
140-
{
141-
+ "array_key": [
142-
+ 1,
128+
129+
{
130+
- "foo":
131+
- "bar",
132+
- "alist": [2, 34, 234],
133+
+ "alist": [
143134
+ 2,
144-
+ 3
135+
+ 34,
136+
+ 234
145137
+ ],
146-
- "key1": "val1",
147-
? --
138+
- "blah": null
139+
+ "blah": null,
140+
? +
141+
+ "foo": "bar"
142+
}
148143
149-
+ "key1": "val1",
150-
- "key2": 2,
151-
? -- --
152144
153-
+ "key2": 2,
154-
- "array_key": [1, 2, 3],
155-
- "object":{
156-
? ------
145+
'''
146+
# output should include a line with the filepath, build it here
147+
file_output_line = 'File {} is not pretty-formatted'.format(resource_path)
148+
# prepend the above line to the diff
149+
expected_output = file_output_line + expected_diff
157150

158-
+ "object": {
159-
? +
151+
actual_retval = main([resource_path])
152+
actual_output = capsys.readouterr()
160153

161-
- "bool_key": true
162-
? ----
154+
assert actual_retval == expected_retval
163155

164-
+ "bool_key": true
165-
- }
166-
+ }
167-
}
168-
""",
169-
},
170-
{
171-
'name': 'diff_test_2',
172-
'source': '',
173-
'target': '',
174-
'expected': '',
175-
},
176-
177-
]
178-
for test in table_tests:
179-
s = list(test['source'])
180-
t = list(test['target'])
181-
expected = test['expected'].strip()
182-
actual = get_diff(s, t).strip()
183-
assert actual == expected
156+
actual_output = '\n'.join(actual_output)
157+
assert actual_output == expected_output

0 commit comments

Comments
 (0)