There was an error while loading. Please reload this page.
1 parent 780f202 commit b28837aCopy full SHA for b28837a
2 files changed
pre_commit_hooks/pretty_format_json.py
@@ -1,10 +1,10 @@
1
from __future__ import print_function
2
3
import argparse
4
+import difflib
5
import io
6
import json
7
import sys
-import difflib
8
from collections import OrderedDict
9
from typing import List
10
from typing import Mapping
@@ -56,7 +56,7 @@ def parse_topkeys(s): # type: (str) -> List[str]
56
return s.split(',')
57
58
59
-def get_diff(source, target):
+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()
@@ -132,7 +132,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
132
print('File {} is not pretty-formatted'.format(json_file))
133
134
if args.show_expected:
135
- print(get_diff(contents, pretty_contents))
+ print(get_diff(contents, list(pretty_contents)))
136
137
if args.autofix:
138
_autofix(json_file, pretty_contents)
tests/pretty_format_json_test.py
@@ -3,6 +3,7 @@
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():
105
106
def test_badfile_main():
107
ret = main([get_resource_path('ok_yaml.yaml')])
108
assert ret == 1
109
+
110
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
+ ],
+ "object": {
139
+ 'expected': """
140
141
++ "array_key": [
142
++ 1,
143
++ 2,
144
++ 3
145
++ ],
146
+- "key1": "val1",
147
+? --
148
149
++ "key1": "val1",
150
+- "key2": 2,
151
+? -- --
152
153
++ "key2": 2,
154
+- "array_key": [1, 2, 3],
155
+- "object":{
156
+? ------
157
158
++ "object": {
159
+? +
160
161
+- "bool_key": true
162
+? ----
163
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
0 commit comments