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

inspection: error on unsupported metadata versions #9203

Merged
merged 2 commits into from
Mar 23, 2024
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
9 changes: 7 additions & 2 deletions src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ def _from_distribution(

:param dist: The distribution instance to parse information from.
"""
if dist.metadata_version not in pkginfo.distribution.HEADER_ATTRS:
# This check can be replaced once upstream implements strict parsing
# https://bugs.launchpad.net/pkginfo/+bug/2058697
raise ValueError("Unknown metadata version")
abn marked this conversation as resolved.
Show resolved Hide resolved

requirements = cls._requirements_from_distribution(dist)

info = cls(
Expand Down Expand Up @@ -545,8 +550,8 @@ def from_wheel(cls, path: Path) -> PackageInfo:
try:
wheel = pkginfo.Wheel(str(path))
return cls._from_distribution(wheel)
except ValueError:
return PackageInfo()
except ValueError as e:
raise PackageInfoError(path, e)

@classmethod
def from_bdist(cls, path: Path) -> PackageInfo:
Expand Down
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/inspection/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ def test_info_from_wheel_metadata_version_23(fixture_dir: FixtureDirGetter) -> N
assert info._source_url == path.resolve().as_posix()


def test_info_from_wheel_metadata_version_unknown(
fixture_dir: FixtureDirGetter,
) -> None:
path = (
fixture_dir("distributions")
/ "demo_metadata_version_unknown-0.1.0-py2.py3-none-any.whl"
)

with pytest.raises(PackageInfoError) as e:
PackageInfo.from_wheel(path)

assert "Unknown metadata version" in str(e.value)


def test_info_from_wheel_metadata(demo_wheel_metadata: RawMetadata) -> None:
info = PackageInfo.from_metadata(demo_wheel_metadata)
demo_check_info(info)
Expand Down
30 changes: 24 additions & 6 deletions tests/repositories/test_http_repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import shutil

from pathlib import Path
Expand All @@ -12,6 +13,7 @@
from packaging.metadata import parse_email
from poetry.core.packages.utils.link import Link

from poetry.inspection.info import PackageInfoError
from poetry.inspection.lazy_wheel import HTTPRangeRequestUnsupported
from poetry.repositories.http_repository import HTTPRepository
from poetry.utils.helpers import HTTPRangeRequestSupported
Expand Down Expand Up @@ -116,41 +118,57 @@ def test_get_info_from_wheel_state_sequence(mocker: MockerFixture) -> None:

# 1. range request and download
mock_metadata_from_wheel_url.side_effect = HTTPRangeRequestUnsupported
repo._get_info_from_wheel(link)

with contextlib.suppress(PackageInfoError):
repo._get_info_from_wheel(link)

assert mock_metadata_from_wheel_url.call_count == 1
assert mock_download.call_count == 1
assert mock_download.call_args[1]["raise_accepts_ranges"] is False

# 2. only download
repo._get_info_from_wheel(link)
with contextlib.suppress(PackageInfoError):
repo._get_info_from_wheel(link)

assert mock_metadata_from_wheel_url.call_count == 1
assert mock_download.call_count == 2
assert mock_download.call_args[1]["raise_accepts_ranges"] is True

# 3. download and range request
mock_metadata_from_wheel_url.side_effect = None
mock_download.side_effect = HTTPRangeRequestSupported
repo._get_info_from_wheel(link)

with contextlib.suppress(PackageInfoError):
repo._get_info_from_wheel(link)

assert mock_metadata_from_wheel_url.call_count == 2
assert mock_download.call_count == 3
assert mock_download.call_args[1]["raise_accepts_ranges"] is True

# 4. only range request
repo._get_info_from_wheel(link)
with contextlib.suppress(PackageInfoError):
repo._get_info_from_wheel(link)

assert mock_metadata_from_wheel_url.call_count == 3
assert mock_download.call_count == 3

# 5. range request and download
mock_metadata_from_wheel_url.side_effect = HTTPRangeRequestUnsupported
mock_download.side_effect = None
repo._get_info_from_wheel(link)

with contextlib.suppress(PackageInfoError):
repo._get_info_from_wheel(link)

assert mock_metadata_from_wheel_url.call_count == 4
assert mock_download.call_count == 4
assert mock_download.call_args[1]["raise_accepts_ranges"] is False

# 6. only range request
mock_metadata_from_wheel_url.side_effect = None
repo._get_info_from_wheel(link)

with contextlib.suppress(PackageInfoError):
repo._get_info_from_wheel(link)

assert mock_metadata_from_wheel_url.call_count == 5
assert mock_download.call_count == 4

Expand Down
Loading