Skip to content

Commit

Permalink
Upgrade test fixture pip in venv without upgrade_deps
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliahKagan committed Mar 11, 2024
1 parent dd8ee4f commit a262a06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions test/lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import logging
import os
import os.path as osp
import subprocess
import sys
import tempfile
import textwrap
import time
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/test_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a262a06

Please sign in to comment.