2024-08-15 18:41:34 +00:00
|
|
|
# pylint: disable=missing-module-docstring,missing-function-docstring,unused-argument,too-few-public-methods,protected-access
|
2021-04-16 23:39:10 +00:00
|
|
|
import time
|
|
|
|
from pathlib import Path
|
2023-05-04 11:05:02 +00:00
|
|
|
from typing import List
|
2021-04-16 23:39:10 +00:00
|
|
|
|
|
|
|
import poetry.factory
|
2023-05-04 11:05:02 +00:00
|
|
|
import poetry.installation.executor
|
2024-08-20 17:56:17 +00:00
|
|
|
import poetry.installation.operations.operation
|
2021-04-16 23:39:10 +00:00
|
|
|
import poetry.utils.env
|
|
|
|
import pytest
|
2023-01-10 02:34:35 +00:00
|
|
|
import tox.tox_env.python.virtual_env.runner
|
2021-04-16 23:39:10 +00:00
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
import tox_poetry_installer.hooks._tox_on_install_helpers
|
2021-04-16 23:39:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST_PROJECT_PATH = Path(__file__).parent.resolve() / "test-project"
|
|
|
|
|
|
|
|
FAKE_VENV_PATH = Path("nowhere")
|
|
|
|
|
|
|
|
|
|
|
|
class MockVirtualEnv:
|
2021-04-17 02:33:08 +00:00
|
|
|
"""Mock class for the :class:`poetry.utils.env.VirtualEnv` and :class:`tox.venv.VirtualEnv`"""
|
|
|
|
|
2021-04-16 23:39:10 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2023-01-10 02:34:35 +00:00
|
|
|
self.env_dir = FAKE_VENV_PATH
|
2021-04-16 23:39:10 +00:00
|
|
|
self.installed = []
|
|
|
|
|
2021-04-17 02:33:08 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_valid_for_marker(*args, **kwargs):
|
2021-04-16 23:39:10 +00:00
|
|
|
return True
|
|
|
|
|
2022-04-07 04:15:34 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_version_info():
|
|
|
|
return (1, 2, 3)
|
|
|
|
|
2021-04-16 23:39:10 +00:00
|
|
|
|
2023-05-04 11:05:02 +00:00
|
|
|
class MockExecutor:
|
|
|
|
"""Mock class for the :class:`poetry.installation.executor.Executor`"""
|
2021-04-17 02:33:08 +00:00
|
|
|
|
2021-04-16 23:39:10 +00:00
|
|
|
def __init__(self, env: MockVirtualEnv, **kwargs):
|
|
|
|
self.env = env
|
|
|
|
|
2024-08-20 17:56:17 +00:00
|
|
|
def execute(
|
|
|
|
self, operations: List[poetry.installation.operations.operation.Operation]
|
|
|
|
):
|
2023-05-04 11:05:02 +00:00
|
|
|
self.env.installed.extend([operation.package for operation in operations])
|
2021-04-16 23:39:10 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_venv(monkeypatch):
|
2024-08-13 17:44:25 +00:00
|
|
|
monkeypatch.setattr(
|
|
|
|
tox_poetry_installer.hooks._tox_on_install_helpers,
|
|
|
|
"convert_virtualenv",
|
|
|
|
lambda venv: venv,
|
|
|
|
)
|
2023-05-04 11:05:02 +00:00
|
|
|
monkeypatch.setattr(poetry.installation.executor, "Executor", MockExecutor)
|
2023-01-10 02:34:35 +00:00
|
|
|
monkeypatch.setattr(
|
|
|
|
tox.tox_env.python.virtual_env.runner, "VirtualEnvRunner", MockVirtualEnv
|
|
|
|
)
|
2021-04-16 23:39:10 +00:00
|
|
|
monkeypatch.setattr(poetry.utils.env, "VirtualEnv", MockVirtualEnv)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def mock_poetry_factory(monkeypatch):
|
2024-08-20 17:56:17 +00:00
|
|
|
project = poetry.factory.Factory().create_poetry(cwd=TEST_PROJECT_PATH)
|
2021-04-16 23:39:10 +00:00
|
|
|
|
|
|
|
def mock_factory(*args, **kwargs):
|
2024-08-20 17:56:17 +00:00
|
|
|
return project
|
2021-04-16 23:39:10 +00:00
|
|
|
|
|
|
|
monkeypatch.setattr(poetry.factory.Factory, "create_poetry", mock_factory)
|