2020-06-15 10:51:52 +00:00
|
|
|
# encoding=utf-8
|
|
|
|
|
|
|
|
|
2020-04-26 11:25:53 +00:00
|
|
|
import io
|
2020-10-12 15:55:52 +00:00
|
|
|
import re
|
2020-04-26 11:25:53 +00:00
|
|
|
|
|
|
|
from rich.console import Console
|
|
|
|
|
2021-11-06 19:12:23 +00:00
|
|
|
re_link_ids = re.compile(r"id=((\d+);(file:///(\w+\/)+(\w+\.py)(#\d+)?))\x1b")
|
2020-10-12 15:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def replace_link_ids(render: str) -> str:
|
|
|
|
"""Link IDs have a random ID and system path which is a problem for
|
|
|
|
reproducible tests.
|
|
|
|
|
|
|
|
"""
|
2021-11-06 19:12:23 +00:00
|
|
|
return re_link_ids.sub("id=0;file:///path/to/source.py#00\x1b", render)
|
2020-04-26 11:25:53 +00:00
|
|
|
|
|
|
|
|
2020-04-26 11:54:51 +00:00
|
|
|
test_data = [1, 2, 3]
|
2020-04-26 11:25:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def render_log():
|
|
|
|
console = Console(
|
2020-04-26 11:43:50 +00:00
|
|
|
file=io.StringIO(),
|
|
|
|
width=80,
|
|
|
|
force_terminal=True,
|
2020-05-13 09:06:11 +00:00
|
|
|
log_time_format="[TIME]",
|
2020-04-26 11:43:50 +00:00
|
|
|
color_system="truecolor",
|
2020-06-20 17:39:00 +00:00
|
|
|
legacy_windows=False,
|
2020-04-26 11:25:53 +00:00
|
|
|
)
|
|
|
|
console.log()
|
|
|
|
console.log("Hello from", console, "!")
|
|
|
|
console.log(test_data, log_locals=True)
|
2021-11-07 15:02:39 +00:00
|
|
|
return replace_link_ids(console.file.getvalue()).replace("test_log.py", "source.py")
|
2020-04-26 11:25:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_log():
|
2021-11-07 15:02:39 +00:00
|
|
|
expected = replace_link_ids(
|
|
|
|
"\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0m \x1b]8;id=0;file:///path/to/foo.py#00\x1b\\\x1b[2msource.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:\x1b[0m\x1b]8;id=0;file:///path/to/foo.py#00\x1b\\\x1b[2m32\x1b[0m\x1b]8;;\x1b\\\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;95mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;36m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b]8;id=0;file:///path/to/foo.py#00\x1b\\\x1b[2msource.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:\x1b[0m\x1b]8;id=0;file:///path/to/foo.py#00\x1b\\\x1b[2m33\x1b[0m\x1b]8;;\x1b\\\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;36m1\x1b[0m, \x1b[1;36m2\x1b[0m, \x1b[1;36m3\x1b[0m\x1b[1m]\x1b[0m \x1b]8;id=0;file:///path/to/foo.py#00\x1b\\\x1b[2msource.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:\x1b[0m\x1b]8;id=0;file:///path/to/foo.py#00\x1b\\\x1b[2m34\x1b[0m\x1b]8;;\x1b\\\n \x1b[34m╭─\x1b[0m\x1b[34m───────────────────── \x1b[0m\x1b[3;34mlocals\x1b[0m\x1b[34m ─────────────────────\x1b[0m\x1b[34m─╮\x1b[0m \n \x1b[34m│\x1b[0m \x1b[3;33mconsole\x1b[0m\x1b[31m =\x1b[0m \x1b[1m<\x1b[0m\x1b[1;95mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;36m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m \x1b[34m│\x1b[0m \n \x1b[34m╰────────────────────────────────────────────────────╯\x1b[0m \n"
|
|
|
|
)
|
2020-10-03 11:06:19 +00:00
|
|
|
rendered = render_log()
|
|
|
|
print(repr(rendered))
|
|
|
|
assert rendered == expected
|
2020-04-26 11:25:53 +00:00
|
|
|
|
|
|
|
|
2021-05-23 17:23:22 +00:00
|
|
|
def test_log_caller_frame_info():
|
|
|
|
for i in range(2):
|
|
|
|
assert Console._caller_frame_info(i) == Console._caller_frame_info(
|
|
|
|
i, lambda: None
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-01 17:03:26 +00:00
|
|
|
def test_justify():
|
|
|
|
console = Console(width=20, log_path=False, log_time=False, color_system=None)
|
|
|
|
console.begin_capture()
|
|
|
|
console.log("foo", justify="right")
|
|
|
|
result = console.end_capture()
|
|
|
|
assert result == " foo\n"
|
|
|
|
|
|
|
|
|
2020-04-26 11:25:53 +00:00
|
|
|
if __name__ == "__main__":
|
2020-06-06 11:08:07 +00:00
|
|
|
render = render_log()
|
|
|
|
print(render)
|
|
|
|
print(repr(render))
|