8000 gh-117225: Add color to doctest output by hugovk · Pull Request #117583 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117225: 8000 Add color to doctest output #117583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2c1108b
Add colour to doctest output and create _colorize module
hugovk Apr 6, 2024
d27c0a8
Use _colorize in traceback module
hugovk Apr 6, 2024
bb591b6
Fix whitespace
hugovk Apr 6, 2024
42079be
Use f-strings
hugovk Apr 6, 2024
0088579
Remove underscores from members of an underscored module
hugovk Apr 6, 2024
d3034fa
Add blurb
hugovk Apr 6, 2024
39780cb
Remove underscores from members of an underscored module
hugovk Apr 6, 2024
c5aec15
Revert "Fix whitespace"
hugovk Apr 6, 2024
7e40133
Move _colorize to stdlib block, colour->color
hugovk Apr 6, 2024
e484465
Move imports together
hugovk Apr 6, 2024
1c7b025
Move imports together
hugovk Apr 6, 2024
ab2c94c
Move imports together
hugovk Apr 6, 2024
1aaeab8
Revert notests -> no_tests
hugovk Apr 6, 2024
cd02e4a
Revert "Use f-strings"
hugovk Apr 6, 2024
06543ff
Fix local tests
hugovk Apr 6, 2024
31c6647
Use red divider for failed test
hugovk Apr 7, 2024
9be3d81
Fix local tests
hugovk Apr 7, 2024
e4ff3e3
Less red
hugovk Apr 7, 2024
b62500a
Revert unnecessary changes
hugovk Apr 7, 2024
eb4f8dc
Move colour tests to test__colorize.py
hugovk Apr 7, 2024
976bfb4
Refactor asserts
hugovk Apr 7, 2024
ad7a946
Add missing captured_output to test.support's __all__ to fix IDE warning
hugovk Apr 7, 2024
8000 796e9f2
Only move test_colorized_detection_checks_for_environment_variables f…
hugovk Apr 7, 2024
99d4d0c
Apply suggestions from code review
hugovk Apr 7, 2024
95b9831
Use unittest's enterContext
hugovk Apr 7, 2024
d5417b4
Merge remote-tracking branch 'upstream/main' into doctest-tidy-output…
hugovk Apr 16, 2024
ece3ce0
Keep colorize functionality in traceback module for now
hugovk Apr 17, 2024
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
Prev Previous commit
Next Next commit
Fix local tests
  • Loading branch information
hugovk committed Apr 7, 2024
commit 9be3d810130ab9fa2cb21a1175bad6acac140d4d
43 changes: 42 additions & 1 deletion Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ def basics(): r"""
DocTestRunner is used to run DocTest test cases, and to accumulate
statistics. Here's a simple DocTest case we can use:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> def f(x):
... '''
... >>> x = 12
Expand Down Expand Up @@ -943,6 +946,8 @@ def basics(): r"""
6
ok
TestResults(failed=1, attempted=3)

>>> _colorize.COLORIZE = save_colorize
"""
def verbose_flag(): r"""
The `verbose` flag makes the test runner generate more detailed
Expand Down Expand Up @@ -1018,6 +1023,9 @@ def exceptions(): r"""
lines between the first line and the type/value may be omitted or
replaced with any other string:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> def f(x):
... '''
... >>> x = 12
Expand Down Expand Up @@ -1248,6 +1256,8 @@ def exceptions(): r"""
...
ZeroDivisionError: integer division or modulo by zero
TestResults(failed=1, attempted=1)

>>> _colorize.COLORIZE = save_colorize
"""
def displayhook(): r"""
Test that changing sys.displayhook doesn't matter for doctest.
Expand Down Expand Up @@ -1289,6 +1299,9 @@ def optionflags(): r"""
The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
and 1/0:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> def f(x):
... '>>> True\n1\n'

Expand Down Expand Up @@ -1708,6 +1721,7 @@ def optionflags(): r"""

Clean up.
>>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
>>> _colorize.COLORIZE = save_colorize

"""

Expand All @@ -1718,6 +1732,9 @@ def option_directives(): r"""
single example. To turn an option on for an example, follow that
example with a comment of the form ``# doctest: +OPTION``:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> def f(x): r'''
... >>> print(list(range(10))) # should fail: no ellipsis
... [0, 1, ..., 9]
Expand Down Expand Up @@ -1925,6 +1942,8 @@ def option_directives(): r"""
>>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
Traceback (most recent call last):
ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'

>>> _colorize.COLORIZE = save_colorize
"""

def test_testsource(): r"""
Expand Down Expand Up @@ -2008,6 +2027,9 @@ def test_pdb_set_trace():
with a version that restores stdout. This is necessary for you to
see debugger output.

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> doc = '''
... >>> x = 42
... >>> raise Exception('clé')
Expand Down Expand Up @@ -2062,7 +2084,7 @@ def test_pdb_set_trace():
... finally:
... sys.stdin = real_stdin
--Return--
> <doctest test.test_doctest.test_doctest.test_pdb_set_trace[7]>(3)calls_set_trace()->None
> <doctest test.test_doctest.test_doctest.test_pdb_set_trace[9]>(3)calls_set_trace()->None
-> import pdb; pdb.set_trace()
(Pdb) print(y)
2
Expand Down Expand Up @@ -2130,6 +2152,8 @@ def test_pdb_set_trace():
Got:
9
TestResults(failed=1, attempted=3)

>>> _colorize.COLORIZE = save_colorize
"""

def test_pdb_set_trace_nested():
Expand Down Expand Up @@ -2639,6 +2663,7 @@ def test_testfile(): r"""

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> save_argv = sys.argv
>>> if '-v' in sys.argv:
... sys.argv = [arg for arg in save_argv if arg != '-v']
Expand Down Expand Up @@ -2943,6 +2968,9 @@ def test_testmod(): r"""
def test_unicode(): """
Check doctest with a non-ascii filename:

>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> doc = '''
... >>> raise Exception('clé')
... '''
Expand All @@ -2968,8 +2996,11 @@ def test_unicode(): """
raise Exception('clé')
Exception: clé
TestResults(failed=1, attempted=1)

>>> _colorize.COLORIZE = save_colorize
"""


def test_CLI(): r"""
The doctest module can be used to run doctests against an arbitrary file.
These tests test this CLI functionality.
Expand Down Expand Up @@ -3260,6 +3291,9 @@ def test_run_doctestsuite_multiple_times():

def test_exception_with_note(note):
"""
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> test_exception_with_note('Note')
Traceback (most recent call last):
...
Expand Down Expand Up @@ -3309,6 +3343,8 @@ def test_exception_with_note(note):
ValueError: message
note
TestResults(failed=1, attempted=...)

>>> _colorize.COLORIZE = save_colorize
"""
exc = ValueError('Text')
exc.add_note(note)
Expand Down Expand Up @@ -3389,6 +3425,9 @@ def test_syntax_error_subclass_from_stdlib():

def test_syntax_error_with_incorrect_expected_note():
"""
>>> save_colorize = _colorize.COLORIZE
>>> _colorize.COLORIZE = False

>>> def f(x):
... r'''
... >>> exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
Expand Down Expand Up @@ -3417,6 +3456,8 @@ def test_syntax_error_with_incorrect_expected_note():
note1
note2
TestResults(failed=1, attempted=...)

>>> _colorize.COLORIZE = save_colorize
"""


Expand Down
0