-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_console.py
More file actions
100 lines (78 loc) · 3.21 KB
/
test_console.py
File metadata and controls
100 lines (78 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Tests for rich module."""
import io
import sys
import pytest
from enrich.console import Console, should_do_markup
from pytest_mock import MockFixture # pylint: disable=wrong-import-order
def test_rich_console_ex() -> None:
"""Validate that ConsoleEx can capture output from print() calls."""
console = Console(record=True, redirect=True)
console.print("alpha")
print("beta") # noqa: T201
sys.stdout.write("gamma\n")
sys.stderr.write("delta\n")
# While not supposed to happen we want to be sure that this will not raise
# an exception. Some libraries may still sometimes send bytes to the
# streams, notable example being click.
# sys.stdout.write(b"epsilon\n")
text = console.export_text()
assert text == "alpha\nbeta\ngamma\ndelta\n"
def test_rich_console_ex_ansi() -> None:
"""Validate that ANSI sent to sys.stdout does not become garbage in record."""
print() # noqa: T201
console = Console(force_terminal=True, record=True, redirect=True)
console.print("[green]this from Console.print()[/green]", style="red")
text = console.export_text(clear=False)
assert "this from Console" in text
html = console.export_html(clear=False)
assert "#008000" in html
def test_console_soft_wrap() -> None:
"""Assures long prints on console are not wrapped when requested."""
console = Console(
file=io.StringIO(),
width=20,
record=True,
soft_wrap=True,
redirect=False,
)
text = 21 * "x"
console.print(text, end="")
# pylint: disable=no-member
assert console.file.getvalue() == text # type: ignore
result = console.export_text()
assert text in result
def test_console_print_ansi() -> None:
"""Validates that Console.print() with ANSI does not make break them."""
console = Console(force_terminal=True, record=True, soft_wrap=True, redirect=True)
text = "\033[92mfuture is green!\033[0m\nwowsers!"
console.print(text)
text_result = console.export_text(clear=False)
assert "future is green!\nwowsers!" in text_result
html_result = console.export_html()
assert "#00ff00" in html_result
def test_markup_detection_pycolors0() -> None:
"""Assure PY_COLORS=0 disables markup."""
with pytest.MonkeyPatch.context() as monkeypatch:
monkeypatch.setenv("PY_COLORS", "0")
assert not should_do_markup()
def test_markup_detection_pycolors1() -> None:
"""Assure PY_COLORS=1 enables markup."""
with pytest.MonkeyPatch.context() as monkeypatch:
monkeypatch.setenv("PY_COLORS", "1")
assert should_do_markup()
def test_markup_detection_tty_yes(mocker: MockFixture) -> None:
"""Assures TERM=xterm enables markup."""
mocker.patch("sys.stdout.isatty", return_value=True)
mocker.patch("os.environ", {"TERM": "xterm"})
assert should_do_markup()
mocker.resetall()
mocker.stopall()
def test_markup_detection_tty_no(mocker: MockFixture) -> None:
"""Assures that if no tty is reported we disable markup."""
mocker.patch("os.environ", {})
mocker.patch("sys.stdout.isatty", return_value=False)
assert not should_do_markup()
mocker.resetall()
mocker.stopall()
if __name__ == "__main__":
test_console_print_ansi()