mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 18:18:22 +00:00
Fix issue with NO_COLOR, add test for Console with no file
This commit is contained in:
parent
104f630740
commit
2ef0c30905
2 changed files with 22 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue