1
1
# pylint: disable=inconsistent-return-statements
2
+ import py
2
3
from pprintpp import pformat
3
4
import icdiff
4
5
6
+ COLS = py .io .TerminalWriter ().fullwidth # pylint: disable=no-member
7
+ MARGIN_L = 9
8
+ GUTTER = 2
9
+ MARGINS = MARGIN_L + GUTTER + 1
10
+
11
+
5
12
6
13
def pytest_assertrepr_compare (config , op , left , right ):
7
14
if op != '==' :
@@ -13,26 +20,23 @@ def pytest_assertrepr_compare(config, op, left, right):
13
20
except TypeError :
14
21
pass
15
22
16
- terminal_writer = config .get_terminal_writer ()
17
- cols = terminal_writer .fullwidth - 12
18
-
19
- wide_left = pformat (left , indent = 2 , width = cols / 2 ).splitlines ()
20
- wide_right = pformat (right , indent = 2 , width = cols / 2 ).splitlines ()
21
- if len (wide_left ) < 3 or len (wide_right ) < 3 :
22
- shortest_left = pformat (left , indent = 2 , width = 1 ).splitlines ()
23
- shortest_right = pformat (right , indent = 2 , width = 1 ).splitlines ()
24
- longest_line_length = max (len (l ) for l in shortest_left + shortest_right )
25
- else :
26
- longest_line_length = max (len (l ) for l in wide_left + wide_right )
27
- cols = int (min (longest_line_length , cols ))
23
+ half_cols = COLS / 2 - MARGINS
28
24
29
- pretty_left = pformat (left , indent = 2 , width = cols / 2 ).splitlines ()
30
- pretty_right = pformat (right , indent = 2 , width = cols / 2 ).splitlines ()
25
+ pretty_left = pformat (left , indent = 2 , width = half_cols ).splitlines ()
26
+ pretty_right = pformat (right , indent = 2 , width = half_cols ).splitlines ()
27
+ diff_cols = COLS - MARGINS
31
28
29
+ if len (pretty_left ) < 3 or len (pretty_right ) < 3 :
30
+ # avoid small diffs far apart by smooshing them up to the left
31
+ pretty_left = pformat (left , indent = 2 , width = 1 ).splitlines ()
32
+ pretty_right = pformat (right , indent = 2 , width = 1 ).splitlines ()
33
+ diff_cols = max (len (l ) + 1 for l in pretty_left + pretty_right ) * 2
34
+ if (diff_cols + MARGINS ) > COLS :
35
+ diff_cols = COLS - MARGINS
32
36
33
- differ = icdiff .ConsoleDiff (cols = cols + 12 , tabsize = 2 )
37
+ differ = icdiff .ConsoleDiff (cols = diff_cols , tabsize = 2 )
34
38
35
- if not terminal_writer .hasmarkup :
39
+ if not config . get_terminal_writer () .hasmarkup :
36
40
# colorization is disabled in Pytest - either due to the terminal not
37
41
# supporting it or the user disabling it. We should obey, but there is
38
42
# no option in icdiff to disable it, so we replace its colorization
@@ -44,4 +48,4 @@ def pytest_assertrepr_compare(config, op, left, right):
44
48
45
49
icdiff_lines = list (differ .make_table (pretty_left , pretty_right ))
46
50
47
- return ['equals failed' ] + [color_off + l for l in icdiff_lines ]
51
+ return [f 'equals failed' ] + [color_off + l for l in icdiff_lines ]
0 commit comments