| 1 |
import datetime |
| 2 |
import io |
| 3 |
import os |
| 4 |
import subprocess |
| 5 |
import sys |
| 6 |
import tempfile |
| 7 |
from typing import Optional, Tuple, Type, Union |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from rich import errors |
| 13 |
from rich._null_file import NullFile |
| 14 |
from rich.color import ColorSystem |
| 15 |
from rich.console import ( |
| 16 |
CaptureError, |
| 17 |
Console, |
| 18 |
ConsoleDimensions, |
| 19 |
ConsoleOptions, |
| 20 |
ScreenUpdate, |
| 21 |
group, |
| 22 |
) |
| 23 |
from rich.control import Control |
| 24 |
from rich.measure import measure_renderables |
| 25 |
from rich.padding import Padding |
| 26 |
from rich.pager import SystemPager |
| 27 |
from rich.panel import Panel |
| 28 |
from rich.region import Region |
| 29 |
from rich.segment import Segment |
| 30 |
from rich.status import Status |
| 31 |
from rich.style import Style |
| 32 |
from rich.text import Text |
| 33 |
|
| 34 |
os.get_terminal_size |
| 35 |
|
| 36 |
|
| 37 |
def test_dumb_terminal() -> None: |
| 38 |
console = Console(force_terminal=True, _environ={}) |
| 39 |
assert console.color_system is not None |
| 40 |
|
| 41 |
console = Console(force_terminal=True, _environ={"TERM": "dumb"}) |
| 42 |
assert console.color_system is None |
| 43 |
width, height = console.size |
| 44 |
assert width == 80 |
| 45 |
assert height == 25 |
| 46 |
|
| 47 |
|
| 48 |
def test_soft_wrap() -> None: |
| 49 |
console = Console(file=io.StringIO(), width=20, soft_wrap=True) |
| 50 |
console.print("foo " * 10) |
| 51 |
assert console.file.getvalue() == "foo " * 20 |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 55 |
def test_16color_terminal() -> None: |
| 56 |
console = Console( |
| 57 |
force_terminal=True, _environ={"TERM": "xterm-16color"}, legacy_windows=False |
| 58 |
) |
| 59 |
assert console.color_system == "standard" |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 63 |
def test_truecolor_terminal() -> None: |
| 64 |
console = Console( |
| 65 |
force_terminal=True, |
| 66 |
legacy_windows=False, |
| 67 |
_environ={"COLORTERM": "truecolor", "TERM": "xterm-16color"}, |
| 68 |
) |
| 69 |
assert console.color_system == "truecolor" |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 73 |
def test_kitty_terminal() -> None: |
| 74 |
console = Console( |
| 75 |
force_terminal=True, |
| 76 |
legacy_windows=False, |
| 77 |
_environ={"TERM": "xterm-kitty"}, |
| 78 |
) |
| 79 |
assert console.color_system == "256" |
| 80 |
|
| 81 |
|
| 82 |
def test_console_options_update() -> None: |
| 83 |
options = ConsoleOptions( |
| 84 |
ConsoleDimensions(80, 25), |
| 85 |
max_height=25, |
| 86 |
legacy_windows=False, |
| 87 |
min_width=10, |
| 88 |
max_width=20, |
| 89 |
is_terminal=False, |
| 90 |
encoding="utf-8", |
| 91 |
) |
| 92 |
options1 = options.update(width=15) |
| 93 |
assert options1.min_width == 15 and options1.max_width == 15 |
| 94 |
|
| 95 |
options2 = options.update(min_width=5, max_width=15, justify="right") |
| 96 |
assert ( |
| 97 |
options2.min_width == 5 |
| 98 |
and options2.max_width == 15 |
| 99 |
and options2.justify == "right" |
| 100 |
) |
| 101 |
|
| 102 |
options_copy = options.update() |
| 103 |
assert options_copy == options and options_copy is not options |
| 104 |
|
| 105 |
|
| 106 |
def test_console_options_update_height() -> None: |
| 107 |
options = ConsoleOptions( |
| 108 |
ConsoleDimensions(80, 25), |
| 109 |
max_height=25, |
| 110 |
legacy_windows=False, |
| 111 |
min_width=10, |
| 112 |
max_width=20, |
| 113 |
is_terminal=False, |
| 114 |
encoding="utf-8", |
| 115 |
) |
| 116 |
assert options.height is None |
| 117 |
render_options = options.update_height(12) |
| 118 |
assert options.height is None |
| 119 |
assert render_options.height == 12 |
| 120 |
assert render_options.max_height == 12 |
| 121 |
|
| 122 |
|
| 123 |
def test_init() -> None: |
| 124 |
console = Console(color_system=None) |
| 125 |
assert console._color_system == None |
| 126 |
console = Console(color_system="standard") |
| 127 |
assert console._color_system == ColorSystem.STANDARD |
| 128 |
console = Console(color_system="auto") |
| 129 |
|
| 130 |
|
| 131 |
def test_size() -> None: |
| 132 |
console = Console() |
| 133 |
w, h = console.size |
| 134 |
assert console.width == w |
| 135 |
|
| 136 |
console = Console(width=99, height=101, legacy_windows=False) |
| 137 |
w, h = console.size |
| 138 |
assert w == 99 and h == 101 |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
"is_windows,no_descriptor_size,stdin_size,stdout_size,stderr_size,expected_size", |
| 143 |
[ |
| 144 |
# on Windows we'll use `os.get_terminal_size()` without arguments... |
| 145 |
(True, (133, 24), ValueError, ValueError, ValueError, (80, 25)), |
| 146 |
(False, (133, 24), ValueError, ValueError, ValueError, (80, 25)), |
| 147 |
# ...while on other OS we'll try to pass stdin, then stdout, then stderr to it: |
| 148 |
(False, ValueError, (133, 24), ValueError, ValueError, (133, 24)), |
| 149 |
(False, ValueError, ValueError, (133, 24), ValueError, (133, 24)), |
| 150 |
(False, ValueError, ValueError, ValueError, (133, 24), (133, 24)), |
| 151 |
(False, ValueError, ValueError, ValueError, ValueError, (80, 25)), |
| 152 |
], |
| 153 |
) |
| 154 |
@mock.patch("rich.console.os.get_terminal_size") |
| 155 |
def test_size_can_fall_back_to_std_descriptors( |
| 156 |
get_terminal_size_mock: mock.MagicMock, |
| 157 |
is_windows: bool, |
| 158 |
no_descriptor_size: Union[Tuple[int, int], Type[ValueError]], |
| 159 |
stdin_size: Union[Tuple[int, int], Type[ValueError]], |
| 160 |
stdout_size: Union[Tuple[int, int], Type[ValueError]], |
| 161 |
stderr_size: Union[Tuple[int, int], Type[ValueError]], |
| 162 |
expected_size: Tuple[int, int], |
| 163 |
) -> None: |
| 164 |
def get_terminal_size_mock_impl(fileno: int = None) -> Tuple[int, int]: |
| 165 |
value = { |
| 166 |
None: no_descriptor_size, |
| 167 |
sys.__stdin__.fileno(): stdin_size, |
| 168 |
sys.__stdout__.fileno(): stdout_size, |
| 169 |
sys.__stderr__.fileno(): stderr_size, |
| 170 |
}[fileno] |
| 171 |
if value is ValueError: |
| 172 |
raise value |
| 173 |
return value |
| 174 |
|
| 175 |
get_terminal_size_mock.side_effect = get_terminal_size_mock_impl |
| 176 |
|
| 177 |
console = Console(legacy_windows=False) |
| 178 |
with mock.patch("rich.console.WINDOWS", new=is_windows): |
| 179 |
w, h = console.size |
| 180 |
assert (w, h) == expected_size |
| 181 |
|
| 182 |
|
| 183 |
def test_repr() -> None: |
| 184 |
console = Console() |
| 185 |
assert isinstance(repr(console), str) |
| 186 |
assert isinstance(str(console), str) |
| 187 |
|
| 188 |
|
| 189 |
def test_print() -> None: |
| 190 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 191 |
console.print("foo") |
| 192 |
assert console.file.getvalue() == "foo\n" |
| 193 |
|
| 194 |
|
| 195 |
def test_print_empty_with_end() -> None: |
| 196 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 197 |
console.print(end="!") |
| 198 |
assert console.file.getvalue() == "!" |
| 199 |
|
| 200 |
|
| 201 |
def test_print_multiple() -> None: |
| 202 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 203 |
console.print("foo", "bar") |
| 204 |
assert console.file.getvalue() == "foo bar\n" |
| 205 |
|
| 206 |
|
| 207 |
def test_print_text() -> None: |
| 208 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 209 |
console.print(Text("foo", style="bold")) |
| 210 |
assert console.file.getvalue() == "\x1b[1mfoo\x1b[0m\n" |
| 211 |
|
| 212 |
|
| 213 |
def test_print_text_multiple() -> None: |
| 214 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 215 |
console.print(Text("foo", style="bold"), Text("bar"), "baz") |
| 216 |
assert console.file.getvalue() == "\x1b[1mfoo\x1b[0m bar baz\n" |
| 217 |
|
| 218 |
|
| 219 |
def test_print_json() -> None: |
| 220 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 221 |
console.print_json('[false, true, null, "foo"]', indent=4) |
| 222 |
result = console.file.getvalue() |
| 223 |
print(repr(result)) |
| 224 |
expected = '\x1b[1m[\x1b[0m\n \x1b[3;91mfalse\x1b[0m,\n \x1b[3;92mtrue\x1b[0m,\n \x1b[3;35mnull\x1b[0m,\n \x1b[32m"foo"\x1b[0m\n\x1b[1m]\x1b[0m\n' |
| 225 |
assert result == expected |
| 226 |
|
| 227 |
|
| 228 |
def test_print_json_error() -> None: |
| 229 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 230 |
with pytest.raises(TypeError): |
| 231 |
console.print_json(["foo"], indent=4) |
| 232 |
|
| 233 |
|
| 234 |
def test_print_json_data() -> None: |
| 235 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 236 |
console.print_json(data=[False, True, None, "foo"], indent=4) |
| 237 |
result = console.file.getvalue() |
| 238 |
print(repr(result)) |
| 239 |
expected = '\x1b[1m[\x1b[0m\n \x1b[3;91mfalse\x1b[0m,\n \x1b[3;92mtrue\x1b[0m,\n \x1b[3;35mnull\x1b[0m,\n \x1b[32m"foo"\x1b[0m\n\x1b[1m]\x1b[0m\n' |
| 240 |
assert result == expected |
| 241 |
|
| 242 |
|
| 243 |
def test_print_json_ensure_ascii() -> None: |
| 244 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 245 |
console.print_json(data={"foo": "š©"}, ensure_ascii=False) |
| 246 |
result = console.file.getvalue() |
| 247 |
print(repr(result)) |
| 248 |
expected = '\x1b[1m{\x1b[0m\n \x1b[1;34m"foo"\x1b[0m: \x1b[32m"š©"\x1b[0m\n\x1b[1m}\x1b[0m\n' |
| 249 |
assert result == expected |
| 250 |
|
| 251 |
|
| 252 |
def test_print_json_with_default_ensure_ascii() -> None: |
| 253 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 254 |
console.print_json(data={"foo": "š©"}) |
| 255 |
result = console.file.getvalue() |
| 256 |
print(repr(result)) |
| 257 |
expected = '\x1b[1m{\x1b[0m\n \x1b[1;34m"foo"\x1b[0m: \x1b[32m"š©"\x1b[0m\n\x1b[1m}\x1b[0m\n' |
| 258 |
assert result == expected |
| 259 |
|
| 260 |
|
| 261 |
def test_print_json_indent_none() -> None: |
| 262 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 263 |
data = {"name": "apple", "count": 1} |
| 264 |
console.print_json(data=data, indent=None) |
| 265 |
result = console.file.getvalue() |
| 266 |
expected = '\x1b[1m{\x1b[0m\x1b[1;34m"name"\x1b[0m: \x1b[32m"apple"\x1b[0m, \x1b[1;34m"count"\x1b[0m: \x1b[1;36m1\x1b[0m\x1b[1m}\x1b[0m\n' |
| 267 |
assert result == expected |
| 268 |
|
| 269 |
|
| 270 |
def test_console_null_file(monkeypatch) -> None: |
| 271 |
# When stdout and stderr are null, Console.file should be replaced with NullFile |
| 272 |
monkeypatch.setattr("sys.stdout", None) |
| 273 |
monkeypatch.setattr("sys.stderr", None) |
| 274 |
|
| 275 |
console = Console() |
| 276 |
assert isinstance(console.file, NullFile) |
| 277 |
|
| 278 |
|
| 279 |
def test_log() -> None: |
| 280 |
console = Console( |
| 281 |
file=io.StringIO(), |
| 282 |
width=80, |
| 283 |
color_system="truecolor", |
| 284 |
log_time_format="TIME", |
| 285 |
log_path=False, |
| 286 |
_environ={}, |
| 287 |
) |
| 288 |
console.log("foo", style="red") |
| 289 |
expected = "\x1b[2;36mTIME\x1b[0m\x1b[2;36m \x1b[0m\x1b[31mfoo \x1b[0m\n" |
| 290 |
result = console.file.getvalue() |
| 291 |
print(repr(result)) |
| 292 |
assert result == expected |
| 293 |
|
| 294 |
|
| 295 |
def test_log_milliseconds() -> None: |
| 296 |
def time_formatter(timestamp: datetime) -> Text: |
| 297 |
return Text("TIME") |
| 298 |
|
| 299 |
console = Console( |
| 300 |
file=io.StringIO(), width=40, log_time_format=time_formatter, log_path=False |
| 301 |
) |
| 302 |
console.log("foo") |
| 303 |
result = console.file.getvalue() |
| 304 |
assert result == "TIME foo \n" |
| 305 |
|
| 306 |
|
| 307 |
def test_print_empty() -> None: |
| 308 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 309 |
console.print() |
| 310 |
assert console.file.getvalue() == "\n" |
| 311 |
|
| 312 |
|
| 313 |
def test_markup_highlight() -> None: |
| 314 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 315 |
console.print("'[bold]foo[/bold]'") |
| 316 |
assert ( |
| 317 |
console.file.getvalue() |
| 318 |
== "\x1b[32m'\x1b[0m\x1b[1;32mfoo\x1b[0m\x1b[32m'\x1b[0m\n" |
| 319 |
) |
| 320 |
|
| 321 |
|
| 322 |
def test_print_style() -> None: |
| 323 |
console = Console(file=io.StringIO(), color_system="truecolor") |
| 324 |
console.print("foo", style="bold") |
| 325 |
assert console.file.getvalue() == "\x1b[1mfoo\x1b[0m\n" |
| 326 |
|
| 327 |
|
| 328 |
def test_show_cursor() -> None: |
| 329 |
console = Console( |
| 330 |
file=io.StringIO(), force_terminal=True, legacy_windows=False, _environ={} |
| 331 |
) |
| 332 |
console.show_cursor(False) |
| 333 |
console.print("foo") |
| 334 |
console.show_cursor(True) |
| 335 |
assert console.file.getvalue() == "\x1b[?25lfoo\n\x1b[?25h" |
| 336 |
|
| 337 |
|
| 338 |
def test_clear() -> None: |
| 339 |
console = Console(file=io.StringIO(), force_terminal=True, _environ={}) |
| 340 |
console.clear() |
| 341 |
console.clear(home=False) |
| 342 |
assert console.file.getvalue() == "\033[2J\033[H" + "\033[2J" |
| 343 |
|
| 344 |
|
| 345 |
def test_clear_no_terminal() -> None: |
| 346 |
console = Console(file=io.StringIO()) |
| 347 |
console.clear() |
| 348 |
console.clear(home=False) |
| 349 |
assert console.file.getvalue() == "" |
| 350 |
|
| 351 |
|
| 352 |
def test_get_style() -> None: |
| 353 |
console = Console() |
| 354 |
console.get_style("repr.brace") == Style(bold=True) |
| 355 |
|
| 356 |
|
| 357 |
def test_get_style_default() -> None: |
| 358 |
console = Console() |
| 359 |
console.get_style("foobar", default="red") == Style(color="red") |
| 360 |
|
| 361 |
|
| 362 |
def test_get_style_error() -> None: |
| 363 |
console = Console() |
| 364 |
with pytest.raises(errors.MissingStyle): |
| 365 |
console.get_style("nosuchstyle") |
| 366 |
with pytest.raises(errors.MissingStyle): |
| 367 |
console.get_style("foo bar") |
| 368 |
|
| 369 |
|
| 370 |
def test_render_error() -> None: |
| 371 |
console = Console() |
| 372 |
with pytest.raises(errors.NotRenderableError): |
| 373 |
list(console.render([], console.options)) |
| 374 |
|
| 375 |
|
| 376 |
def test_control() -> None: |
| 377 |
console = Console(file=io.StringIO(), force_terminal=True, _environ={}) |
| 378 |
console.control(Control.clear()) |
| 379 |
console.print("BAR") |
| 380 |
assert console.file.getvalue() == "\x1b[2JBAR\n" |
| 381 |
|
| 382 |
|
| 383 |
def test_capture() -> None: |
| 384 |
console = Console() |
| 385 |
with console.capture() as capture: |
| 386 |
with pytest.raises(CaptureError): |
| 387 |
capture.get() |
| 388 |
console.print("Hello") |
| 389 |
assert capture.get() == "Hello\n" |
| 390 |
|
| 391 |
|
| 392 |
def test_input(monkeypatch, capsys) -> None: |
| 393 |
def fake_input(prompt=""): |
| 394 |
console.file.write(prompt) |
| 395 |
return "bar" |
| 396 |
|
| 397 |
monkeypatch.setattr("builtins.input", fake_input) |
| 398 |
console = Console() |
| 399 |
user_input = console.input(prompt="foo:") |
| 400 |
assert capsys.readouterr().out == "foo:" |
| 401 |
assert user_input == "bar" |
| 402 |
|
| 403 |
|
| 404 |
def test_input_password(monkeypatch, capsys) -> None: |
| 405 |
def fake_input(prompt, stream=None): |
| 406 |
console.file.write(prompt) |
| 407 |
return "bar" |
| 408 |
|
| 409 |
import getpass |
| 410 |
|
| 411 |
monkeypatch.setattr(getpass, "getpass", fake_input) |
| 412 |
console = Console() |
| 413 |
user_input = console.input(prompt="foo:", password=True) |
| 414 |
assert capsys.readouterr().out == "foo:" |
| 415 |
assert user_input == "bar" |
| 416 |
|
| 417 |
|
| 418 |
def test_status() -> None: |
| 419 |
console = Console(file=io.StringIO(), force_terminal=True, width=20) |
| 420 |
status = console.status("foo") |
| 421 |
assert isinstance(status, Status) |
| 422 |
|
| 423 |
|
| 424 |
def test_justify_none() -> None: |
| 425 |
console = Console(file=io.StringIO(), force_terminal=True, width=20) |
| 426 |
console.print("FOO", justify=None) |
| 427 |
assert console.file.getvalue() == "FOO\n" |
| 428 |
|
| 429 |
|
| 430 |
def test_justify_left() -> None: |
| 431 |
console = Console(file=io.StringIO(), force_terminal=True, width=20, _environ={}) |
| 432 |
console.print("FOO", justify="left") |
| 433 |
assert console.file.getvalue() == "FOO \n" |
| 434 |
|
| 435 |
|
| 436 |
def test_justify_center() -> None: |
| 437 |
console = Console(file=io.StringIO(), force_terminal=True, width=20, _environ={}) |
| 438 |
console.print("FOO", justify="center") |
| 439 |
assert console.file.getvalue() == " FOO \n" |
| 440 |
|
| 441 |
|
| 442 |
def test_justify_right() -> None: |
| 443 |
console = Console(file=io.StringIO(), force_terminal=True, width=20, _environ={}) |
| 444 |
console.print("FOO", justify="right") |
| 445 |
assert console.file.getvalue() == " FOO\n" |
| 446 |
|
| 447 |
|
| 448 |
def test_justify_renderable_none() -> None: |
| 449 |
console = Console( |
| 450 |
file=io.StringIO(), |
| 451 |
force_terminal=True, |
| 452 |
width=20, |
| 453 |
legacy_windows=False, |
| 454 |
_environ={}, |
| 455 |
) |
| 456 |
console.print(Panel("FOO", expand=False, padding=0), justify=None) |
| 457 |
assert console.file.getvalue() == "āāāāā®\nāFOOā\nā°āāāāÆ\n" |
| 458 |
|
| 459 |
|
| 460 |
def test_justify_renderable_left() -> None: |
| 461 |
console = Console( |
| 462 |
file=io.StringIO(), |
| 463 |
force_terminal=True, |
| 464 |
width=10, |
| 465 |
legacy_windows=False, |
| 466 |
_environ={}, |
| 467 |
) |
| 468 |
console.print(Panel("FOO", expand=False, padding=0), justify="left") |
| 469 |
assert console.file.getvalue() == "āāāāā® \nāFOOā \nā°āāā⯠\n" |
| 470 |
|
| 471 |
|
| 472 |
def test_justify_renderable_center() -> None: |
| 473 |
console = Console( |
| 474 |
file=io.StringIO(), |
| 475 |
force_terminal=True, |
| 476 |
width=10, |
| 477 |
legacy_windows=False, |
| 478 |
_environ={}, |
| 479 |
) |
| 480 |
console.print(Panel("FOO", expand=False, padding=0), justify="center") |
| 481 |
assert console.file.getvalue() == " āāāāā® \n āFOOā \n ā°āāā⯠\n" |
| 482 |
|
| 483 |
|
| 484 |
def test_justify_renderable_right() -> None: |
| 485 |
console = Console( |
| 486 |
file=io.StringIO(), |
| 487 |
force_terminal=True, |
| 488 |
width=20, |
| 489 |
legacy_windows=False, |
| 490 |
_environ={}, |
| 491 |
) |
| 492 |
console.print(Panel("FOO", expand=False, padding=0), justify="right") |
| 493 |
assert ( |
| 494 |
console.file.getvalue() |
| 495 |
== " āāāāā®\n āFOOā\n ā°āāāāÆ\n" |
| 496 |
) |
| 497 |
|
| 498 |
|
| 499 |
class BrokenRenderable: |
| 500 |
def __rich_console__(self, console, options): |
| 501 |
pass |
| 502 |
|
| 503 |
|
| 504 |
def test_render_broken_renderable() -> None: |
| 505 |
console = Console() |
| 506 |
broken = BrokenRenderable() |
| 507 |
with pytest.raises(errors.NotRenderableError): |
| 508 |
list(console.render(broken, console.options)) |
| 509 |
|
| 510 |
|
| 511 |
def test_export_text() -> None: |
| 512 |
console = Console(record=True, width=100) |
| 513 |
console.print("[b]foo") |
| 514 |
text = console.export_text() |
| 515 |
expected = "foo\n" |
| 516 |
assert text == expected |
| 517 |
|
| 518 |
|
| 519 |
def test_export_html() -> None: |
| 520 |
console = Console(record=True, width=100) |
| 521 |
console.print("[b]foo <script> 'test' [link=https://example.org]Click[/link]") |
| 522 |
html = console.export_html() |
| 523 |
print(repr(html)) |
| 524 |
expected = '<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n<style>\n.r1 {font-weight: bold}\n.r2 {color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold}\n.r3 {color: #008000; text-decoration-color: #008000; font-weight: bold}\nbody {\n color: #000000;\n background-color: #ffffff;\n}\n</style>\n</head>\n<body>\n <pre style="font-family:Menlo,\'DejaVu Sans Mono\',consolas,\'Courier New\',monospace"><code style="font-family:inherit"><span class="r1">foo <</span><span class="r2">script</span><span class="r1">> </span><span class="r3">'test'</span><span class="r1"> </span><a class="r1" href="https://example.org">Click</a>\n</code></pre>\n</body>\n</html>\n' |
| 525 |
assert html == expected |
| 526 |
|
| 527 |
|
| 528 |
def test_export_html_inline() -> None: |
| 529 |
console = Console(record=True, width=100) |
| 530 |
console.print("[b]foo [link=https://example.org]Click[/link]") |
| 531 |
html = console.export_html(inline_styles=True) |
| 532 |
print(repr(html)) |
| 533 |
expected = '<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n<style>\n\nbody {\n color: #000000;\n background-color: #ffffff;\n}\n</style>\n</head>\n<body>\n <pre style="font-family:Menlo,\'DejaVu Sans Mono\',consolas,\'Courier New\',monospace"><code style="font-family:inherit"><span style="font-weight: bold">foo </span><span style="font-weight: bold"><a href="https://example.org">Click</a></span>\n</code></pre>\n</body>\n</html>\n' |
| 534 |
assert html == expected |
| 535 |
|
| 536 |
|
| 537 |
EXPECTED_SVG = '<svg class="rich-terminal" viewBox="0 0 1238 74.4" xmlns="http://www.w3.org/2000/svg">\n <!-- Generated with Rich https://www.textualize.io -->\n <style>\n\n @font-face {\n font-family: "Fira Code";\n src: local("FiraCode-Regular"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");\n font-style: normal;\n font-weight: 400;\n }\n @font-face {\n font-family: "Fira Code";\n src: local("FiraCode-Bold"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");\n font-style: bold;\n font-weight: 700;\n }\n\n .terminal-3526644552-matrix {\n font-family: Fira Code, monospace;\n font-size: 20px;\n line-height: 24.4px;\n font-variant-east-asian: full-width;\n }\n\n .terminal-3526644552-title {\n font-size: 18px;\n font-weight: bold;\n font-family: arial;\n }\n\n .terminal-3526644552-r1 { fill: #608ab1;font-weight: bold }\n.terminal-3526644552-r2 { fill: #c5c8c6 }\n </style>\n\n <defs>\n <clipPath id="terminal-3526644552-clip-terminal">\n <rect x="0" y="0" width="1219.0" height="23.4" />\n </clipPath>\n \n </defs>\n\n <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="1236" height="72.4" rx="8"/><text class="terminal-3526644552-title" fill="#c5c8c6" text-anchor="middle" x="618" y="27">Rich</text>\n <g transform="translate(26,22)">\n <circle cx="0" cy="0" r="7" fill="#ff5f57"/>\n <circle cx="22" cy="0" r="7" fill="#febc2e"/>\n <circle cx="44" cy="0" r="7" fill="#28c840"/>\n </g>\n \n <g transform="translate(9, 41)" clip-path="url(#terminal-3526644552-clip-terminal)">\n <rect fill="#cc555a" x="0" y="1.5" width="36.6" height="24.65" shape-rendering="crispEdges"/>\n <g class="terminal-3526644552-matrix">\n <text class="terminal-3526644552-r1" x="0" y="20" textLength="36.6" clip-path="url(#terminal-3526644552-line-0)">foo</text><text class="terminal-3526644552-r2" x="48.8" y="20" textLength="61" clip-path="url(#terminal-3526644552-line-0)">Click</text><text class="terminal-3526644552-r2" x="1220" y="20" textLength="12.2" clip-path="url(#terminal-3526644552-line-0)">\n</text>\n </g>\n </g>\n</svg>\n' |
| 538 |
|
| 539 |
|
| 540 |
def test_export_svg() -> None: |
| 541 |
console = Console(record=True, width=100) |
| 542 |
console.print( |
| 543 |
"[b red on blue reverse]foo[/] [blink][link=https://example.org]Click[/link]" |
| 544 |
) |
| 545 |
svg = console.export_svg() |
| 546 |
print(repr(svg)) |
| 547 |
|
| 548 |
assert svg == EXPECTED_SVG |
| 549 |
|
| 550 |
|
| 551 |
def test_export_svg_specified_unique_id() -> None: |
| 552 |
expected_svg = EXPECTED_SVG.replace("terminal-3526644552", "given-id") |
| 553 |
console = Console(record=True, width=100) |
| 554 |
console.print( |
| 555 |
"[b red on blue reverse]foo[/] [blink][link=https://example.org]Click[/link]" |
| 556 |
) |
| 557 |
svg = console.export_svg(unique_id="given-id") |
| 558 |
print(repr(svg)) |
| 559 |
|
| 560 |
assert svg == expected_svg |
| 561 |
|
| 562 |
|
| 563 |
def test_save_svg() -> None: |
| 564 |
console = Console(record=True, width=100) |
| 565 |
console.print( |
| 566 |
"[b red on blue reverse]foo[/] [blink][link=https://example.org]Click[/link]" |
| 567 |
) |
| 568 |
with tempfile.TemporaryDirectory() as path: |
| 569 |
export_path = os.path.join(path, "example.svg") |
| 570 |
console.save_svg(export_path) |
| 571 |
with open(export_path, "rt", encoding="utf-8") as svg_file: |
| 572 |
assert svg_file.read() == EXPECTED_SVG |
| 573 |
|
| 574 |
|
| 575 |
def test_save_text() -> None: |
| 576 |
console = Console(record=True, width=100) |
| 577 |
console.print("foo") |
| 578 |
with tempfile.TemporaryDirectory() as path: |
| 579 |
export_path = os.path.join(path, "rich.txt") |
| 580 |
console.save_text(export_path) |
| 581 |
with open(export_path, "rt") as text_file: |
| 582 |
assert text_file.read() == "foo\n" |
| 583 |
|
| 584 |
|
| 585 |
def test_save_html() -> None: |
| 586 |
expected = '<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n<style>\n\nbody {\n color: #000000;\n background-color: #ffffff;\n}\n</style>\n</head>\n<body>\n <pre style="font-family:Menlo,\'DejaVu Sans Mono\',consolas,\'Courier New\',monospace"><code style="font-family:inherit">foo\n</code></pre>\n</body>\n</html>\n' |
| 587 |
console = Console(record=True, width=100) |
| 588 |
console.print("foo") |
| 589 |
with tempfile.TemporaryDirectory() as path: |
| 590 |
export_path = os.path.join(path, "example.html") |
| 591 |
console.save_html(export_path) |
| 592 |
with open(export_path, "rt") as html_file: |
| 593 |
html = html_file.read() |
| 594 |
print(repr(html)) |
| 595 |
assert html == expected |
| 596 |
|
| 597 |
|
| 598 |
def test_no_wrap() -> None: |
| 599 |
console = Console(width=10, file=io.StringIO()) |
| 600 |
console.print("foo bar baz egg", no_wrap=True) |
| 601 |
assert console.file.getvalue() == "foo bar ba\n" |
| 602 |
|
| 603 |
|
| 604 |
def test_soft_wrap() -> None: |
| 605 |
console = Console(width=10, file=io.StringIO()) |
| 606 |
console.print("foo bar baz egg", soft_wrap=True) |
| 607 |
assert console.file.getvalue() == "foo bar baz egg\n" |
| 608 |
|
| 609 |
|
| 610 |
def test_unicode_error() -> None: |
| 611 |
try: |
| 612 |
with tempfile.TemporaryFile("wt", encoding="ascii") as tmpfile: |
| 613 |
console = Console(file=tmpfile) |
| 614 |
console.print(":vampire:") |
| 615 |
except UnicodeEncodeError as error: |
| 616 |
assert "PYTHONIOENCODING" in str(error) |
| 617 |
else: |
| 618 |
assert False, "didn't raise UnicodeEncodeError" |
| 619 |
|
| 620 |
|
| 621 |
def test_bell() -> None: |
| 622 |
console = Console(force_terminal=True, _environ={}) |
| 623 |
console.begin_capture() |
| 624 |
console.bell() |
| 625 |
assert console.end_capture() == "\x07" |
| 626 |
|
| 627 |
|
| 628 |
def test_pager() -> None: |
| 629 |
console = Console(_environ={}) |
| 630 |
|
| 631 |
pager_content: Optional[str] = None |
| 632 |
|
| 633 |
def mock_pager(content: str) -> None: |
| 634 |
nonlocal pager_content |
| 635 |
pager_content = content |
| 636 |
|
| 637 |
pager = SystemPager() |
| 638 |
pager._pager = mock_pager |
| 639 |
|
| 640 |
with console.pager(pager): |
| 641 |
console.print("[bold]Hello World") |
| 642 |
assert pager_content == "Hello World\n" |
| 643 |
|
| 644 |
with console.pager(pager, styles=True, links=False): |
| 645 |
console.print("[bold link https:/example.org]Hello World") |
| 646 |
|
| 647 |
assert pager_content == "Hello World\n" |
| 648 |
|
| 649 |
|
| 650 |
def test_out() -> None: |
| 651 |
console = Console(width=10) |
| 652 |
console.begin_capture() |
| 653 |
console.out(*(["foo bar"] * 5), sep=".", end="X") |
| 654 |
assert console.end_capture() == "foo bar.foo bar.foo bar.foo bar.foo barX" |
| 655 |
|
| 656 |
|
| 657 |
def test_render_group() -> None: |
| 658 |
@group(fit=False) |
| 659 |
def renderable(): |
| 660 |
yield "one" |
| 661 |
yield "two" |
| 662 |
yield "three" # <- largest width of 5 |
| 663 |
yield "four" |
| 664 |
|
| 665 |
renderables = [renderable() for _ in range(4)] |
| 666 |
console = Console(width=42) |
| 667 |
min_width, _ = measure_renderables(console, console.options, renderables) |
| 668 |
assert min_width == 42 |
| 669 |
|
| 670 |
|
| 671 |
def test_render_group_fit() -> None: |
| 672 |
@group() |
| 673 |
def renderable(): |
| 674 |
yield "one" |
| 675 |
yield "two" |
| 676 |
yield "three" # <- largest width of 5 |
| 677 |
yield "four" |
| 678 |
|
| 679 |
renderables = [renderable() for _ in range(4)] |
| 680 |
|
| 681 |
console = Console(width=42) |
| 682 |
|
| 683 |
min_width, _ = measure_renderables(console, console.options, renderables) |
| 684 |
assert min_width == 5 |
| 685 |
|
| 686 |
|
| 687 |
def test_get_time() -> None: |
| 688 |
console = Console( |
| 689 |
get_time=lambda: 99, get_datetime=lambda: datetime.datetime(1974, 7, 5) |
| 690 |
) |
| 691 |
assert console.get_time() == 99 |
| 692 |
assert console.get_datetime() == datetime.datetime(1974, 7, 5) |
| 693 |
|
| 694 |
|
| 695 |
def test_console_style() -> None: |
| 696 |
console = Console( |
| 697 |
file=io.StringIO(), color_system="truecolor", force_terminal=True, style="red" |
| 698 |
) |
| 699 |
console.print("foo") |
| 700 |
expected = "\x1b[31mfoo\x1b[0m\n" |
| 701 |
result = console.file.getvalue() |
| 702 |
assert result == expected |
| 703 |
|
| 704 |
|
| 705 |
def test_no_color() -> None: |
| 706 |
console = Console( |
| 707 |
file=io.StringIO(), color_system="truecolor", force_terminal=True, no_color=True |
| 708 |
) |
| 709 |
console.print("[bold magenta on red]FOO") |
| 710 |
expected = "\x1b[1mFOO\x1b[0m\n" |
| 711 |
result = console.file.getvalue() |
| 712 |
print(repr(result)) |
| 713 |
assert result == expected |
| 714 |
|
| 715 |
|
| 716 |
def test_quiet() -> None: |
| 717 |
console = Console(file=io.StringIO(), quiet=True) |
| 718 |
console.print("Hello, World!") |
| 719 |
assert console.file.getvalue() == "" |
| 720 |
|
| 721 |
|
| 722 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 723 |
def test_screen() -> None: |
| 724 |
console = Console( |
| 725 |
color_system=None, force_terminal=True, force_interactive=True, _environ={} |
| 726 |
) |
| 727 |
with console.capture() as capture: |
| 728 |
with console.screen(): |
| 729 |
console.print("Don't panic") |
| 730 |
expected = "\x1b[?1049h\x1b[H\x1b[?25lDon't panic\n\x1b[?1049l\x1b[?25h" |
| 731 |
result = capture.get() |
| 732 |
print(repr(result)) |
| 733 |
assert result == expected |
| 734 |
|
| 735 |
|
| 736 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 737 |
def test_screen_update() -> None: |
| 738 |
console = Console( |
| 739 |
width=20, height=4, color_system="truecolor", force_terminal=True, _environ={} |
| 740 |
) |
| 741 |
with console.capture() as capture: |
| 742 |
with console.screen() as screen: |
| 743 |
screen.update("foo", style="blue") |
| 744 |
screen.update("bar") |
| 745 |
screen.update() |
| 746 |
result = capture.get() |
| 747 |
print(repr(result)) |
| 748 |
expected = "\x1b[?1049h\x1b[H\x1b[?25l\x1b[34mfoo\x1b[0m\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\x1b[34mbar\x1b[0m\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\x1b[34mbar\x1b[0m\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\n\x1b[34m \x1b[0m\x1b[?1049l\x1b[?25h" |
| 749 |
assert result == expected |
| 750 |
|
| 751 |
|
| 752 |
def test_height() -> None: |
| 753 |
console = Console(width=80, height=46) |
| 754 |
assert console.height == 46 |
| 755 |
|
| 756 |
|
| 757 |
def test_columns_env() -> None: |
| 758 |
console = Console(_environ={"COLUMNS": "314"}, legacy_windows=False) |
| 759 |
assert console.width == 314 |
| 760 |
# width take precedence |
| 761 |
console = Console(width=40, _environ={"COLUMNS": "314"}, legacy_windows=False) |
| 762 |
assert console.width == 40 |
| 763 |
# Should not fail |
| 764 |
console = Console(width=40, _environ={"COLUMNS": "broken"}, legacy_windows=False) |
| 765 |
|
| 766 |
|
| 767 |
def test_lines_env() -> None: |
| 768 |
console = Console(_environ={"LINES": "220"}) |
| 769 |
assert console.height == 220 |
| 770 |
# height take precedence |
| 771 |
console = Console(height=40, _environ={"LINES": "220"}) |
| 772 |
assert console.height == 40 |
| 773 |
# Should not fail |
| 774 |
console = Console(width=40, _environ={"LINES": "broken"}) |
| 775 |
|
| 776 |
|
| 777 |
def test_screen_update_class() -> None: |
| 778 |
screen_update = ScreenUpdate([[Segment("foo")], [Segment("bar")]], 5, 10) |
| 779 |
assert screen_update.x == 5 |
| 780 |
assert screen_update.y == 10 |
| 781 |
|
| 782 |
console = Console(force_terminal=True) |
| 783 |
console.begin_capture() |
| 784 |
console.print(screen_update) |
| 785 |
result = console.end_capture() |
| 786 |
print(repr(result)) |
| 787 |
expected = "\x1b[11;6Hfoo\x1b[12;6Hbar" |
| 788 |
assert result == expected |
| 789 |
|
| 790 |
|
| 791 |
def test_is_alt_screen() -> None: |
| 792 |
console = Console(force_terminal=True) |
| 793 |
if console.legacy_windows: |
| 794 |
return |
| 795 |
assert not console.is_alt_screen |
| 796 |
with console.screen(): |
| 797 |
assert console.is_alt_screen |
| 798 |
assert not console.is_alt_screen |
| 799 |
|
| 800 |
|
| 801 |
def test_set_console_title() -> None: |
| 802 |
console = Console(force_terminal=True, _environ={}) |
| 803 |
if console.legacy_windows: |
| 804 |
return |
| 805 |
|
| 806 |
with console.capture() as captured: |
| 807 |
console.set_window_title("hello") |
| 808 |
|
| 809 |
result = captured.get() |
| 810 |
assert result == "\x1b]0;hello\x07" |
| 811 |
|
| 812 |
|
| 813 |
def test_update_screen() -> None: |
| 814 |
console = Console(force_terminal=True, width=20, height=5, _environ={}) |
| 815 |
if console.legacy_windows: |
| 816 |
return |
| 817 |
with pytest.raises(errors.NoAltScreen): |
| 818 |
console.update_screen("foo") |
| 819 |
console.begin_capture() |
| 820 |
with console.screen(): |
| 821 |
console.update_screen("foo") |
| 822 |
console.update_screen("bar", region=Region(2, 3, 8, 4)) |
| 823 |
result = console.end_capture() |
| 824 |
print(repr(result)) |
| 825 |
expected = "\x1b[?1049h\x1b[H\x1b[?25l\x1b[1;1Hfoo \x1b[2;1H \x1b[3;1H \x1b[4;1H \x1b[5;1H \x1b[4;3Hbar \x1b[5;3H \x1b[6;3H \x1b[7;3H \x1b[?1049l\x1b[?25h" |
| 826 |
assert result == expected |
| 827 |
|
| 828 |
|
| 829 |
def test_update_screen_lines() -> None: |
| 830 |
console = Console(force_terminal=True, width=20, height=5) |
| 831 |
if console.legacy_windows: |
| 832 |
return |
| 833 |
with pytest.raises(errors.NoAltScreen): |
| 834 |
console.update_screen_lines([]) |
| 835 |
|
| 836 |
|
| 837 |
def test_update_options_markup() -> None: |
| 838 |
console = Console() |
| 839 |
options = console.options |
| 840 |
assert options.update(markup=False).markup == False |
| 841 |
assert options.update(markup=True).markup == True |
| 842 |
|
| 843 |
|
| 844 |
def test_print_width_zero() -> None: |
| 845 |
console = Console() |
| 846 |
with console.capture() as capture: |
| 847 |
console.print("Hello", width=0) |
| 848 |
assert capture.get() == "" |
| 849 |
|
| 850 |
|
| 851 |
def test_size_properties() -> None: |
| 852 |
console = Console(width=80, height=25, legacy_windows=False) |
| 853 |
assert console.size == ConsoleDimensions(80, 25) |
| 854 |
console.size = (10, 20) |
| 855 |
assert console.size == ConsoleDimensions(10, 20) |
| 856 |
console.width = 5 |
| 857 |
assert console.size == ConsoleDimensions(5, 20) |
| 858 |
console.height = 10 |
| 859 |
assert console.size == ConsoleDimensions(5, 10) |
| 860 |
|
| 861 |
|
| 862 |
def test_print_newline_start() -> None: |
| 863 |
console = Console(width=80, height=25) |
| 864 |
console.begin_capture() |
| 865 |
console.print("Foo", new_line_start=True) |
| 866 |
console.print("Foo\nbar\n", new_line_start=True) |
| 867 |
result = console.end_capture() |
| 868 |
|
| 869 |
assert result == "Foo\n\nFoo\nbar\n\n" |
| 870 |
|
| 871 |
|
| 872 |
def test_is_terminal_broken_file() -> None: |
| 873 |
console = Console() |
| 874 |
|
| 875 |
def _mock_isatty(): |
| 876 |
raise ValueError() |
| 877 |
|
| 878 |
console.file.isatty = _mock_isatty |
| 879 |
|
| 880 |
assert console.is_terminal == False |
| 881 |
|
| 882 |
|
| 883 |
@pytest.mark.skipif(sys.platform == "win32", reason="not relevant on Windows") |
| 884 |
def test_detect_color_system() -> None: |
| 885 |
console = Console(_environ={"TERM": "rxvt-unicode-256color"}, force_terminal=True) |
| 886 |
assert console._detect_color_system() == ColorSystem.EIGHT_BIT |
| 887 |
|
| 888 |
|
| 889 |
def test_reset_height() -> None: |
| 890 |
"""Test height is reset when rendering complex renderables.""" |
| 891 |
|
| 892 |
# https://github.com/Textualize/rich/issues/2042 |
| 893 |
class Panels: |
| 894 |
def __rich_console__(self, console, options): |
| 895 |
yield Panel("foo") |
| 896 |
yield Panel("bar") |
| 897 |
|
| 898 |
console = Console( |
| 899 |
force_terminal=True, |
| 900 |
color_system="truecolor", |
| 901 |
width=20, |
| 902 |
height=40, |
| 903 |
legacy_windows=False, |
| 904 |
) |
| 905 |
|
| 906 |
with console.capture() as capture: |
| 907 |
console.print(Panel(Panels()), height=12) |
| 908 |
result = capture.get() |
| 909 |
print(repr(result)) |
| 910 |
expected = "āāāāāāāāāāāāāāāāāāāā®\nā āāāāāāāāāāāāāāāā® ā\nā ā foo ā ā\nā ā°āāāāāāāāāāāāāā⯠ā\nā āāāāāāāāāāāāāāāā® ā\nā ā bar ā ā\nā ā°āāāāāāāāāāāāāā⯠ā\nā ā\nā ā\nā ā\nā ā\nā°āāāāāāāāāāāāāāāāāāāÆ\n" |
| 911 |
|
| 912 |
assert result == expected |
| 913 |
|
| 914 |
|
| 915 |
def test_render_lines_height_minus_vertical_pad_is_negative() -> None: |
| 916 |
# https://github.com/Textualize/textual/issues/389 |
| 917 |
console = Console( |
| 918 |
force_terminal=True, |
| 919 |
color_system="truecolor", |
| 920 |
width=20, |
| 921 |
height=40, |
| 922 |
legacy_windows=False, |
| 923 |
) |
| 924 |
options = console.options.update_height(1) |
| 925 |
|
| 926 |
# Ensuring that no exception is raised... |
| 927 |
console.render_lines(Padding("hello", pad=(1, 0)), options=options) |
| 928 |
|
| 929 |
|
| 930 |
def test_recording_no_stdout_and_no_stderr_files(monkeypatch) -> None: |
| 931 |
# Rich should work even if there's no file available to write to. |
| 932 |
# For example, pythonw nullifies output streams. |
| 933 |
# Built-in print silently no-ops in pythonw. |
| 934 |
# Related: https://github.com/Textualize/rich/issues/2400 |
| 935 |
monkeypatch.setattr("sys.stdout", None) |
| 936 |
monkeypatch.setattr("sys.stderr", None) |
| 937 |
console = Console(record=True) |
| 938 |
console.print("hello world") |
| 939 |
text = console.export_text() |
| 940 |
assert text == "hello world\n" |
| 941 |
|
| 942 |
|
| 943 |
def test_capturing_no_stdout_and_no_stderr_files(monkeypatch) -> None: |
| 944 |
monkeypatch.setattr("sys.stdout", None) |
| 945 |
monkeypatch.setattr("sys.stderr", None) |
| 946 |
console = Console() |
| 947 |
with console.capture() as capture: |
| 948 |
console.print("hello world") |
| 949 |
assert capture.get() == "hello world\n" |
| 950 |
|
| 951 |
|
| 952 |
@pytest.mark.parametrize("env_value", ["", "something", "0"]) |
| 953 |
def test_force_color(env_value) -> None: |
| 954 |
# Even though we use a non-tty file, the presence of FORCE_COLOR env var |
| 955 |
# means is_terminal returns True. |
| 956 |
console = Console(file=io.StringIO(), _environ={"FORCE_COLOR": env_value}) |
| 957 |
assert console.is_terminal |
| 958 |
|
| 959 |
|
| 960 |
def test_force_color_jupyter() -> None: |
| 961 |
# FORCE_COLOR above doesn't happen in a Jupyter kernel |
| 962 |
console = Console( |
| 963 |
file=io.StringIO(), _environ={"FORCE_COLOR": "1"}, force_jupyter=True |
| 964 |
) |
| 965 |
assert not console.is_terminal |
| 966 |
|
| 967 |
|
| 968 |
def test_force_color() -> None: |
| 969 |
console = Console( |
| 970 |
file=io.StringIO(), |
| 971 |
_environ={ |
| 972 |
"FORCE_COLOR": "1", |
| 973 |
"TERM": "xterm-256color", |
| 974 |
"COLORTERM": "truecolor", |
| 975 |
}, |
| 976 |
) |
| 977 |
assert console.color_system in ("truecolor", "windows") |
| 978 |
|
| 979 |
|
| 980 |
def test_reenable_highlighting() -> None: |
| 981 |
"""Check that when highlighting is disabled, it can be reenabled in print()""" |
| 982 |
console = Console( |
| 983 |
file=io.StringIO(), |
| 984 |
_environ={ |
| 985 |
"FORCE_COLOR": "1", |
| 986 |
"TERM": "xterm-256color", |
| 987 |
"COLORTERM": "truecolor", |
| 988 |
}, |
| 989 |
highlight=False, |
| 990 |
) |
| 991 |
console.print("[1, 2, 3]") |
| 992 |
console.print("[1, 2, 3]", highlight=True) |
| 993 |
output = console.file.getvalue() |
| 994 |
lines = output.splitlines() |
| 995 |
print(repr(lines)) |
| 996 |
# First line not highlighted |
| 997 |
assert lines[0] == "[1, 2, 3]" |
| 998 |
# Second line highlighted |
| 999 |
|
| 1000 |
assert ( |
| 1001 |
lines[1] |
| 1002 |
== "\x1b[1m[\x1b[0m\x1b[1;36m1\x1b[0m, \x1b[1;36m2\x1b[0m, \x1b[1;36m3\x1b[0m\x1b[1m]\x1b[0m" |
| 1003 |
) |
| 1004 |
|
| 1005 |
|
| 1006 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 1007 |
def test_brokenpipeerror() -> None: |
| 1008 |
"""Test BrokenPipe works as expected.""" |
| 1009 |
which_py, which_head = (["which", cmd] for cmd in ("python", "head")) |
| 1010 |
rich_cmd = "python -m rich".split() |
| 1011 |
for cmd in [which_py, which_head, rich_cmd]: |
| 1012 |
check = subprocess.run(cmd).returncode |
| 1013 |
if check != 0: |
| 1014 |
return # Only test on suitable Unix platforms |
| 1015 |
head_cmd = "head -1".split() |
| 1016 |
proc1 = subprocess.Popen(rich_cmd, stdout=subprocess.PIPE) |
| 1017 |
proc2 = subprocess.Popen(head_cmd, stdin=proc1.stdout, stdout=subprocess.PIPE) |
| 1018 |
proc1.stdout.close() |
| 1019 |
output, _ = proc2.communicate() |
| 1020 |
proc1.wait() |
| 1021 |
proc2.wait() |
| 1022 |
assert proc1.returncode == 1 |
| 1023 |
assert proc2.returncode == 0 |
| 1024 |
|
| 1025 |
|
| 1026 |
def test_capture_and_record() -> None: |
| 1027 |
"""Regression test for https://github.com/Textualize/rich/issues/2563""" |
| 1028 |
|
| 1029 |
console = Console(record=True) |
| 1030 |
print("Before Capture started:") |
| 1031 |
console.print("[blue underline]Print 0") |
| 1032 |
with console.capture() as capture: |
| 1033 |
console.print("[blue underline]Print 1") |
| 1034 |
console.print("[blue underline]Print 2") |
| 1035 |
console.print("[blue underline]Print 3") |
| 1036 |
console.print("[blue underline]Print 4") |
| 1037 |
|
| 1038 |
capture_content = capture.get() |
| 1039 |
print(repr(capture_content)) |
| 1040 |
assert capture_content == "Print 1\nPrint 2\nPrint 3\nPrint 4\n" |
| 1041 |
|
| 1042 |
recorded_content = console.export_text() |
| 1043 |
print(repr(recorded_content)) |
| 1044 |
assert recorded_content == "Print 0\n" |
| 1045 |
|
| 1046 |
|
| 1047 |
def test_tty_interactive() -> None: |
| 1048 |
"""Check TTY_INTERACTIVE environment var.""" |
| 1049 |
|
| 1050 |
# Bytes file, not interactive |
| 1051 |
console = Console(file=io.BytesIO()) |
| 1052 |
assert not console.is_interactive |
| 1053 |
|
| 1054 |
# Bytes file, force interactive |
| 1055 |
console = Console(file=io.BytesIO(), _environ={"TTY_INTERACTIVE": "1"}) |
| 1056 |
assert console.is_interactive |
| 1057 |
|
| 1058 |
# Force tty compatible, should be interactive |
| 1059 |
console = Console(file=io.BytesIO(), _environ={"TTY_COMPATIBLE": "1"}) |
| 1060 |
assert console.is_interactive |
| 1061 |
|
| 1062 |
# Force tty compatible, force not interactive |
| 1063 |
console = Console( |
| 1064 |
file=io.BytesIO(), _environ={"TTY_COMPATIBLE": "1", "TTY_INTERACTIVE": "0"} |
| 1065 |
) |
| 1066 |
|
| 1067 |
# Bytes file, Unknown value of TTY_INTERACTIVE should still auto-detect |
| 1068 |
console = Console(file=io.BytesIO(), _environ={"TTY_INTERACTIVE": "foo"}) |
| 1069 |
assert not console.is_interactive |
| 1070 |
|
| 1071 |
|
| 1072 |
def test_tty_compatible() -> None: |
| 1073 |
"""Check TTY_COMPATIBLE environment var.""" |
| 1074 |
|
| 1075 |
class FakeTTY: |
| 1076 |
"""An file file-like which reports it is a TTY.""" |
| 1077 |
|
| 1078 |
def __init__(self) -> None: |
| 1079 |
self.called_isatty = False |
| 1080 |
|
| 1081 |
def isatty(self) -> bool: |
| 1082 |
self.called_isatty = True |
| 1083 |
return True |
| 1084 |
|
| 1085 |
class FakeFile: |
| 1086 |
"""A file object that reports False for isatty""" |
| 1087 |
|
| 1088 |
def __init__(self) -> None: |
| 1089 |
self.called_isatty = False |
| 1090 |
|
| 1091 |
def isatty(self) -> bool: |
| 1092 |
self.called_isatty = True |
| 1093 |
return False |
| 1094 |
|
| 1095 |
# Console file is not a TTY |
| 1096 |
console = Console(file=FakeFile()) |
| 1097 |
# Not a TTY, so is_terminal should be False |
| 1098 |
assert not console.is_terminal |
| 1099 |
# Should have called isatty to auto-detect tty support |
| 1100 |
assert console.file.called_isatty |
| 1101 |
|
| 1102 |
# Not a terminal |
| 1103 |
console = Console(file=FakeFile(), _environ={"TTY_COMPATIBLE": "1"}) |
| 1104 |
# env TTY_COMPATIBLE=1 should report that it is a terminal |
| 1105 |
assert console.is_terminal |
| 1106 |
# Should not have called file.isattry |
| 1107 |
assert not console.file.called_isatty |
| 1108 |
|
| 1109 |
# File is a fake TTY |
| 1110 |
console = Console(file=FakeTTY()) |
| 1111 |
# Should report True |
| 1112 |
assert console.is_terminal |
| 1113 |
# Should have auto-detected |
| 1114 |
assert console.file.called_isatty |
| 1115 |
|
| 1116 |
# File is a fake TTY |
| 1117 |
console = Console(file=FakeTTY(), _environ={"TTY_COMPATIBLE": ""}) |
| 1118 |
# Blank TTY_COMPATIBLE should auto-detect, so is_terminal is True |
| 1119 |
assert console.is_terminal |
| 1120 |
# Should have auto-detected |
| 1121 |
assert console.file.called_isatty |
| 1122 |
|
| 1123 |
# File is a fake TTY |
| 1124 |
console = Console(file=FakeTTY(), _environ={"TTY_COMPATIBLE": "whatever"}) |
| 1125 |
# Any pother value should auto-detect |
| 1126 |
assert console.is_terminal |
| 1127 |
# Should have auto-detected |
| 1128 |
assert console.file.called_isatty |
| 1129 |
|
| 1130 |
# TTY_COMPATIBLE should override file.isattry |
| 1131 |
console = Console(file=FakeTTY(), _environ={"TTY_COMPATIBLE": "0"}) |
| 1132 |
# Should report that it is *not* a terminal |
| 1133 |
assert not console.is_terminal |
| 1134 |
# Should not have auto-detected |
| 1135 |
assert not console.file.called_isatty |