Skip to content

Commit 6628f9d

Browse files
committed
simplify test and apply Poetry style of writing test
1 parent eead520 commit 6628f9d

1 file changed

Lines changed: 7 additions & 20 deletions

File tree

tests/inspection/test_lazy_wheel.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import TYPE_CHECKING
88
from typing import Protocol
99
from urllib.parse import urlparse
10+
from zipfile import ZipInfo
1011

1112
import pytest
1213
import requests
@@ -17,6 +18,7 @@
1718
from poetry.inspection.lazy_wheel import HTTPRangeRequestNotRespectedError
1819
from poetry.inspection.lazy_wheel import HTTPRangeRequestUnsupportedError
1920
from poetry.inspection.lazy_wheel import InvalidWheelError
21+
from poetry.inspection.lazy_wheel import LazyWheelOverHTTP
2022
from poetry.inspection.lazy_wheel import LazyWheelUnsupportedError
2123
from poetry.inspection.lazy_wheel import metadata_from_wheel_url
2224
from tests.helpers import http_setup_redirect
@@ -471,31 +473,16 @@ def test_metadata_from_wheel_url_handles_unexpected_errors(
471473
)
472474

473475

474-
def test_prefetch_metadata_closes_zipfile_on_error() -> None:
476+
def test_prefetch_metadata_closes_zipfile_on_error(mocker: MockerFixture) -> None:
475477
"""ZipFile opened in _prefetch_metadata must be closed even if an error occurs."""
476-
import re
477-
478-
from unittest.mock import MagicMock
479-
from unittest.mock import patch
480-
from zipfile import ZipInfo
481-
482-
from poetry.inspection.lazy_wheel import LazyWheelOverHTTP
483-
484-
lazy = object.__new__(LazyWheelOverHTTP)
485-
lazy._metadata_regex = re.compile(r"\.dist-info/METADATA$")
486-
lazy._file = MagicMock()
487-
lazy._file.name = "test.whl"
488-
489-
mock_zf = MagicMock()
478+
mock_zf = mocker.MagicMock()
490479
# Return entries that don't match the metadata regex, triggering UnsupportedWheelError
491480
mock_zf.infolist.return_value = [ZipInfo("pkg/data.txt")]
481+
mocker.patch("poetry.inspection.lazy_wheel.ZipFile", return_value=mock_zf)
492482

493-
mock_zf.__exit__ = MagicMock(return_value=False)
483+
lazy = LazyWheelOverHTTP("url", requests.Session())
494484

495-
with (
496-
patch("poetry.inspection.lazy_wheel.ZipFile", return_value=mock_zf),
497-
pytest.raises(Exception, match=r"no .* found for"),
498-
):
485+
with pytest.raises(Exception, match=r"no .* found for"):
499486
lazy._prefetch_metadata("test-pkg")
500487

501488
mock_zf.__exit__.assert_called_once()

0 commit comments

Comments
 (0)