Fix issue with NO_COLOR, add test for Console with no file

This commit is contained in:
Darren Burns 2022-09-07 14:13:00 +01:00
parent 104f630740
commit 2ef0c30905
No known key found for this signature in database
GPG key ID: B0939B45037DC345
2 changed files with 22 additions and 1 deletions

View file

@ -701,7 +701,14 @@ class Console:
if force_terminal is not None:
self._force_terminal = force_terminal
else:
self._force_terminal = self._environ.get("FORCE_COLOR") is not None
# If FORCE_COLOR env var has any value at all, we force terminal.
force_terminal = self._environ.get("FORCE_COLOR")
if force_terminal is not None:
self._force_terminal = bool(force_terminal)
else:
self._force_terminal = None
print(self._force_terminal)
self._file = file
self.quiet = quiet

View file

@ -5,6 +5,7 @@ import sys
import tempfile
from typing import Optional, Tuple, Type, Union
from unittest import mock
from unittest.mock import PropertyMock
import pytest
@ -897,6 +898,19 @@ def test_render_lines_height_minus_vertical_pad_is_negative():
console.render_lines(Padding("hello", pad=(1, 0)), options=options)
def test_no_stdout_file():
# Rich should work even if there's no file available to write to.
# For example, pythonw nullifies output streams.
# Built-in print silently no-ops in pythonw.
# Related: https://github.com/Textualize/rich/issues/2400
console = Console()
with mock.patch.object(
Console, "file", new_callable=PropertyMock
) as mock_file_property:
mock_file_property.return_value = None
console.print("hello world")
@mock.patch.dict(os.environ, {"FORCE_COLOR": "anything"})
def test_force_color():
# Even though we use a non-tty file, the presence of FORCE_COLOR env var