Skip to content

Commit 02e33d9

Browse files
CuriousLearnerorsenthil
authored andcommitted
[2.7] bpo-24746: Avoid stripping trailing whitespace in doctest fancy diff (#11482)
* bpo-24746: Avoid stripping trailing whitespace in doctest fancy diff * [2.7] bpo-24746: Avoid stripping trailing whitespace in doctest fancy diff (GH-10639). (cherry picked from commit cbb1645) Co-authored-by: Sanyam Khurana <[email protected]>
1 parent 112e4af commit 02e33d9

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

Lib/doctest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,8 +1651,6 @@ def output_difference(self, example, got, optionflags):
16511651
kind = 'ndiff with -expected +actual'
16521652
else:
16531653
assert 0, 'Bad diff option'
1654-
# Remove trailing whitespace on diff output.
1655-
diff = [line.rstrip() + '\n' for line in diff]
16561654
return 'Differences (%s):\n' % kind + _indent(''.join(diff))
16571655

16581656
# If we're not using diff, then simply list the expected

Lib/test/test_doctest.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,12 @@ def test_unittest_reportflags():
23552355
Then the default eporting options are ignored:
23562356
23572357
>>> result = suite.run(unittest.TestResult())
2358-
>>> print result.failures[0][1] # doctest: +ELLIPSIS
2358+
"""
2359+
"""
2360+
*NOTE*: These doctest are intentionally not placed in raw string to depict
2361+
the trailing whitespace using `\x20` in the diff below.
2362+
2363+
>>> print(result.failures[0][1]) # doctest: +ELLIPSIS
23592364
Traceback ...
23602365
Failed example:
23612366
favorite_color
@@ -2368,7 +2373,7 @@ def test_unittest_reportflags():
23682373
Differences (ndiff with -expected +actual):
23692374
a
23702375
- <BLANKLINE>
2371-
+
2376+
+\x20
23722377
b
23732378
<BLANKLINE>
23742379
<BLANKLINE>
@@ -2717,6 +2722,47 @@ def old_test4(): """
27172722
TestResults(failed=0, attempted=4)
27182723
"""
27192724

2725+
def test_no_trailing_whitespace_stripping():
2726+
r"""
2727+
The fancy reports had a bug for a long time where any trailing whitespace on
2728+
the reported diff lines was stripped, making it impossible to see the
2729+
differences in line reported as different that differed only in the amount of
2730+
trailing whitespace. The whitespace still isn't particularly visible unless
2731+
you use NDIFF, but at least it is now there to be found.
2732+
2733+
*NOTE*: This snippet was intentionally put inside a raw string to get rid of
2734+
leading whitespace error in executing the example below
2735+
2736+
>>> def f(x):
2737+
... r'''
2738+
... >>> print('\n'.join(['a ', 'b']))
2739+
... a
2740+
... b
2741+
... '''
2742+
"""
2743+
"""
2744+
*NOTE*: These doctest are not placed in raw string to depict the trailing whitespace
2745+
using `\x20`
2746+
2747+
>>> test = doctest.DocTestFinder().find(f)[0]
2748+
>>> flags = doctest.REPORT_NDIFF
2749+
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
2750+
... # doctest: +ELLIPSIS
2751+
**********************************************************************
2752+
File ..., line 3, in f
2753+
Failed example:
2754+
print('\n'.join(['a ', 'b']))
2755+
Differences (ndiff with -expected +actual):
2756+
- a
2757+
+ a
2758+
b
2759+
TestResults(failed=1, attempted=1)
2760+
2761+
*NOTE*: `\x20` is for checking the trailing whitespace on the +a line above.
2762+
We cannot use actual spaces there, as a commit hook prevents from committing
2763+
patches that contain trailing whitespace. More info on Issue 24746.
2764+
"""
2765+
27202766
######################################################################
27212767
## Main
27222768
######################################################################
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid stripping trailing whitespace in doctest fancy diff. Orignial patch by
2+
R. David Murray & Jairo Trad. Enhanced by Sanyam Khurana.

0 commit comments

Comments
 (0)