From 98adf204b2fbdbeb696b47c4bf6abecad80da1d0 Mon Sep 17 00:00:00 2001 From: Tom Viner Date: Tue, 21 Jun 2016 16:48:29 +0200 Subject: [PATCH] issue 1553: Include terminal newlines in diffs --- CHANGELOG.rst | 8 ++++++++ _pytest/assertion/util.py | 5 +++-- testing/test_assertion.py | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0a71de6b9ad..cb6bd625a71 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -23,15 +23,23 @@ only ran once. Now a failure is raised. Fixes (`#460`_). Thanks to `@nikratio`_ for bug report, `@RedBeardCode`_ and `@tomviner`_ for PR. +* Create correct diff for strings ending with newlines. Fixes (`#1553`_). + Thanks `@Vogtinator`_ for reporting. Thanks to `@RedBeardCode`_ and + `@tomviner`_ for PR. + +* + .. _#1580: https://github.com/pytest-dev/pytest/pull/1580 .. _#1605: https://github.com/pytest-dev/pytest/issues/1605 .. _#1597: https://github.com/pytest-dev/pytest/pull/1597 .. _#460: https://github.com/pytest-dev/pytest/pull/460 +.. _#1553: https://github.com/pytest-dev/pytest/issues/1553 .. _@graingert: https://github.com/graingert .. _@taschini: https://github.com/taschini .. _@nikratio: https://github.com/nikratio .. _@RedBeardCode: https://github.com/RedBeardCode +.. _@Vogtinator: https://github.com/Vogtinator 2.9.2 diff --git a/_pytest/assertion/util.py b/_pytest/assertion/util.py index f2f23efea27..8bf425caf48 100644 --- a/_pytest/assertion/util.py +++ b/_pytest/assertion/util.py @@ -225,9 +225,10 @@ def _diff_text(left, right, verbose=False): 'characters in diff, use -v to show') % i] left = left[:-i] right = right[:-i] + keepends = True explanation += [line.strip('\n') - for line in ndiff(left.splitlines(), - right.splitlines())] + for line in ndiff(left.splitlines(keepends), + right.splitlines(keepends))] return explanation diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 347278e1904..dfa1b942034 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -428,7 +428,7 @@ def test_long(): "*- 3", "*- 5", "*- 7", - "*truncated (191 more lines)*use*-vv*", + "*truncated (193 more lines)*use*-vv*", ]) @@ -626,3 +626,17 @@ def __hash__(self): + repr(3) """).strip() assert '\n'.join(expl) == dedent + +def test_diff_newline_at_end(monkeypatch, testdir): + testdir.makepyfile(r""" + def test_diff(): + assert 'asdf' == 'asdf\n' + """) + + result = testdir.runpytest() + result.stdout.fnmatch_lines(r""" + *assert 'asdf' == 'asdf\n' + * - asdf + * + asdf + * ? + + """)