File tree Expand file tree Collapse file tree 2 files changed +79
-3
lines changed
Expand file tree Collapse file tree 2 files changed +79
-3
lines changed Original file line number Diff line number Diff line change 11from __future__ import print_function
22
33import argparse
4+ import difflib
45import io
56import json
67import sys
7- import difflib
88from collections import OrderedDict
99from typing import List
1010from typing import Mapping
@@ -56,7 +56,7 @@ def parse_topkeys(s): # type: (str) -> List[str]
5656 return s .split (',' )
5757
5858
59- def get_diff (source , target ):
59+ def get_diff (source , target ): # type: (List[str], List[str]) -> str
6060 source_lines = '' .join (source ).split ('\n ' )
6161 target_lines = '' .join (target ).split ('\n ' )
6262 d = difflib .Differ ()
@@ -132,7 +132,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
132132 print ('File {} is not pretty-formatted' .format (json_file ))
133133
134134 if args .show_expected :
135- print (get_diff (contents , pretty_contents ))
135+ print (get_diff (contents , list ( pretty_contents ) ))
136136
137137 if args .autofix :
138138 _autofix (json_file , pretty_contents )
Original file line number Diff line number Diff line change 33import pytest
44from six import PY2
55
6+ from pre_commit_hooks .pretty_format_json import get_diff
67from pre_commit_hooks .pretty_format_json import main
78from pre_commit_hooks .pretty_format_json import parse_num_to_int
89from testing .util import get_resource_path
@@ -105,3 +106,78 @@ def test_top_sorted_get_pretty_format():
105106def test_badfile_main ():
106107 ret = main ([get_resource_path ('ok_yaml.yaml' )])
107108 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+ ],
132+ "key1": "val1",
133+ "key2": 2,
134+ "object": {
135+ "bool_key": true
136+ }
137+ }
138+ """ ,
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
You can’t perform that action at this time.
0 commit comments