1
1
import icdiff
2
- from unittest import mock
3
- import pytest
4
2
import re
5
3
import sys
6
4
from pprintpp import pformat
7
5
8
- YELLOW_ON = '\x1b [1;33m'
9
- COLOR_OFF = '\x1b [m'
10
- GREEN_ON = '\x1b [1;32m'
11
- ANSI_ESCAPE_RE = re .compile (r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]' )
12
-
6
+ YELLOW_ON = "\x1b [1;33m"
7
+ COLOR_OFF = "\x1b [m"
8
+ GREEN_ON = "\x1b [1;32m"
9
+ ANSI_ESCAPE_RE = re .compile (r"(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]" )
13
10
14
11
15
12
def test_short_dict (testdir ):
@@ -20,15 +17,15 @@ def test_short_dict(testdir):
20
17
two = {
21
18
1 : "the number one" ,
22
19
2 : "the number three" ,
23
- 6 : [1 , 2 , 3 ]
20
+ 6 : [1 , 2 , 3 ],
24
21
}
25
22
testdir .makepyfile (
26
23
f"""
27
24
def test_one():
28
25
assert { one !r} == { two !r}
29
26
"""
30
27
)
31
- output = testdir .runpytest (' -vv' ).stdout .str ()
28
+ output = testdir .runpytest (" -vv" ).stdout .str ()
32
29
print (repr (output ))
33
30
two_left = "'the number two'"
34
31
two_right = "'the number three'"
@@ -46,7 +43,7 @@ def test_short_dict_with_colorization(testdir):
46
43
two = {
47
44
1 : "the number one" ,
48
45
2 : "the number three" ,
49
- 6 : [1 , 2 , 3 ]
46
+ 6 : [1 , 2 , 3 ],
50
47
}
51
48
testdir .makepyfile (
52
49
f"""
@@ -55,8 +52,8 @@ def test_one():
55
52
"""
56
53
)
57
54
# Force colorization in py TerminalWriter
58
- testdir .monkeypatch .setenv (' PY_COLORS' , '1' )
59
- output = testdir .runpytest (' -vv' ).stdout .str ()
55
+ testdir .monkeypatch .setenv (" PY_COLORS" , "1" )
56
+ output = testdir .runpytest (" -vv" ).stdout .str ()
60
57
print (repr (output ))
61
58
two_left = f"'the number t{ YELLOW_ON } wo{ COLOR_OFF } '"
62
59
two_right = f"'the number t{ YELLOW_ON } hree{ COLOR_OFF } '"
@@ -68,55 +65,55 @@ def test_one():
68
65
69
66
def test_long_dict (testdir ):
70
67
one = {
71
- ' currency' : ' USD' ,
72
- ' default_UK_warehouse' : ' xforce' ,
73
- ' default_incoterm' : ' EXW' ,
74
- ' name' : ' John Doe' ,
75
- ' payment_term' : ' 30% deposit, 70% balance' ,
76
- ' reference' : ' 42551456-a1b3-49bd-beed-b168d9a5ac83' ,
77
- ' website' : ' http://megasofas.example.com' ,
78
- ' main_contact' : {
79
- ' city' : ' Madeira' ,
80
- ' country' : 'PT' ,
81
-
82
- ' fax' : ' 012356 789039' ,
83
- ' mobile' : ' 012356 789039' ,
84
- ' name' : ' Almeida & Filhos - Example, S.A.' ,
85
- ' phone' : ' 253444802010' ,
86
- ' postcode' : ' 4815-123' ,
87
- ' street' : "Senhora Test D'Ajuda, 432" ,
88
- ' street2' : ' Moreira de Conegos'
68
+ " currency" : " USD" ,
69
+ " default_UK_warehouse" : " xforce" ,
70
+ " default_incoterm" : " EXW" ,
71
+ " name" : " John Doe" ,
72
+ " payment_term" : " 30% deposit, 70% balance" ,
73
+ " reference" : " 42551456-a1b3-49bd-beed-b168d9a5ac83" ,
74
+ " website" : " http://megasofas.example.com" ,
75
+ " main_contact" : {
76
+ " city" : " Madeira" ,
77
+ " country" : "PT" ,
78
+
79
+ " fax" : " 012356 789039" ,
80
+ " mobile" : " 012356 789039" ,
81
+ " name" : " Almeida & Filhos - Example, S.A." ,
82
+ " phone" : " 253444802010" ,
83
+ " postcode" : " 4815-123" ,
84
+ " street" : "Senhora Test D'Ajuda, 432" ,
85
+ " street2" : " Moreira de Conegos" ,
89
86
},
90
87
}
91
88
two = {
92
- ' currency' : ' USD' ,
93
- ' default_UK_warehouse' : ' iforce' ,
94
- ' default_incoterm' : ' EXW' ,
95
- ' freight_forwarder' : ' flexport' ,
96
- ' name' : ' John Doe' ,
97
- ' payment_term' : ' 30% deposit, 70% balance' ,
98
- ' reference' : ' 42551456-a1b3-49bd-beed-b168d9a5ac83' ,
99
- ' website' : ' http://megasofas.example.com' ,
100
- ' main_contact' : {
101
- ' name' : ' Almeida & Filhos - Example, S.A.' ,
102
-
103
- ' street' : "Senhora Test D'Ajuda, 432" ,
104
- ' street2' : ' Moreira de Conegos' ,
105
- ' postcode' : ' 4815-123' ,
106
- ' city' : ' Madeira' ,
107
- ' country' : 'PT' ,
108
- ' phone' : ' 253444802010' ,
109
- ' fax' : ' 012356 789039' ,
110
- ' mobile' : ' 012356 789039'
111
- }
89
+ " currency" : " USD" ,
90
+ " default_UK_warehouse" : " iforce" ,
91
+ " default_incoterm" : " EXW" ,
92
+ " freight_forwarder" : " flexport" ,
93
+ " name" : " John Doe" ,
94
+ " payment_term" : " 30% deposit, 70% balance" ,
95
+ " reference" : " 42551456-a1b3-49bd-beed-b168d9a5ac83" ,
96
+ " website" : " http://megasofas.example.com" ,
97
+ " main_contact" : {
98
+ " name" : " Almeida & Filhos - Example, S.A." ,
99
+
100
+ " street" : "Senhora Test D'Ajuda, 432" ,
101
+ " street2" : " Moreira de Conegos" ,
102
+ " postcode" : " 4815-123" ,
103
+ " city" : " Madeira" ,
104
+ " country" : "PT" ,
105
+ " phone" : " 253444802010" ,
106
+ " fax" : " 012356 789039" ,
107
+ " mobile" : " 012356 789039" ,
108
+ },
112
109
}
113
110
testdir .makepyfile (
114
111
f"""
115
112
def test_two():
116
113
assert { one !r} == { two !r}
117
114
"""
118
115
)
119
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
116
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
120
117
expected_l = f"'default_UK_warehouse': '{ YELLOW_ON } x{ COLOR_OFF } force'"
121
118
expected_r = f"'default_UK_warehouse': '{ YELLOW_ON } i{ COLOR_OFF } force'"
122
119
expected_missing = f"{ GREEN_ON } 'freight_forwarder': 'flexport',{ COLOR_OFF } "
@@ -127,7 +124,7 @@ def test_two():
127
124
128
125
def test_only_works_for_equals (testdir ):
129
126
testdir .makepyfile (
130
- f """
127
+ """
131
128
def test_in():
132
129
assert 1 in [2, 3, 4]
133
130
@@ -146,29 +143,31 @@ def _assert_line_in_ignoring_whitespace(expected, block):
146
143
for line in block .splitlines ():
147
144
if all (part in line for part in parts ):
148
145
return True
149
- assert False , f' could not find { expected } in:\n { block } '
146
+ assert False , f" could not find { expected } in:\n { block } "
150
147
151
148
152
149
def test_prepends_icdiff_output_lines_with_color_off (testdir ):
153
- one = [' hello' , ' hello' ]
154
- two = [' bello' , ' hella' ]
150
+ one = [" hello" , " hello" ]
151
+ two = [" bello" , " hella" ]
155
152
testdir .makepyfile (
156
153
f"""
157
154
def test_thing():
158
155
assert { one !r} == { two !r}
159
156
"""
160
157
)
161
- output = testdir .runpytest ('--color=yes' ).stdout .str ()
162
- expected = list (icdiff .ConsoleDiff ().make_table (
163
- pformat (one , width = 1 ).splitlines (),
164
- pformat (two , width = 1 ).splitlines (),
165
- ))
166
- print ('\n ' .join (repr (l ) for l in output .splitlines ()))
158
+ output = testdir .runpytest ("--color=yes" ).stdout .str ()
159
+ expected = list (
160
+ icdiff .ConsoleDiff ().make_table (
161
+ pformat (one , width = 1 ).splitlines (),
162
+ pformat (two , width = 1 ).splitlines (),
163
+ )
164
+ )
165
+ print ("\n " .join (repr (l ) for l in output .splitlines ()))
167
166
_assert_line_in_ignoring_whitespace (expected [0 ], output )
168
167
169
168
170
169
def strip_color_codes (s ):
171
- return re .sub (r' \x1b\[[0-9;]*m' , '' , s )
170
+ return re .sub (r" \x1b\[[0-9;]*m" , "" , s )
172
171
173
172
174
173
def test_avoids_single_line_diffs (testdir ):
@@ -180,7 +179,7 @@ def test_one():
180
179
assert { one !r} == { two !r}
181
180
"""
182
181
)
183
- output = testdir .runpytest (' -vv' ).stdout .str ()
182
+ output = testdir .runpytest (" -vv" ).stdout .str ()
184
183
print (repr (output ))
185
184
assert "1: '1', 1: '1'," in strip_color_codes (output )
186
185
@@ -193,22 +192,24 @@ def test_a():
193
192
"""
194
193
)
195
194
output = testdir .runpytest ().stdout .str ()
196
- drilldown_expression = ' where 3 = len([1, 2, 3])'
195
+ drilldown_expression = " where 3 = len([1, 2, 3])"
197
196
assert drilldown_expression in output
198
197
199
198
200
199
def test_long_lines_in_comparators_are_wrapped_sensibly_multiline (testdir ):
201
- left = {1 : "hello " * 20 , 2 : ' two' }
202
- right = {1 : "hella " * 20 , 2 : ' two' }
200
+ left = {1 : "hello " * 20 , 2 : " two" }
201
+ right = {1 : "hella " * 20 , 2 : " two" }
203
202
testdir .makepyfile (
204
203
f"""
205
204
def test_one():
206
205
assert { left !r} == { right !r}
207
206
"""
208
207
)
209
- output = testdir .runpytest ('-vv' , '--color=yes' ).stdout .str ()
210
- comparison_line = next (l for l in output .splitlines () if '1:' in l and "assert" not in l )
211
- assert comparison_line .count ('hell' ) < 13
208
+ output = testdir .runpytest ("-vv" , "--color=yes" ).stdout .str ()
209
+ comparison_line = next (
210
+ l for l in output .splitlines () if "1:" in l and "assert" not in l
211
+ )
212
+ assert comparison_line .count ("hell" ) < 13
212
213
213
214
214
215
def test_long_lines_in_comparators_are_wrapped_sensibly_singleline (testdir ):
@@ -220,12 +221,11 @@ def test_one():
220
221
assert { left !r} == { right !r}
221
222
"""
222
223
)
223
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
224
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
224
225
comparison_line = next (
225
- l for l in output .splitlines ()
226
- if "hell" in l and "assert" not in l
226
+ l for l in output .splitlines () if "hell" in l and "assert" not in l
227
227
)
228
- assert comparison_line .count (' hell' ) < 15
228
+ assert comparison_line .count (" hell" ) < 15
229
229
230
230
231
231
def test_columns_are_calculated_outside_hook (testdir ):
@@ -246,51 +246,51 @@ def test_one():
246
246
assert { left !r} == { right !r}
247
247
"""
248
248
)
249
- testdir .monkeypatch .setenv (' COLUMNS' , '50' )
249
+ testdir .monkeypatch .setenv (" COLUMNS" , "50" )
250
250
# testdir._method = 'subprocess'
251
251
output = testdir .runpytest (
252
- '-vv' , '--color=yes' ,
252
+ "-vv" ,
253
+ "--color=yes" ,
253
254
).stdout .str ()
254
255
comparison_line = next (
255
- l for l in output .splitlines ()
256
- if 'hell' in l and "assert" not in l
256
+ l for l in output .splitlines () if "hell" in l and "assert" not in l
257
257
)
258
- assert comparison_line .count (' hell' ) > 5
258
+ assert comparison_line .count (" hell" ) > 5
259
259
260
260
261
261
def test_small_numbers_are_specialcased (testdir ):
262
262
testdir .makepyfile (
263
- f """
263
+ """
264
264
def test_one():
265
265
assert 404 == 400
266
266
"""
267
267
)
268
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
268
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
269
269
assert "assert 404 == 400" in output
270
270
assert "E assert 404 == 400" in output
271
271
272
272
273
273
def test_larger_numbers_are_sane (testdir ):
274
274
testdir .makepyfile (
275
- f """
275
+ """
276
276
def test_one():
277
277
assert 123456 == 1234567
278
278
"""
279
279
)
280
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
280
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
281
281
assert f"123456 123456{ GREEN_ON } 7" in output
282
282
283
283
284
284
def test_really_long_diffs_use_context_mode (testdir ):
285
285
testdir .makepyfile (
286
- f """
286
+ """
287
287
def test_one():
288
288
one = list(range(100))
289
289
two = list(range(20)) + ["X"] + list(range(20, 50)) + ["Y"] + list(range(53, 100))
290
290
assert one == two
291
291
"""
292
292
)
293
- output = testdir .runpytest (' -vv' , ' --color=yes' , ' -r=no' ).stdout .str ()
293
+ output = testdir .runpytest (" -vv" , " --color=yes" , " -r=no" ).stdout .str ()
294
294
assert len (output .splitlines ()) < 50
295
295
assert "---" in output # context split marker
296
296
0 commit comments