|
5 | 5 | import site |
6 | 6 | import sys |
7 | 7 | import sysconfig |
| 8 | +from functools import partial |
8 | 9 | from pathlib import Path |
| 10 | +from shutil import rmtree |
| 11 | +from tempfile import mkdtemp |
9 | 12 | from typing import TYPE_CHECKING, Protocol |
10 | 13 | from unittest.mock import patch |
11 | 14 | from uuid import uuid4 |
@@ -142,15 +145,17 @@ def get_python(self: VirtualEnv, base_python: list[str]) -> PythonInfo | None: |
142 | 145 | return _func |
143 | 146 |
|
144 | 147 |
|
145 | | -@pytest.fixture(scope="session", autouse=True) |
146 | | -def _do_not_share_virtualenv_for_parallel_runs(tmp_path_factory: pytest.TempPathFactory, worker_id: str) -> None: |
147 | | - # virtualenv uses locks to manage access to its cache, when running with xdist this may throw off test timings |
148 | | - if worker_id != "master": # pragma: no branch |
149 | | - temp_app_data = str(tmp_path_factory.mktemp(f"virtualenv-app-{worker_id}")) # pragma: no cover |
150 | | - os.environ["VIRTUALENV_APP_DATA"] = temp_app_data # pragma: no cover |
151 | | - seed_env_folder = str(tmp_path_factory.mktemp(f"seed-cache-{worker_id}")) # pragma: no cover |
152 | | - args = [seed_env_folder, "--without-pip", "--activators", ""] # pragma: no cover |
153 | | - cli_run(args, setup_logging=False) # pragma: no cover |
| 148 | +def pytest_configure(config: pytest.Config) -> None: |
| 149 | + if (worker_id := os.environ.get("PYTEST_XDIST_WORKER")) is not None: # pragma: no cover |
| 150 | + # virtualenv locks its app data, so sharing one across xdist workers throws off test timings |
| 151 | + app_data = mkdtemp(prefix=f"virtualenv-app-{worker_id}-") |
| 152 | + os.environ["VIRTUALENV_APP_DATA"] = app_data |
| 153 | + config.add_cleanup(partial(rmtree, app_data, ignore_errors=True)) |
| 154 | + seed_env = mkdtemp(prefix="virtualenv-seed-") |
| 155 | + config.add_cleanup(partial(rmtree, seed_env, ignore_errors=True)) |
| 156 | + # seeding builds the pip wheel image, a compileall over pip's tree that is slow on Windows CI; pay it here so it |
| 157 | + # does not land inside the timeout of whichever test happens to create the first environment |
| 158 | + cli_run([seed_env, "--activators", ""], setup_logging=False) |
154 | 159 |
|
155 | 160 |
|
156 | 161 | @pytest.fixture(scope="session") |
|
0 commit comments