GH-80789: Get rid of the `ensurepip` infra for many wheels (#109245)

Co-authored-by: vstinner@python.org
Co-authored-by: Pradyun Gedam <pradyunsg@gmail.com>
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
This commit is contained in:
Sviatoslav Sydorenko (Святослав Сидоренко) 2024-01-30 02:25:31 +01:00 committed by GitHub
parent 742ba6081c
commit 963904335e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 69 additions and 104 deletions

View file

@ -6,6 +6,8 @@ import tempfile
import test.support
import unittest
import unittest.mock
from importlib.resources.abc import Traversable
from pathlib import Path
import ensurepip
import ensurepip._uninstall
@ -20,41 +22,35 @@ class TestPackages(unittest.TestCase):
# Test version()
with tempfile.TemporaryDirectory() as tmpdir:
self.touch(tmpdir, "pip-1.2.3b1-py2.py3-none-any.whl")
with (unittest.mock.patch.object(ensurepip, '_PACKAGES', None),
unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', tmpdir)):
with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', Path(tmpdir)):
self.assertEqual(ensurepip.version(), '1.2.3b1')
def test_get_packages_no_dir(self):
# Test _get_packages() without a wheel package directory
with (unittest.mock.patch.object(ensurepip, '_PACKAGES', None),
unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None)):
packages = ensurepip._get_packages()
# when bundled wheel packages are used, we get _PIP_VERSION
def test_version_no_dir(self):
# Test version() without a wheel package directory
with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
# when the bundled pip wheel is used, we get _PIP_VERSION
self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
# use bundled wheel packages
self.assertIsNotNone(packages['pip'].wheel_name)
def test_selected_wheel_path_no_dir(self):
pip_filename = f'pip-{ensurepip._PIP_VERSION}-py3-none-any.whl'
with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
with ensurepip._get_pip_whl_path_ctx() as bundled_wheel_path:
self.assertEqual(pip_filename, bundled_wheel_path.name)
def test_get_packages_with_dir(self):
# Test _get_packages() with a wheel package directory
def test_selected_wheel_path_with_dir(self):
# Test _get_pip_whl_path_ctx() with a wheel package directory
pip_filename = "pip-20.2.2-py2.py3-none-any.whl"
with tempfile.TemporaryDirectory() as tmpdir:
self.touch(tmpdir, pip_filename)
# not used, make sure that it's ignored
# not used, make sure that they're ignored
self.touch(tmpdir, "pip-1.2.3-py2.py3-none-any.whl")
self.touch(tmpdir, "wheel-0.34.2-py2.py3-none-any.whl")
self.touch(tmpdir, "pip-script.py")
with (unittest.mock.patch.object(ensurepip, '_PACKAGES', None),
unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', tmpdir)):
packages = ensurepip._get_packages()
self.assertEqual(packages['pip'].version, '20.2.2')
self.assertEqual(packages['pip'].wheel_path,
os.path.join(tmpdir, pip_filename))
# wheel package is ignored
self.assertEqual(sorted(packages), ['pip'])
with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', Path(tmpdir)):
with ensurepip._get_pip_whl_path_ctx() as bundled_wheel_path:
self.assertEqual(pip_filename, bundled_wheel_path.name)
class EnsurepipMixin:
@ -69,7 +65,7 @@ class EnsurepipMixin:
real_devnull = os.devnull
os_patch = unittest.mock.patch("ensurepip.os")
patched_os = os_patch.start()
# But expose os.listdir() used by _find_packages()
# But expose os.listdir() used by _find_wheel_pkg_dir_pip()
patched_os.listdir = os.listdir
self.addCleanup(os_patch.stop)
patched_os.devnull = real_devnull