Debian Codegraph @

tests/test_log.py

raw
1 # encoding=utf-8
2
3
4 import io
5 import re
6
7 from rich.console import Console
8
9 re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b")
10
11
12 def replace_link_ids(render: str) -> str:
13 """Link IDs have a random ID and system path which is a problem for
14 reproducible tests.
15
16 """
17 return re_link_ids.sub("id=0;foo\x1b", render)
18
19
20 test_data = [1, 2, 3]
21
22
23 def render_log():
24 console = Console(
25 file=io.StringIO(),
26 width=80,
27 force_terminal=True,
28 log_time_format="[TIME]",
29 color_system="truecolor",
30 legacy_windows=False,
31 )
32 console.log()
33 console.log("Hello from", console, "!")
34 console.log(test_data, log_locals=True)
35 return replace_link_ids(console.file.getvalue()).replace("test_log.py", "source.py")
36
37
38 def test_log():
39 expected = replace_link_ids(
40 "\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0m \x1b]8;id=0;foo\x1b\\\x1b[2msource.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:\x1b[0m\x1b]8;id=0;foo\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;foo\x1b\\\x1b[2msource.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:\x1b[0m\x1b]8;id=0;foo\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;foo\x1b\\\x1b[2msource.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:\x1b[0m\x1b]8;id=0;foo\x1b\\\x1b[2m34\x1b[0m\x1b]8;;\x1b\\\n\x1b[2;36m \x1b[0m\x1b[34m╭─\x1b[0m\x1b[34m─────────────────────\x1b[0m\x1b[34m \x1b[0m\x1b[3;34mlocals\x1b[0m\x1b[34m \x1b[0m\x1b[34m─────────────────────\x1b[0m\x1b[34m─╮\x1b[0m \x1b[2m \x1b[0m\n\x1b[2;36m \x1b[0m\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 \x1b[2m \x1b[0m\n\x1b[2;36m \x1b[0m\x1b[34m╰────────────────────────────────────────────────────╯\x1b[0m \x1b[2m \x1b[0m\n"
41 )
42 rendered = render_log()
43 print(repr(rendered))
44 assert rendered == expected
45
46
47 def test_log_caller_frame_info():
48 for i in range(2):
49 assert Console._caller_frame_info(i) == Console._caller_frame_info(
50 i, lambda: None
51 )
52
53
54 def test_justify():
55 console = Console(width=20, log_path=False, log_time=False, color_system=None)
56 console.begin_capture()
57 console.log("foo", justify="right")
58 result = console.end_capture()
59 assert result == " foo\n"
60
61
62 if __name__ == "__main__":
63 render = render_log()
64 print(render)
65 print(repr(render))