Coverage for conftest.py: 78%
37 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-05 00:36 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-05 00:36 +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
38 matplotlib.rcParams['backend'] = 'agg'
39 if is_master(config) or dict(getattr(config, 'workerinput', {}))['workerid'] == 'gw0':
40 # Only warn once.
41 warn(UserWarning('DISPLAY environment variable is set, which can cause problems in some setups (e.g. WSL). '
42 + f'Adjusting matplotlib backend to "{matplotlib.rcParams["backend"]}" to compensate.'))
43 except ImportError:
44 pass
46 # Create a temporary directory for sharing files between master and worker nodes.
47 if is_master(config):
48 # Add it to the config so that it can passed to the worker nodes.
49 setattr(config, "shared_temp_dir", mkdtemp())
52def pytest_configure_node(node: WorkerController) -> None:
53 """
54 Xdist hook used to inform workers of the location of the shared temp dir.
55 """
56 workerinput: dict = getattr(node, "workerinput")
57 workerinput["shared_temp_dir"] = getattr(node.config, "shared_temp_dir")
60@pytest.fixture(scope="session")
61def shared_temp_dir(request: pytest.FixtureRequest) -> str:
62 """
63 Returns a unique and temporary directory which can be shared by
64 master or worker nodes in xdist runs.
65 """
66 if is_master(request.config):
67 return str(getattr(request.config, "shared_temp_dir"))
68 else:
69 workerinput: dict = getattr(request.config, "workerinput")
70 return str(workerinput["shared_temp_dir"])
73def pytest_unconfigure(config: pytest.Config) -> None:
74 """
75 Called after all tests have completed.
76 """
77 if is_master(config):
78 shared_tmp_dir = getattr(config, "shared_temp_dir", None)
79 if shared_tmp_dir:
80 shutil.rmtree(str(shared_tmp_dir))
83@pytest.fixture(scope="session")
84def short_testrun_uid(testrun_uid: str) -> str:
85 """
86 Shorten the unique test run id that xdist provides so we can use it with
87 other systems (e.g., docker).
88 """
89 return testrun_uid[0:8]