|
7 | 7 | from typing import TYPE_CHECKING |
8 | 8 | from typing import Protocol |
9 | 9 | from urllib.parse import urlparse |
| 10 | +from zipfile import ZipInfo |
10 | 11 |
|
11 | 12 | import pytest |
12 | 13 | import requests |
|
17 | 18 | from poetry.inspection.lazy_wheel import HTTPRangeRequestNotRespectedError |
18 | 19 | from poetry.inspection.lazy_wheel import HTTPRangeRequestUnsupportedError |
19 | 20 | from poetry.inspection.lazy_wheel import InvalidWheelError |
| 21 | +from poetry.inspection.lazy_wheel import LazyWheelOverHTTP |
20 | 22 | from poetry.inspection.lazy_wheel import LazyWheelUnsupportedError |
21 | 23 | from poetry.inspection.lazy_wheel import metadata_from_wheel_url |
22 | 24 | from tests.helpers import http_setup_redirect |
@@ -471,31 +473,16 @@ def test_metadata_from_wheel_url_handles_unexpected_errors( |
471 | 473 | ) |
472 | 474 |
|
473 | 475 |
|
474 | | -def test_prefetch_metadata_closes_zipfile_on_error() -> None: |
| 476 | +def test_prefetch_metadata_closes_zipfile_on_error(mocker: MockerFixture) -> None: |
475 | 477 | """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() |
490 | 479 | # Return entries that don't match the metadata regex, triggering UnsupportedWheelError |
491 | 480 | mock_zf.infolist.return_value = [ZipInfo("pkg/data.txt")] |
| 481 | + mocker.patch("poetry.inspection.lazy_wheel.ZipFile", return_value=mock_zf) |
492 | 482 |
|
493 | | - mock_zf.__exit__ = MagicMock(return_value=False) |
| 483 | + lazy = LazyWheelOverHTTP("url", requests.Session()) |
494 | 484 |
|
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"): |
499 | 486 | lazy._prefetch_metadata("test-pkg") |
500 | 487 |
|
501 | 488 | mock_zf.__exit__.assert_called_once() |
0 commit comments