Skip to content

Commit

Permalink
Merge pull request #46 from hjwp/icdiff-cli-options
Browse files Browse the repository at this point in the history
Icdiff cli options
  • Loading branch information
hjwp authored Nov 8, 2023
2 parents 9fa30a6 + 57ff037 commit f6b6a58
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
70 changes: 62 additions & 8 deletions pytest_icdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pprintpp import pformat
import icdiff

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


def pytest_addoption(parser):
parser.addoption(
"--icdiff-cols",
action="store",
default=None,
help="pytest-icdiff: specify the width of the screen, in case autodetection fails you",
)
parser.addoption(
"--icdiff-show-all-spaces",
default=False,
action="store_true",
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",
)
parser.addoption(
"--icdiff-highlight",
default=False,
action="store_true",
help="pytest-icdiff: color by changing the background color instead of the foreground color. Very fast, ugly, displays all changes",
)
parser.addoption(
"--icdiff-line-numbers",
default=False,
action="store_true",
help="pytest-icdiff: generate output with line numbers. Not compatible with the 'exclude-lines' option.",
)
parser.addoption(
"--icdiff-tabsize",
default=2,
help="pytest-icdiff: tab stop spacing",
)
parser.addoption(
"--icdiff-truncate",
default=False,
action="store_true",
help="pytest-icdiff: truncate long lines instead of wrapping them",
)
parser.addoption(
"--icdiff-strip-trailing-cr",
default=False,
action="store_true",
help="pytest-icdiff: strip any trailing carriage return at the end of an input line",
)


def pytest_assertrepr_compare(config, op, left, right):
if op != "==":
return
Expand All @@ -28,23 +72,33 @@ def pytest_assertrepr_compare(config, op, left, right):
# Bail out of generating a diff and use pytest default output
return

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

pretty_left = pformat(left, indent=2, width=half_cols).splitlines()
pretty_right = pformat(right, indent=2, width=half_cols).splitlines()
pretty_left = pformat(left, indent=TABSIZE, width=half_cols).splitlines()
pretty_right = pformat(right, indent=TABSIZE, width=half_cols).splitlines()
diff_cols = COLS - MARGINS

if len(pretty_left) < 3 or len(pretty_right) < 3:
# avoid small diffs far apart by smooshing them up to the left
smallest_left = pformat(left, indent=2, width=1).splitlines()
smallest_right = pformat(right, indent=2, width=1).splitlines()
smallest_left = pformat(left, indent=TABSIZE, width=1).splitlines()
smallest_right = pformat(right, indent=TABSIZE, width=1).splitlines()
max_side = max(len(l) + 1 for l in smallest_left + smallest_right)
if (max_side * 2 + MARGINS) < COLS:
diff_cols = max_side * 2 + GUTTER
pretty_left = pformat(left, indent=2, width=max_side).splitlines()
pretty_right = pformat(right, indent=2, width=max_side).splitlines()
pretty_left = pformat(left, indent=TABSIZE, width=max_side).splitlines()
pretty_right = pformat(right, indent=TABSIZE, width=max_side).splitlines()

differ = icdiff.ConsoleDiff(cols=diff_cols, tabsize=2)
differ = icdiff.ConsoleDiff(
cols=diff_cols,
show_all_spaces=config.getoption("--icdiff-show-all-spaces"),
highlight=config.getoption("--icdiff-highlight"),
line_numbers=config.getoption("--icdiff-line-numbers"),
tabsize=TABSIZE,
truncate=config.getoption("--icdiff-truncate"),
strip_trailing_cr=config.getoption("--icdiff-strip-trailing-cr"),
)

if not config.get_terminal_writer().hasmarkup:
# colorization is disabled in Pytest - either due to the terminal not
Expand Down
15 changes: 9 additions & 6 deletions tests/test_pytest_icdiff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import icdiff
import re
import sys
Expand Down Expand Up @@ -295,23 +296,25 @@ def test_one():
assert "---" in output # context split marker


@pytest.mark.skipif('numpy' not in sys.modules, reason="requires numpy library")
@pytest.mark.skipif("numpy" not in sys.modules, reason="requires numpy library")
def test_np_arrays_can_use_equals(testdir) -> None:
"""
Numpy iterables will fall back to pytest default output
"""
testdir.makepyfile("""
testdir.makepyfile(
"""
import numpy as np
def test():
result = np.array([1, 2, 3])
assert all(result == 2)
""")
"""
)

result = testdir.runpytest()

output = result.stdout.str()
assert 'ValueError' not in output
assert 'AssertionError' in output
assert 'where False = all(equals failed' not in output, 'pytest-icdiff not used'
assert "ValueError" not in output
assert "AssertionError" in output
assert "where False = all(equals failed" not in output, "pytest-icdiff not used"

0 comments on commit f6b6a58

Please sign in to comment.