From 3383d73e216628a77dd90194866774dc07d278b4 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:24:06 +0200 Subject: [PATCH] Fix tests when run with FORCE_COLOR=1 --- Lib/test/test_capi/test_misc.py | 19 ++++++++++++------- Lib/test/test_cmd_line_script.py | 9 ++++++++- Lib/test/test_compileall.py | 2 ++ Lib/test/test_eof.py | 4 +++- Lib/test/test_exceptions.py | 18 +++++++++++++----- Lib/test/test_import/__init__.py | 25 +++++++++++++++++++------ Lib/test/test_inspect/test_inspect.py | 12 +++++++++--- Lib/test/test_regrtest.py | 3 +++ Lib/test/test_repl.py | 4 ++++ Lib/test/test_runpy.py | 11 +++++++++-- Lib/test/test_tracemalloc.py | 1 + Lib/test/test_unicodedata.py | 13 ++++++++++--- 12 files changed, 93 insertions(+), 28 deletions(-) diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index ada30181aeeca94..d52e4c0c4026393 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -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 @@ -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): diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index f30107225ff612a..73c30774a9da41b 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -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) @@ -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, diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 3a34c6822bc0793..7b12e4f0e47daab 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -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 @@ -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) diff --git a/Lib/test/test_eof.py b/Lib/test/test_eof.py index e377383450e19d5..582e5b6de6e6870 100644 --- a/Lib/test/test_eof.py +++ b/Lib/test/test_eof.py @@ -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 @@ -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 = ("(, line 1)") with os_helper.temp_dir() as temp_dir: @@ -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: diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 206e22e791e02a6..0a1f873bcbf9f54 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -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 @@ -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 @@ -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 diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 83efbc1e25e77af..3c52cb6ca831ffb 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -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: @@ -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): diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 1ecf18bf49fa7eb..044873c8d24023a 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -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 @@ -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 diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index ab46ccbf004a3ac..713ffd74a059842 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -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 @@ -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 @@ -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. diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index e764e60560db234..32b3d5f2b429d4f 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -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, @@ -70,6 +71,7 @@ def run_on_interactive_mode(source): return output +@force_not_colorized_test_class class TestInteractiveInterpreter(unittest.TestCase): @cpython_only @@ -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): diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index b64383f6546f31e..ada78ec8e6b0c75 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -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 @@ -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 = ( diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index 5755f7697de91ae..ea0716dab61d082 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -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): diff --git a/Lib/test/test_unicodedata.py b/Lib/test/test_unicodedata.py index c7d09a6b460c194..5757a95157cd567 100644 --- a/Lib/test/test_unicodedata.py +++ b/Lib/test/test_unicodedata.py @@ -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): @@ -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