Skip to content

Commit

Permalink
tests: add stubbed mock dists and improve mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Mar 10, 2024
1 parent 8235b0e commit 164b093
Show file tree
Hide file tree
Showing 94 changed files with 182 additions and 304 deletions.
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ def legacy_repository(
mock_files_python_hosted_factory(
[
FIXTURE_PATH_REPOSITORIES_PYPI / "dists",
FIXTURE_PATH_REPOSITORIES_PYPI / "stubbed",
FIXTURE_PATH_DISTRIBUTIONS,
],
FIXTURE_PATH_REPOSITORIES / "metadata",
Expand Down Expand Up @@ -714,6 +715,7 @@ def pypi_repository(
mock_files_python_hosted_factory(
[
FIXTURE_PATH_REPOSITORIES_PYPI / "dists",
FIXTURE_PATH_REPOSITORIES_PYPI / "stubbed",
FIXTURE_PATH_DISTRIBUTIONS,
],
FIXTURE_PATH_REPOSITORIES / "metadata",
Expand Down
45 changes: 0 additions & 45 deletions tests/installation/conftest.py
Original file line number Diff line number Diff line change
@@ -1,45 +0,0 @@
from __future__ import annotations

import re

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from urllib.parse import urlparse

import pytest


if TYPE_CHECKING:
from httpretty import httpretty
from httpretty.core import HTTPrettyRequest

from tests.types import FixtureDirGetter


@pytest.fixture
def mock_file_downloads(http: type[httpretty], fixture_dir: FixtureDirGetter) -> None:
def callback(
request: HTTPrettyRequest, uri: str, headers: dict[str, Any]
) -> list[int | dict[str, Any] | bytes]:
name = Path(urlparse(uri).path).name

fixture = Path(__file__).parent.parent.joinpath(
"repositories/fixtures/pypi.org/dists/" + name
)

if not fixture.exists():
fixture = fixture_dir("distributions") / name

if not fixture.exists():
fixture = (
fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl"
)

return [200, headers, fixture.read_bytes()]

http.register_uri(
http.GET,
re.compile("^https://files.pythonhosted.org/.*$"),
body=callback,
)
2 changes: 1 addition & 1 deletion tests/installation/fixtures/old-lock.test
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ py = [
{file = "py-1.5.3.tar.gz", hash = "sha256:29c9fab495d7528e80ba1e343b958684f4ace687327e6f789a94bf3d1915f881"},
]
pytest = [
{file = "pytest-3.5.0-py2.py3-none-any.whl", hash = "sha256:6266f87ab64692112e5477eba395cfedda53b1933ccd29478e671e73b420c19c"},
{file = "pytest-3.5.0-py2.py3-none-any.whl", hash = "sha256:28e4d9c2ae3196d74805c2eba24f350ae4c791a5b9b397c79b41506a48dc64ca"},
{file = "pytest-3.5.0.tar.gz", hash = "sha256:fae491d1874f199537fd5872b5e1f0e74a009b979df9d53d1553fd03da1703e1"},
]
six = [
Expand Down
30 changes: 15 additions & 15 deletions tests/installation/fixtures/with-pypi-repository.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.5.0.dev0 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.9.0.dev0 and should not be changed by hand.

[[package]]
name = "attrs"
Expand All @@ -7,8 +7,8 @@ description = "Classes Without Boilerplate"
optional = false
python-versions = "*"
files = [
{file = "attrs-17.4.0-py2.py3-none-any.whl", hash = "sha256:a17a9573a6f475c99b551c0e0a812707ddda1ec9653bed04c13841404ed6f450"},
{file = "attrs-17.4.0.tar.gz", hash = "sha256:1c7960ccfd6a005cd9f7ba884e6316b5e430a3f1a6c37c5f87d8b43f83b54ec9"},
{file = "attrs-17.4.0-py2.py3-none-any.whl", hash = "sha256:d38e57f381e891928357c68e300d28d3d4dcddc50486d5f8dfaf743d40477619"},
{file = "attrs-17.4.0.tar.gz", hash = "sha256:a8727b9fe35f8938cbb51a5ce3b219a21e119f31593a530db429c2c37ebaa8c6"},
]

[package.extras]
Expand All @@ -23,8 +23,8 @@ description = "Cross-platform colored terminal text."
optional = false
python-versions = "*"
files = [
{file = "colorama-0.3.9-py2.py3-none-any.whl", hash = "sha256:463f8483208e921368c9f306094eb6f725c6ca42b0f97e313cb5d5512459feda"},
{file = "colorama-0.3.9.tar.gz", hash = "sha256:48eb22f4f8461b1df5734a074b57042430fb06e1d61bd1e11b078c0fe6d7a1f1"},
{file = "colorama-0.3.9-py2.py3-none-any.whl", hash = "sha256:5b632359f1ed2b7676a869812ba0edaacb99be04679b29eb56c07a5e137ab5a2"},
{file = "colorama-0.3.9.tar.gz", hash = "sha256:0a7cdc22e8579dae1cb9d5f5e65b9eb538fec711bf0dcb07a61b4f2701f544f0"},
]

[[package]]
Expand All @@ -34,9 +34,9 @@ description = "More routines for operating on iterables, beyond itertools"
optional = false
python-versions = "*"
files = [
{file = "more-itertools-4.1.0.tar.gz", hash = "sha256:c9ce7eccdcb901a2c75d326ea134e0886abfbea5f93e91cc95de9507c0816c44"},
{file = "more_itertools-4.1.0-py2-none-any.whl", hash = "sha256:11a625025954c20145b37ff6309cd54e39ca94f72f6bb9576d1195db6fa2442e"},
{file = "more_itertools-4.1.0-py3-none-any.whl", hash = "sha256:0dd8f72eeab0d2c3bd489025bb2f6a1b8342f9b198f6fc37b52d15cfa4531fea"},
{file = "more-itertools-4.1.0.tar.gz", hash = "sha256:ba5dd3cc0a0d2c036497bbaefe6a8130d9c8b4ac0563e3df5dfdabb8044981c6"},
{file = "more_itertools-4.1.0-py2-none-any.whl", hash = "sha256:5dd7dfd88d2fdaea446da478ffef8d7151fdf26ee92ac7ed7b14e8d71efe4b62"},
{file = "more_itertools-4.1.0-py3-none-any.whl", hash = "sha256:29b1e1661aaa56875ce090fa219fa84dfc13daecb52cd4fae321f6f57b419ec4"},
]

[package.dependencies]
Expand All @@ -49,7 +49,7 @@ description = "plugin and hook calling mechanisms for python"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
files = [
{file = "pluggy-0.6.0.tar.gz", hash = "sha256:7f8ae7f5bdf75671a718d2daf0a64b7885f74510bcd98b1a0bb420eb9a9d0cff"},
{file = "pluggy-0.6.0.tar.gz", hash = "sha256:c764684334fefd2390e89d15a9f37f8f8ca6ae0535c93954b26fbf6dace56808"},
]

[[package]]
Expand All @@ -59,8 +59,8 @@ description = "library with cross-python path, ini-parsing, io, code, log facili
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
files = [
{file = "py-1.5.3-py2.py3-none-any.whl", hash = "sha256:983f77f3331356039fdd792e9220b7b8ee1aa6bd2b25f567a963ff1de5a64f6a"},
{file = "py-1.5.3.tar.gz", hash = "sha256:29c9fab495d7528e80ba1e343b958684f4ace687327e6f789a94bf3d1915f881"},
{file = "py-1.5.3-py2.py3-none-any.whl", hash = "sha256:43ee6c7f95e0ec6a906de49906b79d138d89728fff17109d49f086abc2fdd985"},
{file = "py-1.5.3.tar.gz", hash = "sha256:fb6b107b5494ac7c13c76221002934ae27d38cee67b21131f13e4b668562005f"},
]

[[package]]
Expand All @@ -70,8 +70,8 @@ description = "pytest: simple powerful testing with Python"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
files = [
{file = "pytest-3.5.0-py2.py3-none-any.whl", hash = "sha256:6266f87ab64692112e5477eba395cfedda53b1933ccd29478e671e73b420c19c"},
{file = "pytest-3.5.0.tar.gz", hash = "sha256:fae491d1874f199537fd5872b5e1f0e74a009b979df9d53d1553fd03da1703e1"},
{file = "pytest-3.5.0-py2.py3-none-any.whl", hash = "sha256:28e4d9c2ae3196d74805c2eba24f350ae4c791a5b9b397c79b41506a48dc64ca"},
{file = "pytest-3.5.0.tar.gz", hash = "sha256:0db90e9c56935b2376a7e286f079183c756677ae565e3ca72e90fefd928229b5"},
]

[package.dependencies]
Expand Down Expand Up @@ -106,8 +106,8 @@ description = "Python 2 and 3 compatibility utilities"
optional = false
python-versions = "*"
files = [
{file = "six-1.11.0-py2.py3-none-any.whl", hash = "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"},
{file = "six-1.11.0.tar.gz", hash = "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9"},
{file = "six-1.11.0-py2.py3-none-any.whl", hash = "sha256:112f5b46e6aa106db3e4e2494a03694c938f41c4c4535edbdfc816c2e0cb50f2"},
{file = "six-1.11.0.tar.gz", hash = "sha256:a88c970d719b44e5297022628e578615ede4b0d040f9cff2d0695c048cd07f24"},
]

[metadata]
Expand Down
7 changes: 1 addition & 6 deletions tests/installation/test_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def setup(mocker: MockerFixture, pool: RepositoryPool) -> None:
mocker.patch.object(Factory, "create_pool", return_value=pool)


def test_isolated_env_install_success(
pool: RepositoryPool, mock_file_downloads: None
) -> None:
def test_isolated_env_install_success(pool: RepositoryPool) -> None:
with ephemeral_environment(Path(sys.executable)) as venv:
env = IsolatedEnv(venv, pool)
assert "poetry-core" not in venv.run("pip", "freeze")
Expand Down Expand Up @@ -92,7 +90,6 @@ def test_prepare_sdist(
config_cache_dir: Path,
artifact_cache: ArtifactCache,
fixture_dir: FixtureDirGetter,
mock_file_downloads: None,
) -> None:
chef = Chef(
artifact_cache, EnvManager.get_system_env(), Factory.create_pool(config)
Expand All @@ -111,7 +108,6 @@ def test_prepare_directory(
config_cache_dir: Path,
artifact_cache: ArtifactCache,
fixture_dir: FixtureDirGetter,
mock_file_downloads: None,
) -> None:
chef = Chef(
artifact_cache, EnvManager.get_system_env(), Factory.create_pool(config)
Expand Down Expand Up @@ -155,7 +151,6 @@ def test_prepare_directory_editable(
config_cache_dir: Path,
artifact_cache: ArtifactCache,
fixture_dir: FixtureDirGetter,
mock_file_downloads: None,
) -> None:
chef = Chef(
artifact_cache, EnvManager.get_system_env(), Factory.create_pool(config)
Expand Down
26 changes: 4 additions & 22 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from poetry.installation.operations import Uninstall
from poetry.installation.operations import Update
from poetry.installation.wheel_installer import WheelInstaller
from poetry.repositories.pypi_repository import PyPiRepository
from poetry.repositories.repository_pool import RepositoryPool
from poetry.utils.cache import ArtifactCache
from poetry.utils.env import MockEnv
Expand All @@ -43,6 +42,7 @@

from poetry.config.config import Config
from poetry.installation.operations.operation import Operation
from poetry.repositories.pypi_repository import PyPiRepository
from poetry.utils.env import VirtualEnv
from tests.types import FixtureDirGetter

Expand Down Expand Up @@ -125,7 +125,9 @@ def io_not_decorated() -> BufferedIO:
@pytest.fixture
def pool(pypi_repository: PyPiRepository) -> RepositoryPool:
pool = RepositoryPool()
pool.add_repository(PyPiRepository(fallback=False))

pypi_repository._fallback = True
pool.add_repository(pypi_repository)

return pool

Expand Down Expand Up @@ -161,7 +163,6 @@ def test_execute_executes_a_batch_of_operations(
pool: RepositoryPool,
io: BufferedIO,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
copy_wheel: Callable[[], Path],
fixture_dir: FixtureDirGetter,
Expand Down Expand Up @@ -274,7 +275,6 @@ def test_execute_prints_warning_for_yanked_package(
pool: RepositoryPool,
io: BufferedIO,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
operations: list[Operation],
has_warning: bool,
Expand Down Expand Up @@ -308,7 +308,6 @@ def test_execute_prints_warning_for_invalid_wheels(
pool: RepositoryPool,
io: BufferedIO,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
) -> None:
config.merge({"cache-dir": str(tmp_path)})
Expand Down Expand Up @@ -423,7 +422,6 @@ def test_execute_works_with_ansi_output(
pool: RepositoryPool,
io_decorated: BufferedIO,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
) -> None:
config.merge({"cache-dir": str(tmp_path)})
Expand Down Expand Up @@ -460,7 +458,6 @@ def test_execute_works_with_no_ansi_output(
pool: RepositoryPool,
io_not_decorated: BufferedIO,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
) -> None:
config.merge({"cache-dir": str(tmp_path)})
Expand Down Expand Up @@ -547,7 +544,6 @@ def test_executor_should_delete_incomplete_downloads(
tmp_path: Path,
mocker: MockerFixture,
pool: RepositoryPool,
mock_file_downloads: None,
env: MockEnv,
) -> None:
cached_archive = tmp_path / "tomlkit-0.5.3-py2.py3-none-any.whl"
Expand Down Expand Up @@ -694,7 +690,6 @@ def test_executor_should_write_pep610_url_references_for_non_wheel_files(
config: Config,
io: BufferedIO,
fixture_dir: FixtureDirGetter,
mock_file_downloads: None,
) -> None:
url = (fixture_dir("distributions") / "demo-0.1.0.tar.gz").resolve()
package = Package("demo", "0.1.0", source_type="file", source_url=url.as_posix())
Expand Down Expand Up @@ -790,7 +785,6 @@ def test_executor_should_write_pep610_url_references_for_wheel_urls(
pool: RepositoryPool,
config: Config,
io: BufferedIO,
mock_file_downloads: None,
mocker: MockerFixture,
fixture_dir: FixtureDirGetter,
is_artifact_cached: bool,
Expand Down Expand Up @@ -866,7 +860,6 @@ def test_executor_should_write_pep610_url_references_for_non_wheel_urls(
pool: RepositoryPool,
config: Config,
io: BufferedIO,
mock_file_downloads: None,
mocker: MockerFixture,
fixture_dir: FixtureDirGetter,
is_sdist_cached: bool,
Expand Down Expand Up @@ -953,7 +946,6 @@ def test_executor_should_write_pep610_url_references_for_git(
config: Config,
artifact_cache: ArtifactCache,
io: BufferedIO,
mock_file_downloads: None,
wheel: Path,
mocker: MockerFixture,
fixture_dir: FixtureDirGetter,
Expand Down Expand Up @@ -1017,7 +1009,6 @@ def test_executor_should_write_pep610_url_references_for_editable_git(
config: Config,
artifact_cache: ArtifactCache,
io: BufferedIO,
mock_file_downloads: None,
wheel: Path,
mocker: MockerFixture,
fixture_dir: FixtureDirGetter,
Expand Down Expand Up @@ -1066,7 +1057,6 @@ def test_executor_should_append_subdirectory_for_git(
config: Config,
artifact_cache: ArtifactCache,
io: BufferedIO,
mock_file_downloads: None,
wheel: Path,
) -> None:
package = Package(
Expand Down Expand Up @@ -1097,7 +1087,6 @@ def test_executor_should_write_pep610_url_references_for_git_with_subdirectories
config: Config,
artifact_cache: ArtifactCache,
io: BufferedIO,
mock_file_downloads: None,
wheel: Path,
) -> None:
package = Package(
Expand Down Expand Up @@ -1168,7 +1157,6 @@ def test_executor_fallback_on_poetry_create_error_without_wheel_installer(
pool: RepositoryPool,
io: BufferedIO,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
fixture_dir: FixtureDirGetter,
) -> None:
Expand Down Expand Up @@ -1244,7 +1232,6 @@ def test_build_backend_errors_are_reported_correctly_if_caused_by_subprocess(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
mock_file_downloads: None,
env: MockEnv,
fixture_dir: FixtureDirGetter,
) -> None:
Expand Down Expand Up @@ -1313,7 +1300,6 @@ def test_build_backend_errors_are_reported_correctly_if_caused_by_subprocess_enc
config: Config,
pool: RepositoryPool,
io: BufferedIO,
mock_file_downloads: None,
env: MockEnv,
fixture_dir: FixtureDirGetter,
) -> None:
Expand Down Expand Up @@ -1349,7 +1335,6 @@ def test_build_system_requires_not_available(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
mock_file_downloads: None,
env: MockEnv,
fixture_dir: FixtureDirGetter,
) -> None:
Expand Down Expand Up @@ -1395,7 +1380,6 @@ def test_build_system_requires_install_failure(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
mock_file_downloads: None,
env: MockEnv,
fixture_dir: FixtureDirGetter,
) -> None:
Expand Down Expand Up @@ -1450,7 +1434,6 @@ def test_other_error(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
mock_file_downloads: None,
env: MockEnv,
fixture_dir: FixtureDirGetter,
) -> None:
Expand Down Expand Up @@ -1562,7 +1545,6 @@ def test_executor_known_hashes(
config: Config,
io: BufferedIO,
fixture_dir: FixtureDirGetter,
mock_file_downloads: None,
) -> None:
# when installing sdist, an isolated build environment is required to extract metadata
# this will install any build system requirements into the environment, to avoid failures when
Expand Down
Loading

0 comments on commit 164b093

Please sign in to comment.