2020-04-26 15:28:18 +00:00
|
|
|
import io
|
2020-05-14 19:15:31 +00:00
|
|
|
import os
|
2020-07-27 16:33:51 +00:00
|
|
|
import sys
|
2020-05-14 19:15:31 +00:00
|
|
|
import tempfile
|
2020-04-26 15:28:18 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-09-23 14:53:41 +00:00
|
|
|
from rich.color import ColorSystem
|
|
|
|
from rich.console import CaptureError, Console, ConsoleOptions
|
2020-04-26 15:28:18 +00:00
|
|
|
from rich import errors
|
2020-05-17 11:39:51 +00:00
|
|
|
from rich.panel import Panel
|
2019-12-26 16:51:47 +00:00
|
|
|
from rich.style import Style
|
|
|
|
|
|
|
|
|
2020-07-22 15:18:35 +00:00
|
|
|
def test_dumb_terminal():
|
|
|
|
console = Console(force_terminal=True)
|
|
|
|
assert console.color_system is not None
|
|
|
|
|
|
|
|
console = Console(force_terminal=True, _environ={"TERM": "dumb"})
|
|
|
|
assert console.color_system is None
|
|
|
|
width, height = console.size
|
|
|
|
assert width == 80
|
|
|
|
assert height == 25
|
|
|
|
|
|
|
|
|
2020-07-27 16:33:51 +00:00
|
|
|
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
|
2020-07-27 16:21:03 +00:00
|
|
|
def test_16color_terminal():
|
2020-07-27 16:27:50 +00:00
|
|
|
console = Console(
|
|
|
|
force_terminal=True, _environ={"TERM": "xterm-16color"}, legacy_windows=False
|
|
|
|
)
|
2020-07-27 16:33:51 +00:00
|
|
|
assert console.color_system == "standard"
|
2020-07-27 16:21:03 +00:00
|
|
|
|
|
|
|
|
2020-07-27 16:33:51 +00:00
|
|
|
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
|
2020-07-27 16:21:03 +00:00
|
|
|
def test_truecolor_terminal():
|
|
|
|
console = Console(
|
|
|
|
force_terminal=True,
|
2020-07-27 16:27:50 +00:00
|
|
|
legacy_windows=False,
|
2020-07-27 16:21:03 +00:00
|
|
|
_environ={"COLORTERM": "truecolor", "TERM": "xterm-16color"},
|
|
|
|
)
|
|
|
|
assert console.color_system == "truecolor"
|
|
|
|
|
|
|
|
|
2019-12-26 16:51:47 +00:00
|
|
|
def test_console_options_update():
|
|
|
|
options = ConsoleOptions(
|
|
|
|
min_width=10, max_width=20, is_terminal=False, encoding="utf-8"
|
|
|
|
)
|
|
|
|
options1 = options.update(width=15)
|
|
|
|
assert options1.min_width == 15 and options1.max_width == 15
|
|
|
|
|
|
|
|
options2 = options.update(min_width=5, max_width=15, justify="right")
|
|
|
|
assert (
|
|
|
|
options2.min_width == 5
|
|
|
|
and options2.max_width == 15
|
|
|
|
and options2.justify == "right"
|
|
|
|
)
|
|
|
|
|
|
|
|
options_copy = options.update()
|
|
|
|
assert options_copy == options and options_copy is not options
|
|
|
|
|
|
|
|
|
|
|
|
def test_init():
|
|
|
|
console = Console(color_system=None)
|
|
|
|
assert console._color_system == None
|
|
|
|
console = Console(color_system="standard")
|
|
|
|
assert console._color_system == ColorSystem.STANDARD
|
|
|
|
console = Console(color_system="auto")
|
|
|
|
|
|
|
|
|
|
|
|
def test_size():
|
|
|
|
console = Console()
|
|
|
|
w, h = console.size
|
|
|
|
assert console.width == w
|
|
|
|
|
|
|
|
console = Console(width=99, height=101)
|
|
|
|
w, h = console.size
|
|
|
|
assert w == 99 and h == 101
|
2020-04-02 15:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_repr():
|
|
|
|
console = Console()
|
2020-04-26 15:28:18 +00:00
|
|
|
assert isinstance(repr(console), str)
|
|
|
|
assert isinstance(str(console), str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_print():
|
|
|
|
console = Console(file=io.StringIO(), color_system="truecolor")
|
|
|
|
console.print("foo")
|
|
|
|
assert console.file.getvalue() == "foo\n"
|
|
|
|
|
|
|
|
|
2020-04-27 15:22:39 +00:00
|
|
|
def test_print_empty():
|
|
|
|
console = Console(file=io.StringIO(), color_system="truecolor")
|
|
|
|
console.print()
|
|
|
|
assert console.file.getvalue() == "\n"
|
|
|
|
|
|
|
|
|
2020-05-08 12:36:12 +00:00
|
|
|
def test_markup_highlight():
|
|
|
|
console = Console(file=io.StringIO(), color_system="truecolor")
|
|
|
|
console.print("'[bold]foo[/bold]'")
|
|
|
|
assert (
|
|
|
|
console.file.getvalue()
|
|
|
|
== "\x1b[32m'\x1b[0m\x1b[1;32mfoo\x1b[0m\x1b[32m'\x1b[0m\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-26 15:28:18 +00:00
|
|
|
def test_print_style():
|
|
|
|
console = Console(file=io.StringIO(), color_system="truecolor")
|
|
|
|
console.print("foo", style="bold")
|
|
|
|
assert console.file.getvalue() == "\x1b[1mfoo\x1b[0m\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_show_cursor():
|
2020-06-20 17:39:00 +00:00
|
|
|
console = Console(file=io.StringIO(), force_terminal=True, legacy_windows=False)
|
2020-04-26 15:28:18 +00:00
|
|
|
console.show_cursor(False)
|
|
|
|
console.print("foo")
|
|
|
|
console.show_cursor(True)
|
|
|
|
assert console.file.getvalue() == "\x1b[?25lfoo\n\x1b[?25h"
|
|
|
|
|
|
|
|
|
2020-05-20 16:19:55 +00:00
|
|
|
def test_clear():
|
|
|
|
console = Console(file=io.StringIO(), force_terminal=True)
|
|
|
|
console.clear()
|
|
|
|
console.clear(home=False)
|
|
|
|
assert console.file.getvalue() == "\033[2J\033[H" + "\033[2J"
|
|
|
|
|
|
|
|
|
|
|
|
def test_clear_no_terminal():
|
|
|
|
console = Console(file=io.StringIO())
|
|
|
|
console.clear()
|
|
|
|
console.clear(home=False)
|
|
|
|
assert console.file.getvalue() == ""
|
|
|
|
|
|
|
|
|
2020-04-26 15:28:18 +00:00
|
|
|
def test_get_style():
|
|
|
|
console = Console()
|
|
|
|
console.get_style("repr.brace") == Style(bold=True)
|
|
|
|
|
|
|
|
|
2020-09-26 14:56:27 +00:00
|
|
|
def test_get_style_default():
|
|
|
|
console = Console()
|
|
|
|
console.get_style("foobar", default="red") == Style(color="red")
|
|
|
|
|
|
|
|
|
2020-04-26 15:28:18 +00:00
|
|
|
def test_get_style_error():
|
|
|
|
console = Console()
|
|
|
|
with pytest.raises(errors.MissingStyle):
|
|
|
|
console.get_style("nosuchstyle")
|
|
|
|
with pytest.raises(errors.MissingStyle):
|
|
|
|
console.get_style("foo bar")
|
2020-04-27 15:22:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_render_error():
|
|
|
|
console = Console()
|
|
|
|
with pytest.raises(errors.NotRenderableError):
|
|
|
|
list(console.render([], console.options))
|
|
|
|
|
|
|
|
|
2020-05-14 19:15:31 +00:00
|
|
|
def test_control():
|
2020-05-15 11:45:49 +00:00
|
|
|
console = Console(file=io.StringIO(), force_terminal=True)
|
2020-05-14 19:15:31 +00:00
|
|
|
console.control("FOO")
|
|
|
|
console.print("BAR")
|
|
|
|
assert console.file.getvalue() == "FOOBAR\n"
|
|
|
|
|
|
|
|
|
2020-09-23 14:53:41 +00:00
|
|
|
def test_capture():
|
|
|
|
console = Console()
|
|
|
|
with console.capture() as capture:
|
|
|
|
with pytest.raises(CaptureError):
|
|
|
|
capture.get()
|
|
|
|
console.print("Hello")
|
|
|
|
assert capture.get() == "Hello\n"
|
|
|
|
|
|
|
|
|
2020-05-15 20:47:59 +00:00
|
|
|
def test_input(monkeypatch, capsys):
|
2020-09-23 14:53:41 +00:00
|
|
|
def fake_input(prompt):
|
|
|
|
console.file.write(prompt)
|
|
|
|
return "bar"
|
|
|
|
|
|
|
|
monkeypatch.setattr("builtins.input", fake_input)
|
2020-05-15 20:47:59 +00:00
|
|
|
console = Console()
|
|
|
|
user_input = console.input(prompt="foo:")
|
|
|
|
assert capsys.readouterr().out == "foo:"
|
|
|
|
assert user_input == "bar"
|
|
|
|
|
|
|
|
|
2020-09-23 14:53:41 +00:00
|
|
|
def test_input_password(monkeypatch, capsys):
|
|
|
|
def fake_input(prompt, stream=None):
|
|
|
|
console.file.write(prompt)
|
|
|
|
return "bar"
|
|
|
|
|
|
|
|
import rich.console
|
|
|
|
|
|
|
|
monkeypatch.setattr(rich.console, "getpass", fake_input)
|
|
|
|
console = Console()
|
|
|
|
user_input = console.input(prompt="foo:", password=True)
|
|
|
|
assert capsys.readouterr().out == "foo:"
|
|
|
|
assert user_input == "bar"
|
|
|
|
|
|
|
|
|
2020-05-17 11:39:51 +00:00
|
|
|
def test_justify_none():
|
|
|
|
console = Console(file=io.StringIO(), force_terminal=True, width=20)
|
|
|
|
console.print("FOO", justify=None)
|
|
|
|
assert console.file.getvalue() == "FOO\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_justify_left():
|
|
|
|
console = Console(file=io.StringIO(), force_terminal=True, width=20)
|
|
|
|
console.print("FOO", justify="left")
|
|
|
|
assert console.file.getvalue() == "FOO \n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_justify_center():
|
|
|
|
console = Console(file=io.StringIO(), force_terminal=True, width=20)
|
|
|
|
console.print("FOO", justify="center")
|
|
|
|
assert console.file.getvalue() == " FOO \n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_justify_right():
|
|
|
|
console = Console(file=io.StringIO(), force_terminal=True, width=20)
|
|
|
|
console.print("FOO", justify="right")
|
|
|
|
assert console.file.getvalue() == " FOO\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_justify_renderable_none():
|
2020-06-20 17:29:01 +00:00
|
|
|
console = Console(
|
2020-06-20 17:39:00 +00:00
|
|
|
file=io.StringIO(), force_terminal=True, width=20, legacy_windows=False
|
2020-06-20 17:29:01 +00:00
|
|
|
)
|
2020-05-17 11:39:51 +00:00
|
|
|
console.print(Panel("FOO", expand=False), justify=None)
|
|
|
|
assert console.file.getvalue() == "╭───╮\n│FOO│\n╰───╯\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_justify_renderable_left():
|
2020-06-20 17:29:01 +00:00
|
|
|
console = Console(
|
2020-07-10 15:11:33 +00:00
|
|
|
file=io.StringIO(), force_terminal=True, width=10, legacy_windows=False
|
2020-06-20 17:29:01 +00:00
|
|
|
)
|
2020-05-17 11:39:51 +00:00
|
|
|
console.print(Panel("FOO", expand=False), justify="left")
|
2020-07-10 15:11:33 +00:00
|
|
|
assert console.file.getvalue() == "╭───╮ \n│FOO│ \n╰───╯ \n"
|
2020-05-17 11:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_justify_renderable_center():
|
2020-06-20 17:29:01 +00:00
|
|
|
console = Console(
|
2020-07-10 15:11:33 +00:00
|
|
|
file=io.StringIO(), force_terminal=True, width=10, legacy_windows=False
|
2020-06-20 17:29:01 +00:00
|
|
|
)
|
2020-05-17 11:39:51 +00:00
|
|
|
console.print(Panel("FOO", expand=False), justify="center")
|
2020-07-10 15:11:33 +00:00
|
|
|
assert console.file.getvalue() == " ╭───╮ \n │FOO│ \n ╰───╯ \n"
|
2020-05-17 11:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_justify_renderable_right():
|
2020-06-20 17:29:01 +00:00
|
|
|
console = Console(
|
2020-06-20 17:39:00 +00:00
|
|
|
file=io.StringIO(), force_terminal=True, width=20, legacy_windows=False
|
2020-06-20 17:29:01 +00:00
|
|
|
)
|
2020-05-17 11:39:51 +00:00
|
|
|
console.print(Panel("FOO", expand=False), justify="right")
|
|
|
|
assert (
|
|
|
|
console.file.getvalue()
|
|
|
|
== " ╭───╮\n │FOO│\n ╰───╯\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-27 15:22:39 +00:00
|
|
|
class BrokenRenderable:
|
2020-05-22 09:33:57 +00:00
|
|
|
def __rich_console__(self, console, options):
|
2020-04-27 15:22:39 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_render_broken_renderable():
|
|
|
|
console = Console()
|
|
|
|
broken = BrokenRenderable()
|
|
|
|
with pytest.raises(errors.NotRenderableError):
|
|
|
|
list(console.render(broken, console.options))
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_text():
|
|
|
|
console = Console(record=True, width=100)
|
|
|
|
console.print("[b]foo")
|
|
|
|
text = console.export_text()
|
|
|
|
expected = "foo\n"
|
|
|
|
assert text == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_html():
|
|
|
|
console = Console(record=True, width=100)
|
2020-05-20 16:19:55 +00:00
|
|
|
console.print("[b]foo [link=https://example.org]Click[/link]")
|
2020-04-27 15:22:39 +00:00
|
|
|
html = console.export_html()
|
2020-09-19 13:02:20 +00:00
|
|
|
expected = '<!DOCTYPE html>\n<head>\n<meta charset="UTF-8">\n<style>\n.r1 {font-weight: bold}\nbody {\n color: #000000;\n background-color: #ffffff;\n}\n</style>\n</head>\n<html>\n<body>\n <code>\n <pre style="font-family:Menlo,\'DejaVu Sans Mono\',consolas,\'Courier New\',monospace"><span class="r1">foo </span><a href="https://example.org"><span class="r1">Click</span></a>\n</pre>\n </code>\n</body>\n</html>\n'
|
2020-04-27 15:22:39 +00:00
|
|
|
assert html == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_html_inline():
|
|
|
|
console = Console(record=True, width=100)
|
2020-05-20 16:19:55 +00:00
|
|
|
console.print("[b]foo [link=https://example.org]Click[/link]")
|
2020-04-27 15:22:39 +00:00
|
|
|
html = console.export_html(inline_styles=True)
|
2020-09-19 13:02:20 +00:00
|
|
|
expected = '<!DOCTYPE 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<html>\n<body>\n <code>\n <pre style="font-family:Menlo,\'DejaVu Sans Mono\',consolas,\'Courier New\',monospace"><span style="font-weight: bold">foo </span><a href="https://example.org"><span style="font-weight: bold">Click</span></a>\n</pre>\n </code>\n</body>\n</html>\n'
|
2020-04-27 15:22:39 +00:00
|
|
|
assert html == expected
|
2020-05-14 19:15:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_save_text():
|
|
|
|
console = Console(record=True, width=100)
|
|
|
|
console.print("foo")
|
|
|
|
with tempfile.TemporaryDirectory() as path:
|
|
|
|
export_path = os.path.join(path, "rich.txt")
|
|
|
|
console.save_text(export_path)
|
|
|
|
with open(export_path, "rt") as text_file:
|
|
|
|
assert text_file.read() == "foo\n"
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_html():
|
2020-09-19 13:02:20 +00:00
|
|
|
expected = "<!DOCTYPE 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<html>\n<body>\n <code>\n <pre style=\"font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">foo\n</pre>\n </code>\n</body>\n</html>\n"
|
2020-05-14 19:15:31 +00:00
|
|
|
console = Console(record=True, width=100)
|
|
|
|
console.print("foo")
|
|
|
|
with tempfile.TemporaryDirectory() as path:
|
|
|
|
export_path = os.path.join(path, "example.html")
|
|
|
|
console.save_html(export_path)
|
|
|
|
with open(export_path, "rt") as html_file:
|
|
|
|
assert html_file.read() == expected
|
2020-06-06 12:11:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_wrap():
|
|
|
|
console = Console(width=10, file=io.StringIO())
|
2020-06-06 14:34:59 +00:00
|
|
|
console.print("foo bar baz egg", no_wrap=True)
|
|
|
|
assert console.file.getvalue() == "foo bar ba\n"
|
2020-08-24 17:37:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_soft_wrap():
|
|
|
|
console = Console(width=10, file=io.StringIO())
|
|
|
|
console.print("foo bar baz egg", soft_wrap=True)
|
|
|
|
assert console.file.getvalue() == "foo bar baz egg\n"
|
2020-08-25 09:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unicode_error() -> None:
|
|
|
|
try:
|
|
|
|
with tempfile.TemporaryFile("wt", encoding="ascii") as tmpfile:
|
|
|
|
console = Console(file=tmpfile)
|
|
|
|
console.print(":vampire:")
|
|
|
|
except UnicodeEncodeError as error:
|
|
|
|
assert "PYTHONIOENCODING" in str(error)
|
|
|
|
else:
|
|
|
|
assert False, "didn't raise UnicodeEncodeError"
|
2020-09-30 08:34:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_bell() -> None:
|
|
|
|
console = Console(force_terminal=True)
|
|
|
|
console.begin_capture()
|
|
|
|
console.bell()
|
2020-09-30 08:52:32 +00:00
|
|
|
assert console.end_capture() == "\x07"
|