Skip to content

Commit

Permalink
Fix tests when run with FORCE_COLOR=1
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Dec 14, 2024
1 parent bf32693 commit 3383d73
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 28 deletions.
19 changes: 12 additions & 7 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import weakref
import operator
from test import support
from test.support import MISSING_C_DOCSTRINGS
from test.support import import_helper
from test.support import threading_helper
from test.support import warnings_helper
from test.support import requires_limited_api
from test.support import expected_failure_if_gil_disabled
from test.support import Py_GIL_DISABLED
from test.support import (
MISSING_C_DOCSTRINGS,
Py_GIL_DISABLED,
expected_failure_if_gil_disabled,
force_not_colorized_test_class,
import_helper,
requires_limited_api,
threading_helper,
warnings_helper,
)
from test.support.script_helper import assert_python_failure, assert_python_ok, run_python_until_end
try:
import _posixsubprocess
Expand Down Expand Up @@ -75,6 +78,8 @@ class InstanceMethod:
id = _testcapi.instancemethod(id)
testfunction = _testcapi.instancemethod(testfunction)


@force_not_colorized_test_class
class CAPITest(unittest.TestCase):

def test_instancemethod(self):
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

import textwrap
from test import support
from test.support import import_helper, is_apple, os_helper
from test.support import (
force_not_colorized_test_class,
import_helper,
is_apple,
os_helper,
)
from test.support.script_helper import (
make_pkg, make_script, make_zip_pkg, make_zip_script,
assert_python_ok, assert_python_failure, spawn_python, kill_python)
Expand Down Expand Up @@ -88,6 +93,8 @@ def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
importlib.invalidate_caches()
return to_return


@force_not_colorized_test_class
class CmdLineTest(unittest.TestCase):
def _check_output(self, script_name, exit_code, data,
expected_file, expected_argv0,
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_have_multiprocessing = False

from test import support
from test.support import force_not_colorized
from test.support import os_helper
from test.support import script_helper
from test.test_py_compile import without_source_date_epoch
Expand Down Expand Up @@ -766,6 +767,7 @@ def test_d_compile_error(self):
rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
self.assertRegex(out, b'File "dinsdale')

@force_not_colorized
def test_d_runtime_error(self):
bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_eof.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys
from codecs import BOM_UTF8
from test import support
from test.support import force_not_colorized
from test.support import os_helper
from test.support import script_helper
from test.support import warnings_helper
Expand Down Expand Up @@ -44,6 +44,7 @@ def test_EOFS(self):
self.assertEqual(cm.exception.text, "ä = '''thîs is ")
self.assertEqual(cm.exception.offset, 5)

@force_not_colorized
def test_EOFS_with_file(self):
expect = ("(<string>, line 1)")
with os_helper.temp_dir() as temp_dir:
Expand Down Expand Up @@ -123,6 +124,7 @@ def test_line_continuation_EOF(self):
self.assertEqual(str(cm.exception), expect)

@unittest.skipIf(not sys.executable, "sys.executable required")
@force_not_colorized
def test_line_continuation_EOF_from_file_bpo2180(self):
"""Ensure tok_nextc() does not add too many ending newlines."""
with os_helper.temp_dir() as temp_dir:
Expand Down
18 changes: 13 additions & 5 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
from itertools import product
from textwrap import dedent

from test.support import (captured_stderr, check_impl_detail,
cpython_only, gc_collect,
no_tracing, script_helper,
SuppressCrashReport,
force_not_colorized)
from test.support import (
SuppressCrashReport,
captured_stderr,
check_impl_detail,
cpython_only,
force_not_colorized,
force_not_colorized_test_class,
gc_collect,
no_tracing,
script_helper,
)
from test.support.import_helper import import_module
from test.support.os_helper import TESTFN, unlink
from test.support.warnings_helper import check_warnings
Expand Down Expand Up @@ -1465,6 +1471,7 @@ def gen():

@cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
@force_not_colorized
def test_recursion_normalizing_infinite_exception(self):
# Issue #30697. Test that a RecursionError is raised when
# maximum recursion depth has been exceeded when creating
Expand Down Expand Up @@ -2180,6 +2187,7 @@ def test_multiline_not_highlighted(self):
self.assertEqual(result[-len(expected):], expected)


@force_not_colorized_test_class
class SyntaxErrorTests(unittest.TestCase):
maxDiff = None

Expand Down
25 changes: 19 additions & 6 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,30 @@
from unittest import mock
import _imp

from test.support import os_helper
from test.support import (
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_apple_mobile, is_emscripten,
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS,
requires_gil_enabled, Py_GIL_DISABLED, no_rerun)
STDLIB_DIR,
Py_GIL_DISABLED,
Py_TRACE_REFS,
cpython_only,
force_not_colorized_test_class,
is_apple_mobile,
is_emscripten,
is_wasi,
no_rerun,
os_helper,
requires_gil_enabled,
run_in_subinterp,
run_in_subinterp_with_config,
script_helper,
swap_attr,
swap_item,
threading_helper,
)
from test.support.import_helper import (
forget, make_legacy_pyc, unlink, unload, ready_to_import,
DirsOnSysPath, CleanImport, import_module)
from test.support.os_helper import (
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
from test.support import script_helper
from test.support import threading_helper
from test.test_importlib.util import uncache
from types import ModuleType
try:
Expand Down Expand Up @@ -333,6 +345,7 @@ def _from_subinterp(cls, name, interpid, pipe, script_kwargs):
return cls.parse(text.decode())


@force_not_colorized_test_class
class ImportTests(unittest.TestCase):

def setUp(self):
Expand Down
12 changes: 9 additions & 3 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@
except ImportError:
ThreadPoolExecutor = None

from test.support import cpython_only, import_helper
from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ
from test.support import (
ALWAYS_EQ,
MISSING_C_DOCSTRINGS,
cpython_only,
force_not_colorized,
has_subprocess_support,
import_helper,
)
from test.support.import_helper import DirsOnSysPath, ready_to_import
from test.support.os_helper import TESTFN, temp_cwd
from test.support.script_helper import assert_python_ok, assert_python_failure, kill_python
from test.support import has_subprocess_support
from test import support

from test.test_inspect import inspect_fodder as mod
Expand Down Expand Up @@ -890,6 +895,7 @@ def test_getsource_stdlib_decimal(self):
self.assertEqual(src.splitlines(True), lines)

class TestGetsourceInteractive(unittest.TestCase):
@force_not_colorized
def test_getclasses_interactive(self):
# bpo-44648: simulate a REPL session;
# there is no `__file__` in the __main__ module
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from xml.etree import ElementTree

from test import support
from test.support import force_not_colorized_test_class
from test.support import import_helper
from test.support import os_helper
from test.libregrtest import cmdline
Expand Down Expand Up @@ -792,6 +793,7 @@ def test_finds_expected_number_of_tests(self):
f'{", ".join(output.splitlines())}')


