Skip to content

Commit

Permalink
Merge pull request #80 from oshmoun/tox4
Browse files Browse the repository at this point in the history
Make plugin compatible with tox v4 and poetry 1.5+
  • Loading branch information
enpaul authored Aug 2, 2023
2 parents c55ba47 + 9efcea7 commit 76cc63e
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 456 deletions.
306 changes: 103 additions & 203 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ poetry = ["poetry", "cleo"]
[tool.poetry.dependencies]
python = "^3.7"
cleo = {version = ">=1.0,<3.0", optional = true}
poetry = {version = ">=1.2.0,<1.5.0", optional = true}
poetry = {version = "^1.5.0", optional = true}
poetry-core = "^1.1.0"
tox = "^3.8.0"
tox = "^4"

[tool.poetry.group.dev.dependencies]
bandit = "^1.6.2"
Expand All @@ -60,13 +60,8 @@ pytest-cov = "^2.10.1"
reorder-python-imports = "^2.3.5"
safety = "^2.2.0"
toml = "^0.10.1"
tox = "^3.20.0"
tox = "^4"
types-toml = "^0.10.1"
# This is a workaround for this issue with the Poetry export
# plugin which was blocking the 'security' CI check:
#
# https://github.com/python-poetry/poetry-plugin-export/issues/176
virtualenv = ">=20.15,<20.16"

[build-system]
requires = ["poetry-core>=1.1.0"]
Expand Down
24 changes: 11 additions & 13 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -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
from poetry.core.packages.package import Package as PoetryPackage
import tox.tox_env.python.virtual_env.runner
from poetry.installation.operations.operation import Operation

from tox_poetry_installer import utilities

Expand All @@ -20,11 +21,8 @@
class MockVirtualEnv:
"""Mock class for the :class:`poetry.utils.env.VirtualEnv` and :class:`tox.venv.VirtualEnv`"""

class MockTestenvConfig: # pylint: disable=missing-class-docstring
envdir = FAKE_VENV_PATH

def __init__(self, *args, **kwargs):
self.envconfig = self.MockTestenvConfig()
self.env_dir = FAKE_VENV_PATH
self.installed = []

@staticmethod
Expand All @@ -36,24 +34,24 @@ def get_version_info():
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.executor, "Executor", MockExecutor)
monkeypatch.setattr(
poetry.installation.pip_installer, "PipInstaller", MockPipInstaller
tox.tox_env.python.virtual_env.runner, "VirtualEnvRunner", MockVirtualEnv
)
monkeypatch.setattr(tox.venv, "VirtualEnv", MockVirtualEnv)
monkeypatch.setattr(poetry.utils.env, "VirtualEnv", MockVirtualEnv)


Expand Down
14 changes: 7 additions & 7 deletions tests/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import mock

import pytest
import tox.venv
import tox.tox_env.python.virtual_env.runner
from poetry.factory import Factory

from .fixtures import mock_poetry_factory
Expand All @@ -19,7 +19,7 @@ def test_deduplication(mock_venv, mock_poetry_factory):
item.name: item for item in poetry.locker.locked_repository().packages
}

venv = tox.venv.VirtualEnv()
venv = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
to_install = [packages["toml"], packages["toml"]]

installer.install(poetry, venv, to_install)
Expand All @@ -43,12 +43,12 @@ def test_parallelization(mock_venv, mock_poetry_factory):
packages["attrs"],
]

venv_sequential = tox.venv.VirtualEnv()
venv_sequential = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
start_sequential = time.time()
installer.install(poetry, venv_sequential, to_install, 0)
sequential = time.time() - start_sequential

venv_parallel = tox.venv.VirtualEnv()
venv_parallel = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
start_parallel = time.time()
installer.install(poetry, venv_parallel, to_install, 5)
parallel = time.time() - start_parallel
Expand Down Expand Up @@ -76,13 +76,13 @@ def test_propagates_exceptions_during_installation(
item.name: item for item in poetry.locker.locked_repository().packages
}
to_install = [packages["toml"]]
venv = tox.venv.VirtualEnv()
venv = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
fake_exception = ValueError("my testing exception")

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)
Expand Down
7 changes: 3 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[tox]
envlist = py37, py38, py39, py310, py311, static, static-tests, security
isolated_build = true
skip_missing_interpreters = true

[testenv]
Expand All @@ -21,7 +20,7 @@ commands =

[testenv:static]
description = Static formatting and quality enforcement
basepython = python3.10
basepython = py310
platform = linux
ignore_errors = true
locked_deps =
Expand All @@ -46,7 +45,7 @@ commands =

[testenv:static-tests]
description = Static formatting and quality enforcement for the tests
basepython = python3.10
basepython = py310
platform = linux
ignore_errors = true
locked_deps =
Expand All @@ -63,7 +62,7 @@ commands =

[testenv:security]
description = Security checks
basepython = python3.10
basepython = py310
platform = linux
ignore_errors = true
skip_install = true
Expand Down
5 changes: 3 additions & 2 deletions tox_poetry_installer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=missing-docstring
from tox_poetry_installer.hooks import tox_addoption
from tox_poetry_installer.hooks import tox_testenv_install_deps
from tox_poetry_installer.hooks import tox_add_env_config
from tox_poetry_installer.hooks import tox_add_option
from tox_poetry_installer.hooks import tox_on_install
6 changes: 5 additions & 1 deletion tox_poetry_installer/_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@

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:
Expand Down
Loading

0 comments on commit 76cc63e

Please sign in to comment.