Coverage for conftest.py: 78%
36 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Provides some pytest configuration overrides for both modules.
7"""
9# Note: This file is named conftest.py so that pytest picks it up automatically
10# without the need to adjust PYTHONPATH or sys.path as much.
12from warnings import warn
13from tempfile import mkdtemp
15import os
16import shutil
18import pytest
19from xdist.workermanage import WorkerController
22def is_master(config: pytest.Config) -> bool:
23 """
24 True if the code running the given pytest.config object is running in a
25 xdist master node or not running xdist at all.
26 """
27 return not hasattr(config, "workerinput")
30def pytest_configure(config: pytest.Config) -> None:
31 """
32 Add some additional (global) configuration steps for pytest.
33 """
34 # Workaround some issues loading emukit in certain environments.
35 if os.environ.get("DISPLAY", None):
36 try:
37 import matplotlib # pylint: disable=import-outside-toplevel
39 matplotlib.rcParams["backend"] = "agg"
40 if is_master(config) or dict(getattr(config, "workerinput", {}))["workerid"] == "gw0":
41 # Only warn once.
42 warn(
43 UserWarning(
44 (
45 "DISPLAY environment variable is set, "
46 "which can cause problems in some setups (e.g. WSL). "
47 f"Adjusting matplotlib backend to '{matplotlib.rcParams['backend']}' "
48 "to compensate."
49 )
50 )
51 )
52 except ImportError:
53 pass
55 # Create a temporary directory for sharing files between master and worker nodes.
56 if is_master(config):
57 # Add it to the config so that it can passed to the worker nodes.
58 setattr(config, "shared_temp_dir", mkdtemp())
61def pytest_configure_node(node: WorkerController) -> None:
62 """
63 Xdist hook used to inform workers of the location of the shared temp dir.
64 """
65 workerinput: dict = getattr(node, "workerinput")
66 workerinput["shared_temp_dir"] = getattr(node.config, "shared_temp_dir")
69@pytest.fixture(scope="session")
70def shared_temp_dir(request: pytest.FixtureRequest) -> str:
71 """
72 Returns a unique and temporary directory which can be shared by
73 master or worker nodes in xdist runs.
74 """
75 if is_master(request.config):
76 return str(getattr(request.config, "shared_temp_dir"))
77 else:
78 workerinput: dict = getattr(request.config, "workerinput")
79 return str(workerinput["shared_temp_dir"])
82def pytest_unconfigure(config: pytest.Config) -> None:
83 """
84 Called after all tests have completed.
85 """
86 if is_master(config):
87 shared_tmp_dir = getattr(config, "shared_temp_dir", None)
88 if shared_tmp_dir:
89 shutil.rmtree(str(shared_tmp_dir))
92@pytest.fixture(scope="session")
93def short_testrun_uid(testrun_uid: str) -> str:
94 """
95 Shorten the unique test run id that xdist provides so we can use it with
96 other systems (e.g., docker).
97 """
98 return testrun_uid[0:8]