|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +from poetry.core.packages.utils.link import Link |
| 7 | + |
| 8 | +from poetry.packages.direct_origin import DirectOrigin |
| 9 | +from poetry.utils.cache import ArtifactCache |
| 10 | + |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from pathlib import Path |
| 14 | + |
| 15 | + from pytest_mock import MockerFixture |
| 16 | + |
| 17 | + from tests.types import FixtureDirGetter |
| 18 | + |
| 19 | + |
| 20 | +def test_direct_origin_get_package_from_file(fixture_dir: FixtureDirGetter) -> None: |
| 21 | + wheel_path = fixture_dir("distributions").joinpath( |
| 22 | + "demo-0.1.2-py2.py3-none-any.whl" |
| 23 | + ) |
| 24 | + package = DirectOrigin.get_package_from_file(wheel_path) |
| 25 | + assert package.name == "demo" |
| 26 | + |
| 27 | + |
| 28 | +def test_direct_origin_caches_url_dependency(tmp_path: Path) -> None: |
| 29 | + artifact_cache = ArtifactCache(cache_dir=tmp_path) |
| 30 | + direct_origin = DirectOrigin(artifact_cache) |
| 31 | + url = "https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl" |
| 32 | + |
| 33 | + direct_origin.get_package_from_url(url) |
| 34 | + |
| 35 | + assert artifact_cache.get_cached_archive_for_link(Link(url), strict=True) |
| 36 | + |
| 37 | + |
| 38 | +def test_direct_origin_does_not_download_url_dependency_when_cached( |
| 39 | + fixture_dir: FixtureDirGetter, mocker: MockerFixture |
| 40 | +) -> None: |
| 41 | + artifact_cache = MagicMock() |
| 42 | + artifact_cache.get_cached_archive_for_link = MagicMock( |
| 43 | + return_value=fixture_dir("distributions").joinpath( |
| 44 | + "demo-0.1.2-py2.py3-none-any.whl" |
| 45 | + ) |
| 46 | + ) |
| 47 | + direct_origin = DirectOrigin(artifact_cache) |
| 48 | + url = "https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl" |
| 49 | + mocker.patch( |
| 50 | + "poetry.packages.direct_origin.download_file", |
| 51 | + side_effect=Exception("download_file should not be called"), |
| 52 | + ) |
| 53 | + |
| 54 | + direct_origin.get_package_from_url(url) |
| 55 | + |
| 56 | + artifact_cache.get_cached_archive_for_link.assert_called_once_with( |
| 57 | + Link(url), strict=True |
| 58 | + ) |
0 commit comments