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

Fix Relative Links for Single Page Repositories #9533

Closed
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
4 changes: 2 additions & 2 deletions src/poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from poetry.inspection.info import PackageInfo
from poetry.repositories.exceptions import PackageNotFound
from poetry.repositories.http_repository import HTTPRepository
from poetry.repositories.link_sources.html import SimpleRepositoryPage
from poetry.repositories.link_sources.html import HTMLPage, SimpleRepositoryPage
from poetry.repositories.link_sources.html import SimpleRepositoryRootPage


Expand Down Expand Up @@ -125,7 +125,7 @@ def _get_release_info(
),
)

def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage:
def _get_page(self, name: NormalizedName) -> HTMLPage:
if not (response := self._get_response(f"/{name}/")):
raise PackageNotFound(f"Package [{name}] not found.")
return SimpleRepositoryPage(response.url, response.text)
Expand Down
6 changes: 3 additions & 3 deletions src/poetry/repositories/single_page_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

from poetry.repositories.exceptions import PackageNotFound
from poetry.repositories.legacy_repository import LegacyRepository
from poetry.repositories.link_sources.html import SimpleRepositoryPage
from poetry.repositories.link_sources.html import HTMLPage


if TYPE_CHECKING:
from packaging.utils import NormalizedName


class SinglePageRepository(LegacyRepository):
def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage:
def _get_page(self, name: NormalizedName) -> HTMLPage:
"""
Single page repositories only have one page irrespective of endpoint.
"""
response = self._get_response("")
if not response:
raise PackageNotFound(f"Package [{name}] not found.")
return SimpleRepositoryPage(response.url, response.text)
return HTMLPage(response.url, response.text)
4 changes: 2 additions & 2 deletions tests/repositories/fixtures/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from packaging.utils import NormalizedName
from pytest_mock import MockerFixture

from poetry.repositories.link_sources.html import SimpleRepositoryPage
from poetry.repositories.link_sources.html import HTMLPage
from tests.types import HTTPrettyRequestCallback
from tests.types import NormalizedNameTransformer
from tests.types import SpecializedLegacyRepositoryMocker
Expand Down Expand Up @@ -129,7 +129,7 @@ def mock(
)
original_get_page = specialized_repository._get_page

def _mocked_get_page(name: NormalizedName) -> SimpleRepositoryPage:
def _mocked_get_page(name: NormalizedName) -> HTMLPage:
return original_get_page(
canonicalize_name(f"{name}{transformer_or_suffix}")
if isinstance(transformer_or_suffix, str)
Expand Down
7 changes: 7 additions & 0 deletions tests/repositories/fixtures/single-page/relative_links.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<a href="relative-links-0.1.0.tar.gz">relative-links-0.1.0.tar.gz</a><br>
</body>
</html>
31 changes: 26 additions & 5 deletions tests/repositories/test_single_page_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from pathlib import Path
from typing import TYPE_CHECKING

from requests import Response

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.utils.link import Link

from poetry.repositories.exceptions import PackageNotFound
from poetry.repositories.link_sources.html import SimpleRepositoryPage
Expand All @@ -16,24 +19,33 @@
from packaging.utils import NormalizedName


class MockResponse(Response):
def __init__(self, url: str, content: str):
super().__init__()
self.url = url
self._content = content.encode("utf-8")
self.status_code = 200


class MockSinglePageRepository(SinglePageRepository):
FIXTURES = Path(__file__).parent / "fixtures" / "single-page"
BASE_URL = "http://single-page.foo.bar"

def __init__(self, page: str) -> None:
super().__init__(
"single-page",
url=f"http://single-page.foo.bar/{page}.html",
url=f"{self.BASE_URL}/{page}.html",
disable_cache=True,
)
self._lazy_wheel = False

def _get_page(self, name: NormalizedName) -> SimpleRepositoryPage:
fixture = self.FIXTURES / self.url.rsplit("/", 1)[-1]
def _get_response(self, endpoint: str) -> MockResponse:
fixture = self.FIXTURES / (self.url.rsplit("/", 1)[-1] + endpoint)
if not fixture.exists():
raise PackageNotFound(f"Package [{name}] not found.")
raise PackageNotFound(f"Package not found.")

with fixture.open(encoding="utf-8") as f:
return SimpleRepositoryPage(self._url, f.read())
return MockResponse(self._url, f.read())

def _download(
self, url: str, dest: Path, *, raise_accepts_ranges: bool = False
Expand All @@ -55,6 +67,15 @@ def test_single_page_repository_get_page() -> None:
assert link.path.startswith("/jax-releases/")


def test_single_page_repository_relative_links_get_page() -> None:
repo = MockSinglePageRepository("relative_links")

page = repo.get_page("/ignored")
links = list(page.links)

assert links == [Link(f"{repo.BASE_URL}/relative-links-0.1.0.tar.gz")]


def test_single_page_repository_find_packages() -> None:
repo = MockSinglePageRepository("jax_releases")

Expand Down
Loading