@force_not_colorized_test_class
class ProgramsTestCase(BaseTestCase):
"""
Test various ways to run the Python test suite. Use options close
Expand Down Expand Up @@ -905,6 +907,7 @@ def test_pcbuild_rt(self):
self.run_batch(script, *rt_args, *self.regrtest_args, *self.tests)


@force_not_colorized_test_class
class ArgsTestCase(BaseTestCase):
"""
Test arguments of the Python test suite.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from test import support
from test.support import (
cpython_only,
force_not_colorized_test_class,
has_subprocess_support,
os_helper,
SuppressCrashReport,
Expand Down Expand Up @@ -70,6 +71,7 @@ def run_on_interactive_mode(source):
return output


@force_not_colorized_test_class
class TestInteractiveInterpreter(unittest.TestCase):

@cpython_only
Expand Down Expand Up @@ -273,6 +275,8 @@ def test_asyncio_repl_is_ok(self):

self.assertEqual(exit_code, 0, "".join(output))


@force_not_colorized_test_class
class TestInteractiveModeSyntaxErrors(unittest.TestCase):

def test_interactive_syntax_error_correct_line(self):
Expand Down
11 changes: 9 additions & 2 deletions Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
import textwrap
import unittest
import warnings
from test.support import (infinite_recursion, no_tracing, verbose,
requires_subprocess, requires_resource)
from test.support import (
force_not_colorized_test_class,
infinite_recursion,
no_tracing,
requires_resource,
requires_subprocess,
verbose,
)
from test.support.import_helper import forget, make_legacy_pyc, unload
from test.support.os_helper import create_empty_file, temp_dir, FakePath
from test.support.script_helper import make_script, make_zip_script
Expand Down Expand Up @@ -758,6 +764,7 @@ def test_encoding(self):
self.assertEqual(result['s'], "non-ASCII: h\xe9")


@force_not_colorized_test_class
class TestExit(unittest.TestCase):
STATUS_CONTROL_C_EXIT = 0xC000013A
EXPECTED_CODE = (
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_tracemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ def check_sys_xoptions_invalid(self, nframe):
return
self.fail(f"unexpected output: {stderr!a}")

@force_not_colorized
def test_sys_xoptions_invalid(self):
for nframe in INVALID_NFRAME:
with self.subTest(nframe=nframe):
Expand Down
13 changes: 10 additions & 3 deletions Lib/test/test_unicodedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
"""

import hashlib
from http.client import HTTPException
import sys
import unicodedata
import unittest
from test.support import (open_urlresource, requires_resource, script_helper,
cpython_only, check_disallow_instantiation)
from http.client import HTTPException
from test.support import (
check_disallow_instantiation,
cpython_only,
force_not_colorized,
open_urlresource,
requires_resource,
script_helper,
)


class UnicodeMethodsTest(unittest.TestCase):
Expand Down Expand Up @@ -277,6 +283,7 @@ def test_disallow_instantiation(self):
# Ensure that the type disallows instantiation (bpo-43916)
check_disallow_instantiation(self, unicodedata.UCD)

@force_not_colorized
def test_failed_import_during_compiling(self):
# Issue 4367
# Decoding \N escapes requires the unicodedata module. If it can't be
Expand Down

0 comments on commit 3383d73

Please sign in to comment.