Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit 8e98c8d

Browse files
committed
Utility fixtures for pytest runner
Add fixtures for deriving system test name from the directory name and for a module-specific logger. Add fixtures to execute shell and perl scripts. Similar to how run.sh calls commands, this functionality is also needed in pytest. The fixtures take care of switching to a proper directory, logging everything and handling errors. Note that the fixture system_test_dir is not defined yet. It is omitted now for review readability and it's added in follow-up commits.
1 parent 0b42f21 commit 8e98c8d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

bin/tests/system/conftest.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ def control_port():
5151
# don't branch the code.
5252

5353
if os.getenv("LEGACY_TEST_RUNNER", "0") == "0":
54+
from functools import partial
5455
import logging
5556
from pathlib import Path
5657
import re
5758
import subprocess
5859
import time
60+
from typing import List, Optional
5961

6062
# Silence warnings caused by passing a pytest fixture to another fixture.
6163
# pylint: disable=redefined-outer-name
@@ -203,3 +205,69 @@ def env(ports):
203205
env["builddir"] = f"{env['TOP_BUILDDIR']}/bin/tests/system"
204206
env["srcdir"] = f"{env['TOP_SRCDIR']}/bin/tests/system"
205207
return env
208+
209+
@pytest.fixture(scope="module")
210+
def system_test_name(request):
211+
"""Name of the system test directory."""
212+
path = Path(request.fspath)
213+
return path.parent.name
214+
215+
@pytest.fixture(scope="module")
216+
def logger(system_test_name):
217+
"""Logging facility specific to this test."""
218+
return logging.getLogger(system_test_name)
219+
220+
def _run_script( # pylint: disable=too-many-arguments
221+
env,
222+
logger,
223+
system_test_dir: Path,
224+
interpreter: str,
225+
script: str,
226+
args: Optional[List[str]] = None,
227+
):
228+
"""Helper function for the shell / perl script invocations (through fixtures below)."""
229+
if args is None:
230+
args = []
231+
path = Path(script)
232+
if not path.is_absolute():
233+
# make sure relative paths are always relative to system_dir
234+
path = system_test_dir.parent / path
235+
script = str(path)
236+
cwd = os.getcwd()
237+
if not path.exists():
238+
raise FileNotFoundError(f"script {script} not found in {cwd}")
239+
logger.debug("running script: %s %s %s", interpreter, script, " ".join(args))
240+
logger.debug(" workdir: %s", cwd)
241+
stdout = b""
242+
returncode = 1
243+
try:
244+
proc = subprocess.run(
245+
[interpreter, script] + args,
246+
env=env,
247+
check=True,
248+
stdout=subprocess.PIPE,
249+
stderr=subprocess.STDOUT,
250+
)
251+
except subprocess.CalledProcessError as exc:
252+
stdout = exc.stdout
253+
returncode = exc.returncode
254+
raise exc
255+
else:
256+
stdout = proc.stdout
257+
returncode = proc.returncode
258+
finally:
259+
if stdout:
260+
for line in stdout.decode().splitlines():
261+
logger.debug(" %s", line)
262+
logger.debug(" exited with %d", returncode)
263+
return proc
264+
265+
@pytest.fixture(scope="module")
266+
def shell(env, system_test_dir, logger):
267+
"""Function to call a shell script with arguments."""
268+
return partial(_run_script, env, logger, system_test_dir, env["SHELL"])
269+
270+
@pytest.fixture(scope="module")
271+
def perl(env, system_test_dir, logger):
272+
"""Function to call a perl script with arguments."""
273+
return partial(_run_script, env, logger, system_test_dir, env["PERL"])

0 commit comments

Comments
 (0)