Debian Codegraph @

tests/cli_tests/test_pager.py

raw
1 # noqa: F541
2 from contextlib import contextmanager
3 import os
4 import pathlib
5 from textwrap import dedent
6 from packaging.version import parse as version_parse
7
8 import pexpect
9
10 TEST_IREDISRC = "/tmp/.iredisrc.test"
11 TEST_PAGER_BOUNDARY = "---boundary---"
12 TEST_PAGER_BOUNDARY_NUMBER = "---88938347271---"
13
14 # Use shell scripts as pager instead of python command with arguments
15 # because click.echo_via_pager uses shutil.which() which doesn't work
16 # with commands that have arguments
17 env_pager = os.path.join(pathlib.Path(__file__).parent, "pager_boundary.sh")
18 env_pager_numbers = os.path.join(
19 pathlib.Path(__file__).parent, "pager_boundary_number.sh"
20 )
21
22 long_list_type = "quicklist"
23 if version_parse(os.environ["REDIS_VERSION"]) >= version_parse("7"):
24 long_list_type = "listpack"
25
26
27 @contextmanager
28 def pager_enabled_cli():
29 env = os.environ
30 env["PAGER"] = env_pager
31 child = pexpect.spawn("iredis -n 15", timeout=3, env=env)
32 child.logfile_read = open("cli_test.log", "ab")
33 child.expect("127.0.0.1")
34 try:
35 yield child
36 finally:
37 child.close()
38
39
40 def test_using_pager_when_rows_too_high(clean_redis):
41 for index in range(100):
42 clean_redis.lpush("long-list", f"value-{index}")
43 with pager_enabled_cli() as child:
44 child.sendline("lrange long-list 0 -1")
45 child.expect(TEST_PAGER_BOUNDARY)
46 child.expect("value-1")
47 child.expect(TEST_PAGER_BOUNDARY)
48
49
50 def test_using_pager_works_for_help():
51 with pager_enabled_cli() as child:
52 child.sendline("help set")
53 child.expect(TEST_PAGER_BOUNDARY)
54 child.expect("Sets the string value of a key")
55 child.expect(TEST_PAGER_BOUNDARY)
56
57
58 def test_pager_works_for_peek(clean_redis):
59 for index in range(100):
60 clean_redis.lpush("long-list", f"value-{index}")
61 with pager_enabled_cli() as child:
62 child.sendline("peek long-list")
63 child.expect(TEST_PAGER_BOUNDARY)
64 child.expect(f"({long_list_type})")
65 child.expect("value-1")
66 child.expect(TEST_PAGER_BOUNDARY)
67
68
69 def test_using_pager_from_config(clean_redis):
70 config_content = dedent(
71 f"""
72 [main]
73 log_location = /tmp/iredis1.log
74 pager = {env_pager_numbers}
75 """
76 )
77
78 with open(TEST_IREDISRC, "w+") as test_iredisrc:
79 test_iredisrc.write(config_content)
80
81 child = pexpect.spawn(f"iredis -n 15 --iredisrc {TEST_IREDISRC}", timeout=3)
82 child.logfile_read = open("cli_test.log", "ab")
83 child.expect("127.0.0.1")
84 for index in range(100):
85 clean_redis.lpush("long-list", f"value-{index}")
86 child.sendline("lrange long-list 0 -1")
87 child.expect(TEST_PAGER_BOUNDARY_NUMBER)
88 child.expect("value-1")
89 child.expect(TEST_PAGER_BOUNDARY_NUMBER)
90 child.close()
91
92
93 def test_using_pager_from_config_when_env_config_both_set(clean_redis):
94 config_content = dedent(
95 f"""
96 [main]
97 log_location = /tmp/iredis1.log
98 pager = {env_pager_numbers}
99 """
100 )
101
102 with open(TEST_IREDISRC, "w+") as test_iredisrc:
103 test_iredisrc.write(config_content)
104
105 env = os.environ
106 env["PAGER"] = env_pager
107 child = pexpect.spawn(
108 f"iredis -n 15 --iredisrc {TEST_IREDISRC}", timeout=3, env=env
109 )
110 child.logfile_read = open("cli_test.log", "ab")
111 child.expect("127.0.0.1")
112 for index in range(100):
113 clean_redis.lpush("long-list", f"value-{index}")
114 child.sendline("lrange long-list 0 -1")
115 child.expect(TEST_PAGER_BOUNDARY_NUMBER)
116 child.expect("value-1")
117 child.expect(TEST_PAGER_BOUNDARY_NUMBER)
118 child.close()