Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #656: add 'modules' keyword arg to load Redis extension modules #661

Merged
merged 9 commits into from
Jun 5, 2024
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ You can pick which you prefer, but remember that these settings are handled in t
- redis_datadir
- -
- ""
* - Redis test instance extension module(s) path
- modules (list of paths)
- --redis-modules (comma-separated string)
- redis_modules (comma-separated string)
- -
- ""

Example usage:

Expand Down
1 change: 1 addition & 0 deletions newsfragments/656.misc.rst
fizyk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add '--redis-modules' command line option (or 'redis_modules' in .ini file) to specify comma-separated list of Redis extension modules to load
1 change: 1 addition & 0 deletions pytest_redis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ def get_conf_option(option: str) -> Any:
"syslog": bool(get_conf_option("syslog")),
"decode": bool(get_conf_option("decode")),
"datadir": get_conf_option("datadir"),
"modules": get_conf_option("modules"),
}
return config
6 changes: 6 additions & 0 deletions pytest_redis/executor/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
syslog_enabled: bool = False,
appendonly: str = "no",
datadir: Optional[Path] = None,
modules: Optional[list[str]] = None,
fizyk marked this conversation as resolved.
Show resolved Hide resolved
) -> None: # pylint:disable=too-many-locals
"""Init method of a RedisExecutor.

Expand All @@ -79,6 +80,7 @@ def __init__(
to the system logger
:param datadir: location where all the process files will be located
:param appendonly:
:param modules: list of paths of Redis extension modules to load
"""
if not datadir:
datadir = Path(gettempdir())
Expand Down Expand Up @@ -142,6 +144,10 @@ def __init__(
else:
command.extend([f"--save {save}"])

if modules:
for module_path in modules:
command.extend(["--loadmodule", module_path])

super().__init__(command, host, port, timeout=startup_timeout)

@classmethod
Expand Down
10 changes: 10 additions & 0 deletions pytest_redis/factories/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def redis_proc(
syslog: Optional[bool] = None,
loglevel: Optional[str] = None,
datadir: Optional[str] = None,
modules: Optional[list[str]] = None,
) -> Callable[[FixtureRequest, TempPathFactory], Generator[RedisExecutor, None, None]]:
"""Fixture factory for pytest-redis.

Expand All @@ -62,6 +63,7 @@ def redis_proc(
:param datadir: Path for redis data files, including the unix domain socket.
If this is not configured, then a temporary directory is created and used
instead.
:param modules: list of paths of Redis extension modules to load
:returns: function which makes a redis process
"""

Expand Down Expand Up @@ -92,6 +94,13 @@ def redis_proc_fixture(
else:
redis_datadir = tmp_path_factory.mktemp(f"pytest-redis-{request.fixturename}")

if modules:
redis_modules = modules
elif config.get("modules"):
redis_modules = config["modules"].split(",")
mguijarr marked this conversation as resolved.
Show resolved Hide resolved
else:
redis_modules = []

redis_port = get_port(port) or get_port(config["port"])
assert redis_port
redis_executor = RedisExecutor(
Expand All @@ -109,6 +118,7 @@ def redis_proc_fixture(
password=password or config["password"],
startup_timeout=60,
datadir=redis_datadir,
modules=redis_modules,
)
with redis_executor:
yield redis_executor
Expand Down
3 changes: 3 additions & 0 deletions pytest_redis/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"Client: to decode response or not. " "See redis.StrictRedis decode_reponse client parameter."
)
_help_datadir = "Directory where test Redis instance data files will be stored"
_help_modules = "Comma separated list of paths to Redis extension modules to be loaded at startup"


def pytest_addoption(parser: Parser) -> None:
Expand Down Expand Up @@ -87,6 +88,7 @@ def pytest_addoption(parser: Parser) -> None:
parser.addini(name="redis_syslog", type="bool", help=_help_syslog)
parser.addini(name="redis_decode", type="bool", help=_help_decode, default=False)
parser.addini(name="redis_datadir", help=_help_datadir, default=None)
parser.addini(name="redis_modules", help=_help_modules, default=None)

parser.addoption(
"--redis-exec",
Expand Down Expand Up @@ -118,6 +120,7 @@ def pytest_addoption(parser: Parser) -> None:
"--redis-client-decode", action="store_true", dest="redis_decode", help=_help_decode
)
parser.addoption("--redis-datadir", action="store", dest="redis_datadir", help=_help_datadir)
parser.addoption("--redis-modules", action="store", dest="redis_modules", help=_help_modules)


redis_proc = pytest_redis.factories.proc.redis_proc()
Expand Down
Loading