Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/10704.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show whitespace escape codes (such as ``\t``, ``\r``, ``\v``, ``\f``, ``\u00a0``) in string comparison diffs when non-printable whitespace characters differ.
23 changes: 22 additions & 1 deletion src/_pytest/assertion/compare_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
from _pytest.compat import assert_never


_WHITESPACE_MAP = {
"\t": r"\t",
"\r": r"\r",
"\v": r"\v",
"\f": r"\f",
"\u00a0": r"\u00a0",
}


def _escape_whitespace(text: str) -> str:
"""Replace hidden whitespace characters with their literal escape representations."""
return "".join(_WHITESPACE_MAP.get(ch, ch) for ch in text)


def _compare_eq_text(
left: str,
right: str,
Expand Down Expand Up @@ -86,12 +100,19 @@ def _diff_text(
left = repr(str(left))
right = repr(str(right))
yield "Strings contain only whitespace, escaping them using repr()"

left_lines = _cap_ndiff_input(left, keepends, truncation_budget)
right_lines = _cap_ndiff_input(right, keepends, truncation_budget)

# Escape invisible whitespace characters (\t, \r, \v, \f, \u00a0) while
# preserving the original line-break structure for diff display.
# "right" is the expected base against which we compare "left",
# see https://github.com/pytest-dev/pytest/issues/3333
yield from highlighter(
"\n".join(line.strip("\n") for line in ndiff(right_lines, left_lines)),
"\n".join(
_escape_whitespace(line.strip("\n"))
for line in ndiff(right_lines, left_lines)
),
lexer="diff",
).splitlines()

Expand Down
51 changes: 51 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3155,3 +3155,54 @@ def test_order():
"test_order.py:*: AssertionError",
]
)


@pytest.mark.parametrize(
("actual", "expected", "escape"),
[
pytest.param("hello\tworld", "hello world", r"\t", id="tab"),
pytest.param("hello\tworld", "hello earth", r"\t", id="whitespace-and-text"),
pytest.param("hello\vworld", "hello world", r"\v", id="vertical-tab"),
pytest.param("hello\fworld", "hello world", r"\f", id="form-feed"),
pytest.param("hello\r\nworld", "hello\nworld", r"\r", id="carriage-return"),
pytest.param("hello\t", "hello ", r"\t", id="trailing-tab-vs-space"),
pytest.param("hello world", "hello\u00a0world", r"\u00a0", id="nbsp"),
],
)
def test_reprcompare_whitespace_inside_text(
actual: str, expected: str, escape: str
) -> None:
lines = callequal(actual, expected)
assert lines is not None

diff_lines = [line for line in lines if line.startswith(("+", "-"))]
assert diff_lines
assert any(escape in line for line in diff_lines)
assert not any(char in line for line in diff_lines for char in "\t\r\v\f")


def test_reprcompare_whitespace_inside_text_identical() -> None:
lines = callequal("hello\tworld", "hello\tworld")
assert lines is not None
assert not any(line.startswith(("+", "-")) for line in lines)


def test_whitespace_diff_pytester(pytester: Pytester) -> None:
"""Garante que códigos de escape para whitespace invisível sejam visíveis na saída do terminal (#10704)."""
p = pytester.makepyfile(
"""
def test_whitespace():
expected = "hello\tworld"
actual = "hello world"
assert actual == expected
"""
)
result = pytester.runpytest(str(p))
result.stdout.fnmatch_lines(
[
"*AssertionError: assert*",
"*- hello\\tworld*",
"*+ hello world*",
"*1 failed in*",
]
)
Loading