Skip to content

Commit 9237d8d

Browse files
committed
gh-140082: Forward colorizing from libregrtest to unittest
libregrtest redirects test output to a file as part of its operation. When `unittest` checks to see if it should colorize with `isatty(sys.stdout)` that fails resulting in no colorizing of the unittest output. Update `libregrtest` to set `FORCE_COLOR=1` when redirecting test output so that unittest will do color printing.
1 parent 8e0bf4f commit 9237d8d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Lib/test/libregrtest/single.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import gc
33
import importlib
44
import io
5+
import os
56
import sys
67
import time
78
import traceback
89
import unittest
910

10-
from _colorize import get_colors # type: ignore[import-not-found]
11+
from _colorize import can_colorize, get_colors # type: ignore[import-not-found]
1112
from test import support
1213
from test.support import threading_helper
1314

@@ -274,6 +275,9 @@ def _runtest(result: TestResult, runtests: RunTests) -> None:
274275
output_on_failure = runtests.output_on_failure
275276
timeout = runtests.timeout
276277

278+
if can_colorize(file=sys.stdout):
279+
os.environ['FORCE_COLOR'] = "1"
280+
277281
if timeout is not None and threading_helper.can_start_thread:
278282
use_timeout = True
279283
faulthandler.dump_traceback_later(timeout, exit=True)

Lib/test/libregrtest/worker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _colorize
12
import subprocess
23
import sys
34
import os
@@ -32,6 +33,12 @@ def create_worker_process(runtests: WorkerRunTests, output_fd: int,
3233
env['TEMP'] = tmp_dir
3334
env['TMP'] = tmp_dir
3435

36+
# The subcommand is run with a temporary output which means it is not a tty
37+
# and won't auto-color. The test results are printed to stdout so if we can
38+
# color that have the subprocess use color.
39+
if _colorize.can_colorize(file=sys.stdout):
40+
env['FORCE_COLOR'] = '1'
41+
3542
# Running the child from the same working directory as regrtest's original
3643
# invocation ensures that TEMPDIR for the child is the same when
3744
# sysconfig.is_python_build() is true. See issue 15300.

0 commit comments

Comments
 (0)