summaryrefslogtreecommitdiffstats
path: root/tests/test_log.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-14 20:18:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-14 20:18:28 +0000
commitf8363b456f1ab31ee56abad579b215af195093d5 (patch)
treeb1500c675c2e0a55fb75721a854e1510acf7c862 /tests/test_log.py
parentInitial commit. (diff)
downloadrich-upstream.tar.xz
rich-upstream.zip
Adding upstream version 9.11.0.upstream/9.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/test_log.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/test_log.py b/tests/test_log.py
new file mode 100644
index 0000000..82dbfe2
--- /dev/null
+++ b/tests/test_log.py
@@ -0,0 +1,59 @@
+# encoding=utf-8
+
+
+import io
+import re
+
+from rich.console import Console
+
+
+re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b")
+
+
+def replace_link_ids(render: str) -> str:
+ """Link IDs have a random ID and system path which is a problem for
+ reproducible tests.
+
+ """
+ return re_link_ids.sub("id=0;foo\x1b", render)
+
+
+test_data = [1, 2, 3]
+
+
+def render_log():
+ console = Console(
+ file=io.StringIO(),
+ width=80,
+ force_terminal=True,
+ log_time_format="[TIME]",
+ color_system="truecolor",
+ legacy_windows=False,
+ )
+ console.log()
+ console.log("Hello from", console, "!")
+ console.log(test_data, log_locals=True)
+ return replace_link_ids(console.file.getvalue())
+
+
+def test_log():
+ expected = replace_link_ids(
+ "\n\x1b[2;36m[TIME]\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;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:34\x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:35\x1b[0m\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;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m \x1b[34m│\x1b[0m \n \x1b[34m╰────────────────────────────────────────────────────╯\x1b[0m \n"
+ )
+ rendered = render_log()
+ print(repr(rendered))
+ assert rendered == expected
+
+
+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"
+
+
+if __name__ == "__main__":
+ render = render_log()
+ print(render)
+ print(repr(render))