Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: reduce internet access in tests #8744

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tests/installation/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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,
)
10 changes: 6 additions & 4 deletions tests/installation/test_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def setup(mocker: MockerFixture, pool: RepositoryPool) -> None:
mocker.patch.object(Factory, "create_pool", return_value=pool)


def test_isolated_env_install_success(pool: RepositoryPool) -> None:
def test_isolated_env_install_success(
pool: RepositoryPool, mock_file_downloads: None
) -> 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 @@ -85,12 +87,12 @@ def test_isolated_env_install_failure(
assert e.value.requirements == {"a", "b>1"}


@pytest.mark.network
def test_prepare_sdist(
config: Config,
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 @@ -104,12 +106,12 @@ def test_prepare_sdist(
assert wheel.name == "demo-0.1.0-py3-none-any.whl"


@pytest.mark.network
def test_prepare_directory(
config: Config,
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 @@ -145,12 +147,12 @@ def test_prepare_directory_with_extensions(
os.unlink(wheel)


@pytest.mark.network
def test_prepare_directory_editable(
config: Config,
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
35 changes: 1 addition & 34 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from urllib.parse import urlparse

import pytest

Expand Down Expand Up @@ -40,9 +39,6 @@
if TYPE_CHECKING:
from collections.abc import Iterator

import httpretty

from httpretty.core import HTTPrettyRequest
from pytest_mock import MockerFixture

from poetry.config.config import Config
Expand Down Expand Up @@ -134,36 +130,6 @@ def pool() -> RepositoryPool:
return pool


@pytest.fixture
def mock_file_downloads(
http: type[httpretty.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,
)


@pytest.fixture
def copy_wheel(tmp_path: Path, fixture_dir: FixtureDirGetter) -> Callable[[], Path]:
def _copy_wheel() -> Path:
Expand Down Expand Up @@ -716,6 +682,7 @@ 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