chore: rework virtual environments creation for tests (#2356)

As mentioned in https://github.com/pypa/cibuildwheel/pull/2347#discussion_r2029821701

Rework the way test venvs are created to reuse the same mechanisms as build venvs.
This commit is contained in:
Matthieu Darbois
2025-04-14 17:20:08 -04:00
committed by GitHub
parent 1fcca39f6d
commit e9fae06abe
3 changed files with 44 additions and 103 deletions
+10 -43
View File
@@ -265,21 +265,6 @@ def setup_python(
env["PYTHON_ARCH"] = python_configuration.arch
env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
# upgrade pip to the version matching our constraints
# if necessary, reinstall it to ensure that it's available on PATH as 'pip.exe'
if not use_uv:
call(
"python",
"-m",
"pip",
"install",
"--upgrade",
"pip",
*constraint_flags(dependency_constraint),
env=env,
cwd=venv_path,
)
# update env with results from CIBW_ENVIRONMENT
env = environment.as_dictionary(prev_environment=env)
@@ -378,8 +363,7 @@ def build(options: Options, tmp_path: Path) -> None:
build_options.environment,
build_frontend.name,
)
if not use_uv:
pip_version = get_pip_version(env)
pip_version = None if use_uv else get_pip_version(env)
compatible_wheel = find_compatible_wheel(built_wheels, config.identifier)
if compatible_wheel:
@@ -407,7 +391,7 @@ def build(options: Options, tmp_path: Path) -> None:
)
build_env = env.copy()
if not use_uv:
if pip_version is not None:
build_env["VIRTUALENV_PIP"] = pip_version
if constraints_path:
@@ -486,33 +470,16 @@ def build(options: Options, tmp_path: Path) -> None:
log.step("Testing wheel...")
# set up a virtual environment to install and test from, to make sure
# there are no dependencies that were pulled in at build time.
if not use_uv:
call(
"pip", "install", "virtualenv", *constraint_flags(constraints_path), env=env
)
venv_dir = identifier_tmp_dir / "venv-test"
if use_uv:
call("uv", "venv", venv_dir, f"--python={base_python}", env=env)
else:
# Use pip version from the initial env to ensure determinism
venv_args = [
"--no-periodic-update",
f"--pip={pip_version}",
"--no-setuptools",
"--no-wheel",
]
call("python", "-m", "virtualenv", *venv_args, venv_dir, env=env)
virtualenv_env = env.copy()
virtualenv_env["PATH"] = os.pathsep.join(
[
str(venv_dir / "Scripts"),
virtualenv_env["PATH"],
]
virtualenv_env = virtualenv(
config.version,
base_python,
venv_dir,
None,
use_uv=use_uv,
env=env,
pip_version=pip_version,
)
virtualenv_env["VIRTUAL_ENV"] = str(venv_dir)
# check that we are using the Python from the virtual environment
call("where", "python", env=virtualenv_env)