Skip to content

Commit cbb1645

Browse files
CuriousLearnerorsenthil
authored andcommitted
bpo-24746: Avoid stripping trailing whitespace in doctest fancy diff (#10639)
1 parent a234e14 commit cbb1645

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

Lib/doctest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,6 @@ def output_difference(self, example, got, optionflags):
16901690
kind = 'ndiff with -expected +actual'
16911691
else:
16921692
assert 0, 'Bad diff option'
1693-
# Remove trailing whitespace on diff output.
1694-
diff = [line.rstrip() + '\n' for line in diff]
16951693
return 'Differences (%s):\n' % kind + _indent(''.join(diff))
16961694

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

Lib/test/test_doctest.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,11 @@ def test_unittest_reportflags():
24512451
Then the default eporting options are ignored:
24522452
24532453
>>> result = suite.run(unittest.TestResult())
2454+
"""
2455+
"""
2456+
*NOTE*: These doctest are intentionally not placed in raw string to depict
2457+
the trailing whitespace using `\x20` in the diff below.
2458+
24542459
>>> print(result.failures[0][1]) # doctest: +ELLIPSIS
24552460
Traceback ...
24562461
Failed example:
@@ -2464,7 +2469,7 @@ def test_unittest_reportflags():
24642469
Differences (ndiff with -expected +actual):
24652470
a
24662471
- <BLANKLINE>
2467-
+
2472+
+\x20
24682473
b
24692474
<BLANKLINE>
24702475
<BLANKLINE>
@@ -2953,6 +2958,47 @@ def test_CLI(): r"""
29532958
29542959
"""
29552960

2961+
def test_no_trailing_whitespace_stripping():
2962+
r"""
2963+
The fancy reports had a bug for a long time where any trailing whitespace on
2964+
the reported diff lines was stripped, making it impossible to see the
2965+
differences in line reported as different that differed only in the amount of
2966+
trailing whitespace. The whitespace still isn't particularly visible unless
2967+
you use NDIFF, but at least it is now there to be found.
2968+
2969+
*NOTE*: This snippet was intentionally put inside a raw string to get rid of
2970+
leading whitespace error in executing the example below
2971+
2972+
>>> def f(x):
2973+
... r'''
2974+
... >>> print('\n'.join(['a ', 'b']))
2975+
... a
2976+
... b
2977+
... '''
2978+
"""
2979+
"""
2980+
*NOTE*: These doctest are not placed in raw string to depict the trailing whitespace
2981+
using `\x20`
2982+
2983+
>>> test = doctest.DocTestFinder().find(f)[0]
2984+
>>> flags = doctest.REPORT_NDIFF
2985+
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
2986+
... # doctest: +ELLIPSIS
2987+
**********************************************************************
2988+
File ..., line 3, in f
2989+
Failed example:
2990+
print('\n'.join(['a ', 'b']))
2991+
Differences (ndiff with -expected +actual):
2992+
- a
2993+
+ a
2994+
b
2995+
TestResults(failed=1, attempted=1)
2996+
2997+
*NOTE*: `\x20` is for checking the trailing whitespace on the +a line above.
2998+
We cannot use actual spaces there, as a commit hook prevents from committing
2999+
patches that contain trailing whitespace. More info on Issue 24746.
3000+
"""
3001+
29563002
######################################################################
29573003
## Main
29583004
######################################################################
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)