From dd8ee4fabb8ae7921cf53b95b195f4d2186f136f Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Mon, 11 Mar 2024 12:06:18 -0400 Subject: [PATCH 1/2] Start fixing venv test fixture pip toml bug This is not yet a usable fix, because venv.create only supports upgrade_deps on Python 3.9 and higher. --- test/lib/helper.py | 6 +++--- test/test_index.py | 2 +- test/test_installation.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/lib/helper.py b/test/lib/helper.py index 26469ed5d..cf7b37b08 100644 --- a/test/lib/helper.py +++ b/test/lib/helper.py @@ -403,13 +403,13 @@ class VirtualEnvironment: __slots__ = ("_env_dir",) - def __init__(self, env_dir, *, with_pip): + def __init__(self, env_dir, *, need_pip): if os.name == "nt": self._env_dir = osp.realpath(env_dir) - venv.create(self.env_dir, symlinks=False, with_pip=with_pip) + venv.create(self.env_dir, symlinks=False, with_pip=need_pip, upgrade_deps=need_pip) else: self._env_dir = env_dir - venv.create(self.env_dir, symlinks=True, with_pip=with_pip) + venv.create(self.env_dir, symlinks=True, with_pip=need_pip, upgrade_deps=need_pip) @property def env_dir(self): diff --git a/test/test_index.py b/test/test_index.py index fa64b82a2..206023bfe 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -1060,7 +1060,7 @@ def test_hook_uses_shell_not_from_cwd(self, rw_dir, case): # from a venv may not run when copied outside of it, and a global interpreter # won't run when copied to a different location if it was installed from the # Microsoft Store. So we make a new venv in rw_dir and use its interpreter. - venv = VirtualEnvironment(rw_dir, with_pip=False) + venv = VirtualEnvironment(rw_dir, need_pip=False) shutil.copy(venv.python, Path(rw_dir, shell_name)) shutil.copy(fixture_path("polyglot"), hook_path("polyglot", repo.git_dir)) payload = Path(rw_dir, "payload.txt") diff --git a/test/test_installation.py b/test/test_installation.py index ae6472e98..38c0c45b6 100644 --- a/test/test_installation.py +++ b/test/test_installation.py @@ -64,7 +64,7 @@ def test_installation(self, rw_dir): @staticmethod def _set_up_venv(rw_dir): - venv = VirtualEnvironment(rw_dir, with_pip=True) + venv = VirtualEnvironment(rw_dir, need_pip=True) os.symlink( os.path.dirname(os.path.dirname(__file__)), venv.sources, From a262a06634ccb616f061fdae7cf67b00a92f0ba6 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Mon, 11 Mar 2024 13:16:15 -0400 Subject: [PATCH 2/2] Upgrade test fixture pip in venv without upgrade_deps Because the upgrade_deps parameter to venv.create, as well as related functionality such as the EnvBuilder.upgrade_dependencies method that it uses, are only available starting in Python 3.9. This also puts the name of the VirtualEnvironment.__init__ parameter for setting up pip in the test fixture virtual environment back from need_pip to with_pip, which may be more intuitive. --- test/lib/helper.py | 15 ++++++++++++--- test/test_index.py | 2 +- test/test_installation.py | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/lib/helper.py b/test/lib/helper.py index cf7b37b08..27586c2b0 100644 --- a/test/lib/helper.py +++ b/test/lib/helper.py @@ -10,6 +10,8 @@ import logging import os import os.path as osp +import subprocess +import sys import tempfile import textwrap import time @@ -403,13 +405,20 @@ class VirtualEnvironment: __slots__ = ("_env_dir",) - def __init__(self, env_dir, *, need_pip): + def __init__(self, env_dir, *, with_pip): if os.name == "nt": self._env_dir = osp.realpath(env_dir) - venv.create(self.env_dir, symlinks=False, with_pip=need_pip, upgrade_deps=need_pip) + venv.create(self.env_dir, symlinks=False, with_pip=with_pip) else: self._env_dir = env_dir - venv.create(self.env_dir, symlinks=True, with_pip=need_pip, upgrade_deps=need_pip) + venv.create(self.env_dir, symlinks=True, with_pip=with_pip) + + if with_pip: + # The upgrade_deps parameter to venv.create is 3.9+ only, so do it this way. + command = [self.python, "-m", "pip", "install", "--upgrade", "pip"] + if sys.version_info < (3, 12): + command.append("setuptools") + subprocess.check_output(command) @property def env_dir(self): diff --git a/test/test_index.py b/test/test_index.py index 206023bfe..fa64b82a2 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -1060,7 +1060,7 @@ def test_hook_uses_shell_not_from_cwd(self, rw_dir, case): # from a venv may not run when copied outside of it, and a global interpreter # won't run when copied to a different location if it was installed from the # Microsoft Store. So we make a new venv in rw_dir and use its interpreter. - venv = VirtualEnvironment(rw_dir, need_pip=False) + venv = VirtualEnvironment(rw_dir, with_pip=False) shutil.copy(venv.python, Path(rw_dir, shell_name)) shutil.copy(fixture_path("polyglot"), hook_path("polyglot", repo.git_dir)) payload = Path(rw_dir, "payload.txt") diff --git a/test/test_installation.py b/test/test_installation.py index 38c0c45b6..ae6472e98 100644 --- a/test/test_installation.py +++ b/test/test_installation.py @@ -64,7 +64,7 @@ def test_installation(self, rw_dir): @staticmethod def _set_up_venv(rw_dir): - venv = VirtualEnvironment(rw_dir, need_pip=True) + venv = VirtualEnvironment(rw_dir, with_pip=True) os.symlink( os.path.dirname(os.path.dirname(__file__)), venv.sources,