Move away from soon-to-be-removed PipInstaller

This commit is contained in:
Obeida Shamoun 2023-05-04 13:05:02 +02:00
parent a94933e7ef
commit a489fe2c53
No known key found for this signature in database
GPG Key ID: C393AE9D78004C18
4 changed files with 21 additions and 18 deletions

View File

@ -1,13 +1,14 @@
# pylint: disable=missing-module-docstring, missing-function-docstring, unused-argument, too-few-public-methods
import time
from pathlib import Path
from typing import List
import poetry.factory
import poetry.installation.pip_installer
import poetry.installation.executor
import poetry.utils.env
import pytest
import tox.tox_env.python.virtual_env.runner
from poetry.core.packages.package import Package as PoetryPackage
from poetry.installation.operations.operation import Operation
from tox_poetry_installer import utilities
@ -33,23 +34,21 @@ class MockVirtualEnv:
return (1, 2, 3)
class MockPipInstaller:
"""Mock class for the :class:`poetry.installation.pip_installer.PipInstaller`"""
class MockExecutor:
"""Mock class for the :class:`poetry.installation.executor.Executor`"""
def __init__(self, env: MockVirtualEnv, **kwargs):
self.env = env
def install(self, package: PoetryPackage):
self.env.installed.append(package)
def execute(self, operations: List[Operation]):
self.env.installed.extend([operation.package for operation in operations])
time.sleep(1)
@pytest.fixture
def mock_venv(monkeypatch):
monkeypatch.setattr(utilities, "convert_virtualenv", lambda venv: venv)
monkeypatch.setattr(
poetry.installation.pip_installer, "PipInstaller", MockPipInstaller
)
monkeypatch.setattr(poetry.installation.executor, "Executor", MockExecutor)
monkeypatch.setattr(
tox.tox_env.python.virtual_env.runner, "VirtualEnvRunner", MockVirtualEnv
)

View File

@ -81,8 +81,8 @@ def test_propagates_exceptions_during_installation(
with mock.patch.object(
_poetry,
"PipInstaller",
**{"return_value.install.side_effect": fake_exception},
"Executor",
**{"return_value.execute.side_effect": fake_exception},
):
with pytest.raises(ValueError) as exc_info:
installer.install(poetry, venv, to_install, num_threads)

View File

@ -29,8 +29,12 @@ from tox_poetry_installer import exceptions
try:
from cleo.io.null_io import NullIO
from poetry.config.config import Config
from poetry.core.packages.dependency import Dependency as PoetryDependency
from poetry.core.packages.package import Package as PoetryPackage
from poetry.factory import Factory
from poetry.installation.pip_installer import PipInstaller
from poetry.installation.executor import Executor
from poetry.installation.operations.install import Install
from poetry.poetry import Poetry
from poetry.utils.env import VirtualEnv
except ImportError:

View File

@ -9,7 +9,6 @@ from datetime import datetime
from typing import Collection
from typing import Set
from poetry.core.packages.package import Package as PoetryPackage
from tox.tox_env.api import ToxEnv as ToxVirtualEnv
from tox_poetry_installer import logger
@ -22,7 +21,7 @@ if typing.TYPE_CHECKING:
def install(
poetry: "_poetry.Poetry",
venv: ToxVirtualEnv,
packages: Collection[PoetryPackage],
packages: Collection["_poetry.PoetryPackage"],
parallels: int = 0,
):
"""Install a bunch of packages to a virtualenv
@ -37,18 +36,19 @@ def install(
logger.info(f"Installing {len(packages)} packages to environment at {venv.env_dir}")
pip = _poetry.PipInstaller(
install_executor = _poetry.Executor(
env=utilities.convert_virtualenv(venv),
io=_poetry.NullIO(),
pool=poetry.pool,
config=_poetry.Config(),
)
installed: Set[PoetryPackage] = set()
installed: Set[_poetry.PoetryPackage] = set()
def logged_install(dependency: PoetryPackage) -> None:
def logged_install(dependency: _poetry.PoetryPackage) -> None:
start = datetime.now()
logger.debug(f"Installing {dependency}")
pip.install(dependency)
install_executor.execute([_poetry.Install(package=dependency)])
end = datetime.now()
logger.debug(f"Finished installing {dependency} in {end - start}")