Skip to content

Commit f6b6a58

Browse files
authored
Merge pull request #46 from hjwp/icdiff-cli-options
Icdiff cli options
2 parents 9fa30a6 + 57ff037 commit f6b6a58

File tree

2 files changed

+71
-14
lines changed

2 files changed

+71
-14
lines changed

pytest_icdiff.py

+62-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pprintpp import pformat
44
import icdiff
55

6-
COLS = shutil.get_terminal_size().columns
6+
AUTO_COLS = shutil.get_terminal_size().columns
77
MARGIN_L = 10
88
GUTTER = 2
99
MARGINS = MARGIN_L + GUTTER + 1
@@ -14,6 +14,50 @@
1414
# f.write('\n')
1515

1616

17+
def pytest_addoption(parser):
18+
parser.addoption(
19+
"--icdiff-cols",
20+
action="store",
21+
default=None,
22+
help="pytest-icdiff: specify the width of the screen, in case autodetection fails you",
23+
)
24+
parser.addoption(
25+
"--icdiff-show-all-spaces",
26+
default=False,
27+
action="store_true",
28+
help="pytest-icdiff: color all non-matching whitespace including that which is not needed for drawing the eye to changes. Slow, ugly, displays all changes",
29+
)
30+
parser.addoption(
31+
"--icdiff-highlight",
32+
default=False,
33+
action="store_true",
34+
help="pytest-icdiff: color by changing the background color instead of the foreground color. Very fast, ugly, displays all changes",
35+
)
36+
parser.addoption(
37+
"--icdiff-line-numbers",
38+
default=False,
39+
action="store_true",
40+
help="pytest-icdiff: generate output with line numbers. Not compatible with the 'exclude-lines' option.",
41+
)
42+
parser.addoption(
43+
"--icdiff-tabsize",
44+
default=2,
45+
help="pytest-icdiff: tab stop spacing",
46+
)
47+
parser.addoption(
48+
"--icdiff-truncate",
49+
default=False,
50+
action="store_true",
51+
help="pytest-icdiff: truncate long lines instead of wrapping them",
52+
)
53+
parser.addoption(
54+
"--icdiff-strip-trailing-cr",
55+
default=False,
56+
action="store_true",
57+
help="pytest-icdiff: strip any trailing carriage return at the end of an input line",
58+
)
59+
60+
1761
def pytest_assertrepr_compare(config, op, left, right):
1862
if op != "==":
1963
return
@@ -28,23 +72,33 @@ def pytest_assertrepr_compare(config, op, left, right):
2872
# Bail out of generating a diff and use pytest default output
2973
return
3074

75+
COLS = int(config.getoption("--icdiff-cols") or AUTO_COLS)
3176
half_cols = COLS / 2 - MARGINS
77+
TABSIZE = int(config.getoption("--icdiff-tabsize") or 2)
3278

33-
pretty_left = pformat(left, indent=2, width=half_cols).splitlines()
34-
pretty_right = pformat(right, indent=2, width=half_cols).splitlines()
79+
pretty_left = pformat(left, indent=TABSIZE, width=half_cols).splitlines()
80+
pretty_right = pformat(right, indent=TABSIZE, width=half_cols).splitlines()
3581
diff_cols = COLS - MARGINS
3682

3783
if len(pretty_left) < 3 or len(pretty_right) < 3:
3884
# avoid small diffs far apart by smooshing them up to the left
39-
smallest_left = pformat(left, indent=2, width=1).splitlines()
40-
smallest_right = pformat(right, indent=2, width=1).splitlines()
85+
smallest_left = pformat(left, indent=TABSIZE, width=1).splitlines()
86+
smallest_right = pformat(right, indent=TABSIZE, width=1).splitlines()
4187
max_side = max(len(l) + 1 for l in smallest_left + smallest_right)
4288
if (max_side * 2 + MARGINS) < COLS:
4389
diff_cols = max_side * 2 + GUTTER
44-
pretty_left = pformat(left, indent=2, width=max_side).splitlines()
45-
pretty_right = pformat(right, indent=2, width=max_side).splitlines()
90+
pretty_left = pformat(left, indent=TABSIZE, width=max_side).splitlines()
91+
pretty_right = pformat(right, indent=TABSIZE, width=max_side).splitlines()
4692

47-
differ = icdiff.ConsoleDiff(cols=diff_cols, tabsize=2)
93+
differ = icdiff.ConsoleDiff(
94+
cols=diff_cols,
95+
show_all_spaces=config.getoption("--icdiff-show-all-spaces"),
96+
highlight=config.getoption("--icdiff-highlight"),
97+
line_numbers=config.getoption("--icdiff-line-numbers"),
98+
tabsize=TABSIZE,
99+
truncate=config.getoption("--icdiff-truncate"),
100+
strip_trailing_cr=config.getoption("--icdiff-strip-trailing-cr"),
101+
)
48102

49103
if not config.get_terminal_writer().hasmarkup:
50104
# colorization is disabled in Pytest - either due to the terminal not

tests/test_pytest_icdiff.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
import icdiff
23
import re
34
import sys
@@ -295,23 +296,25 @@ def test_one():
295296
assert "---" in output # context split marker
296297

297298

298-
@pytest.mark.skipif('numpy' not in sys.modules, reason="requires numpy library")
299+
@pytest.mark.skipif("numpy" not in sys.modules, reason="requires numpy library")
299300
def test_np_arrays_can_use_equals(testdir) -> None:
300301
"""
301302
Numpy iterables will fall back to pytest default output
302303
"""
303-
testdir.makepyfile("""
304+
testdir.makepyfile(
305+
"""
304306
import numpy as np
305307
306308
def test():
307309
result = np.array([1, 2, 3])
308310
309311
assert all(result == 2)
310-
""")
312+
"""
313+
)
311314

312315
result = testdir.runpytest()
313316

314317
output = result.stdout.str()
315-
assert 'ValueError' not in output
316-
assert 'AssertionError' in output
317-
assert 'where False = all(equals failed' not in output, 'pytest-icdiff not used'
318+
assert "ValueError" not in output
319+
assert "AssertionError" in output
320+
assert "where False = all(equals failed" not in output, "pytest-icdiff not used"

0 commit comments

Comments
 (0)