2024-08-15 18:41:34 +00:00
|
|
|
# pylint: disable=missing-module-docstring,redefined-outer-name,unused-argument,unused-import,protected-access
|
2021-04-16 23:40:52 +00:00
|
|
|
import time
|
2023-03-29 22:52:51 +00:00
|
|
|
from unittest import mock
|
2021-04-16 23:40:52 +00:00
|
|
|
|
2023-03-29 22:52:51 +00:00
|
|
|
import pytest
|
2023-01-10 02:34:35 +00:00
|
|
|
import tox.tox_env.python.virtual_env.runner
|
2021-04-16 23:40:52 +00:00
|
|
|
from poetry.factory import Factory
|
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
import tox_poetry_installer.hooks._tox_on_install_helpers
|
2024-08-16 18:27:46 +00:00
|
|
|
|
2021-04-16 23:40:52 +00:00
|
|
|
from .fixtures import mock_poetry_factory
|
|
|
|
from .fixtures import mock_venv
|
|
|
|
|
|
|
|
|
|
|
|
def test_deduplication(mock_venv, mock_poetry_factory):
|
|
|
|
"""Test that the installer does not install duplicate dependencies"""
|
|
|
|
poetry = Factory().create_poetry(None)
|
2024-08-13 17:44:25 +00:00
|
|
|
packages: tox_poetry_installer.hooks._tox_on_install_helpers.PackageMap = {
|
2022-07-02 02:08:11 +00:00
|
|
|
item.name: item for item in poetry.locker.locked_repository().packages
|
2021-04-16 23:40:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
venv = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
|
2021-04-16 23:40:52 +00:00
|
|
|
to_install = [packages["toml"], packages["toml"]]
|
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
tox_poetry_installer.hooks._tox_on_install_helpers.install_package(
|
|
|
|
poetry, venv, to_install
|
|
|
|
)
|
2021-04-16 23:40:52 +00:00
|
|
|
|
2021-04-17 02:33:08 +00:00
|
|
|
assert len(set(to_install)) == len(venv.installed) # pylint: disable=no-member
|
2021-04-16 23:40:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parallelization(mock_venv, mock_poetry_factory):
|
|
|
|
"""Test that behavior is consistent between parallel and non-parallel usage"""
|
|
|
|
poetry = Factory().create_poetry(None)
|
2024-08-13 17:44:25 +00:00
|
|
|
packages: tox_poetry_installer.hooks._tox_on_install_helpers.PackageMap = {
|
2022-07-02 02:08:11 +00:00
|
|
|
item.name: item for item in poetry.locker.locked_repository().packages
|
2021-04-16 23:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
to_install = [
|
|
|
|
packages["toml"],
|
|
|
|
packages["toml"],
|
|
|
|
packages["tox"],
|
|
|
|
packages["requests"],
|
|
|
|
packages["python-dateutil"],
|
|
|
|
packages["attrs"],
|
|
|
|
]
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
venv_sequential = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
|
2021-04-16 23:40:52 +00:00
|
|
|
start_sequential = time.time()
|
2024-08-13 17:44:25 +00:00
|
|
|
tox_poetry_installer.hooks._tox_on_install_helpers.install_package(
|
|
|
|
poetry, venv_sequential, to_install, 0
|
|
|
|
)
|
2021-04-16 23:40:52 +00:00
|
|
|
sequential = time.time() - start_sequential
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
venv_parallel = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
|
2021-04-16 23:40:52 +00:00
|
|
|
start_parallel = time.time()
|
2024-08-13 17:44:25 +00:00
|
|
|
tox_poetry_installer.hooks._tox_on_install_helpers.install_package(
|
|
|
|
poetry, venv_parallel, to_install, 5
|
|
|
|
)
|
2021-04-16 23:40:52 +00:00
|
|
|
parallel = time.time() - start_parallel
|
|
|
|
|
|
|
|
# The mock delay during package install is static (one second) so these values should all
|
|
|
|
# be within microseconds of each other
|
|
|
|
assert parallel < sequential
|
|
|
|
assert round(parallel * 5) == round(sequential)
|
|
|
|
assert round(sequential) == len(set(to_install))
|
|
|
|
assert round(parallel * 5) == len(set(to_install))
|
2023-03-29 22:52:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("num_threads", (0, 8))
|
|
|
|
def test_propagates_exceptions_during_installation(
|
|
|
|
mock_venv, mock_poetry_factory, num_threads
|
|
|
|
):
|
2023-03-29 22:53:14 +00:00
|
|
|
"""Assert that an exception which occurs during installation is properly raised.
|
|
|
|
|
|
|
|
Regression test for https://github.com/enpaul/tox-poetry-installer/issues/86
|
|
|
|
"""
|
|
|
|
from tox_poetry_installer import _poetry # pylint: disable=import-outside-toplevel
|
2023-03-29 22:53:06 +00:00
|
|
|
|
2023-03-29 22:52:51 +00:00
|
|
|
poetry = Factory().create_poetry(None)
|
2024-08-13 17:44:25 +00:00
|
|
|
packages: tox_poetry_installer.hooks._tox_on_install_helpers.PackageMap = {
|
2023-03-29 22:52:51 +00:00
|
|
|
item.name: item for item in poetry.locker.locked_repository().packages
|
|
|
|
}
|
|
|
|
to_install = [packages["toml"]]
|
2023-01-10 02:34:35 +00:00
|
|
|
venv = tox.tox_env.python.virtual_env.runner.VirtualEnvRunner()
|
2023-03-29 22:52:51 +00:00
|
|
|
fake_exception = ValueError("my testing exception")
|
|
|
|
|
|
|
|
with mock.patch.object(
|
|
|
|
_poetry,
|
2023-05-04 11:05:02 +00:00
|
|
|
"Executor",
|
|
|
|
**{"return_value.execute.side_effect": fake_exception},
|
2023-03-29 22:52:51 +00:00
|
|
|
):
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
2024-08-13 17:44:25 +00:00
|
|
|
tox_poetry_installer.hooks._tox_on_install_helpers.install_package(
|
|
|
|
poetry, venv, to_install, num_threads
|
|
|
|
)
|
2023-03-29 22:52:51 +00:00
|
|
|
|
|
|
|
assert exc_info.value is fake_exception
|