|
3 | 3 | import pytest |
4 | 4 | from six import PY2 |
5 | 5 |
|
6 | | -from pre_commit_hooks.pretty_format_json import get_diff |
7 | 6 | from pre_commit_hooks.pretty_format_json import main |
8 | 7 | from pre_commit_hooks.pretty_format_json import parse_num_to_int |
9 | 8 | from testing.util import get_resource_path |
@@ -108,76 +107,51 @@ def test_badfile_main(): |
108 | 107 | assert ret == 1 |
109 | 108 |
|
110 | 109 |
|
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" |
136 | 127 | } |
137 | | -} |
138 | | -""", |
139 | | - 'expected': """ |
140 | | -{ |
141 | | -+ "array_key": [ |
142 | | -+ 1, |
| 128 | +
|
| 129 | + { |
| 130 | +- "foo": |
| 131 | +- "bar", |
| 132 | +- "alist": [2, 34, 234], |
| 133 | ++ "alist": [ |
143 | 134 | + 2, |
144 | | -+ 3 |
| 135 | ++ 34, |
| 136 | ++ 234 |
145 | 137 | + ], |
146 | | -- "key1": "val1", |
147 | | -? -- |
| 138 | +- "blah": null |
| 139 | ++ "blah": null, |
| 140 | +? + |
| 141 | ++ "foo": "bar" |
| 142 | + } |
148 | 143 |
|
149 | | -+ "key1": "val1", |
150 | | -- "key2": 2, |
151 | | -? -- -- |
152 | 144 |
|
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 |
157 | 150 |
|
158 | | -+ "object": { |
159 | | -? + |
| 151 | + actual_retval = main([resource_path]) |
| 152 | + actual_output = capsys.readouterr() |
160 | 153 |
|
161 | | -- "bool_key": true |
162 | | -? ---- |
| 154 | + assert actual_retval == expected_retval |
163 | 155 |
|
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