Skip to content

Conversation

cmaloney
Copy link
Contributor

@cmaloney cmaloney commented Oct 14, 2025

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 returns False 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.

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.
@cmaloney
Copy link
Contributor Author

I think this can be skip news as it's a strictly internal change / libregrtest isn't externally exposed

timeout = runtests.timeout

if can_colorize(file=sys.stdout):
os.environ['FORCE_COLOR'] = "1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change also needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for single-process mode coloring of unittest output because unittest gets given a io.StringIO as its stdout (on which isatty returns False). ./python -m test -W --single-process with a failing test output is uncolored without this, colored with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been pondering if this would be better done by setting os.environ['FORCE_COLOR'] inside libregrtest main (

def main(self, tests: TestList | None = None) -> NoReturn:
if self.want_add_python_opts:
self._add_python_opts()
self._init()
if self.want_cleanup:
cleanup_temp_dir(self.tmp_dir)
sys.exit(0)
if self.want_wait:
input("Press any key to continue...")
setup_test_dir(self.test_dir)
selected, tests = self.find_tests(tests)
exitcode = 0
if self.want_list_tests:
self.list_tests(selected)
elif self.want_list_cases:
list_cases(selected,
match_tests=self.match_tests,
test_dir=self.test_dir)
else:
exitcode = self.run_tests(selected, tests)
) rather than inside the the single and parallel code paths separately; that seems like it's covering too much code though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to move these 2 lines in setup_tests()?

@vstinner
Copy link
Member

You might add a NEWS entry in the Tests category.

timeout = runtests.timeout

if can_colorize(file=sys.stdout):
os.environ['FORCE_COLOR'] = "1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to move these 2 lines in setup_tests()?

env['TEMP'] = tmp_dir
env['TMP'] = tmp_dir

# The subcommand is run with a temporary output which means it is not a tty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# The subcommand is run with a temporary output which means it is not a tty
# The subcommand is run with a temporary output which means it is not a TTY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants