Skip to content

Commit b13a046

Browse files
committed
issue 1553: Include terminal newlines in diffs
1 parent 5d8d1db commit b13a046

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
is specified on the command line together with the ``--pyargs``
1818
option. Thanks to `@taschini`_ for the PR (`#1597`_).
1919

20+
* Create correct diff for strings ending with newlines. Fixes (`#1553`_).
21+
Thanks `@Vogtinator`_ for reporting. Thanks to `@RedBeardCode`_ and
22+
`@tomviner`_ for PR.
23+
2024
*
2125

2226
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
2327
.. _#1605: https://github.com/pytest-dev/pytest/issues/1605
2428
.. _#1597: https://github.com/pytest-dev/pytest/pull/1597
29+
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553
2530

2631
.. _@graingert: https://github.com/graingert
2732
.. _@taschini: https://github.com/taschini
33+
.. _@Vogtinator: https://github.com/Vogtinator
2834

2935

3036
2.9.2

_pytest/assertion/util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ def _diff_text(left, right, verbose=False):
225225
'characters in diff, use -v to show') % i]
226226
left = left[:-i]
227227
right = right[:-i]
228+
# We set splitlines' keepends=True, which can only be passed as a
229+
# positional argument
228230
explanation += [line.strip('\n')
229-
for line in ndiff(left.splitlines(),
230-
right.splitlines())]
231+
for line in ndiff(left.splitlines(True),
232+
right.splitlines(True))]
231233
return explanation
232234

233235

testing/test_assertion.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def test_long():
428428
"*- 3",
429429
"*- 5",
430430
"*- 7",
431-
"*truncated (191 more lines)*use*-vv*",
431+
"*truncated (193 more lines)*use*-vv*",
432432
])
433433

434434

@@ -626,3 +626,17 @@ def __hash__(self):
626626
+ repr(3)
627627
""").strip()
628628
assert '\n'.join(expl) == dedent
629+
630+
def test_diff_newline_at_end(monkeypatch, testdir):
631+
testdir.makepyfile(r"""
632+
def test_diff():
633+
assert 'asdf' == 'asdf\n'
634+
""")
635+
636+
result = testdir.runpytest()
637+
result.stdout.fnmatch_lines(r"""
638+
*assert 'asdf' == 'asdf\n'
639+
* - asdf
640+
* + asdf
641+
* ? +
642+
""")

0 commit comments

Comments
 (0)