Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking13 committed Feb 19, 2025
1 parent 37c19a2 commit 9cc277e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion micropip/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def add_requirement(self, req: str | Requirement) -> None:

if urlparse(req).path.endswith(".whl"):
# custom download location
wheel = WheelInfo.from_url(req)
wheel = WheelInfo.from_url(req, compat_layer=self._compat_layer)
check_compatible(wheel.filename)
return await self.add_wheel(wheel, extras=set(), specifier="")

Expand Down
37 changes: 31 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import gzip
import io
import sys
from urllib.parse import urlparse
import zipfile
from dataclasses import dataclass
from importlib.metadata import Distribution, PackageNotFoundError
Expand All @@ -15,6 +16,7 @@
from pytest_pyodide.runner import JavascriptException

from micropip._vendored.packaging.src.packaging.utils import parse_wheel_filename
from micropip import PackageManager


def pytest_addoption(parser):
Expand Down Expand Up @@ -305,7 +307,7 @@ def add_pkg_version(
self.metadata_map[filename] = metadata
self.top_level_map[filename] = top_level

async def query_package(self, pkgname, index_urls, kwargs):
async def query_package(self, pkgname, index_urls, *args, **kwargs):
from micropip.package_index import ProjectInfo

try:
Expand All @@ -317,12 +319,10 @@ async def query_package(self, pkgname, index_urls, kwargs):
) from e

async def _fetch_bytes(self, url, kwargs):
from micropip.transaction import WheelInfo
parsed_url = urlparse(url)
filename = Path(parsed_url.path).name
name, version, _, _ = parse_wheel_filename(filename)

wheel_info = WheelInfo.from_url(url)
version = wheel_info.version
name = wheel_info.name
filename = wheel_info.filename
metadata = self.metadata_map[filename]
metadata_str = "\n".join(": ".join(x) for x in metadata)
toplevel = self.top_level_map[filename]
Expand Down Expand Up @@ -464,3 +464,28 @@ def host_compat_layer():
from micropip._compat._compat_not_in_pyodide import CompatibilityNotInPyodide

yield CompatibilityNotInPyodide


@pytest.fixture
def host_micropip(host_compat_layer):
manager = PackageManager(host_compat_layer)

yield manager


@pytest.fixture
def host_micropip_with_mock_fetch(host_compat_layer, monkeypatch):
from micropip import package_index

result = mock_fetch_cls()
class CompatibilityLayerWithMockFetch(host_compat_layer):
@staticmethod
def fetch_bytes(url, kwargs):
return result._fetch_bytes(url, kwargs)

# TODO: do not use monkeypatch
monkeypatch.setattr(package_index, "query_package", result.query_package)

manager = PackageManager(CompatibilityLayerWithMockFetch)

yield manager, result
9 changes: 4 additions & 5 deletions tests/test_freeze.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pytest
from conftest import mock_fetch_cls


@pytest.mark.asyncio
async def test_freeze(mock_fetch: mock_fetch_cls, mock_importlib: None) -> None:
import micropip
async def test_freeze(host_micropip_with_mock_fetch, mock_importlib: None) -> None:
micropip, mock_fetch = host_micropip_with_mock_fetch

dummy = "dummy"
dep1 = "dep1"
Expand Down Expand Up @@ -34,9 +33,9 @@ async def test_freeze(mock_fetch: mock_fetch_cls, mock_importlib: None) -> None:

@pytest.mark.asyncio
async def test_freeze_fix_depends(
mock_fetch: mock_fetch_cls, mock_importlib: None
host_micropip_with_mock_fetch, mock_importlib: None
) -> None:
import micropip
micropip, mock_fetch = host_micropip_with_mock_fetch

dummy = "dummy"
dep1 = "dep1"
Expand Down
21 changes: 12 additions & 9 deletions tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import pytest
from conftest import mock_fetch_cls

import micropip


@pytest.mark.asyncio
async def test_list_pypi_package(mock_fetch: mock_fetch_cls) -> None:
async def test_list_pypi_package(host_micropip_with_mock_fetch) -> None:
micropip, mock_fetch = host_micropip_with_mock_fetch

dummy = "dummy"
mock_fetch.add_pkg_version(dummy)

await micropip.install(dummy)
pkg_list = micropip.list()
pkg_list = micropip.list_packages()
assert dummy in pkg_list
assert pkg_list[dummy].source.lower() == "pypi"


@pytest.mark.asyncio
async def test_list_wheel_package(mock_fetch: mock_fetch_cls) -> None:
async def test_list_wheel_package(host_micropip_with_mock_fetch) -> None:
micropip, mock_fetch = host_micropip_with_mock_fetch

dummy = "dummy"
mock_fetch.add_pkg_version(dummy)
dummy_url = f"https://dummy.com/{dummy}-1.0.0-py3-none-any.whl"

await micropip.install(dummy_url)

pkg_list = micropip.list()
pkg_list = micropip.list_packages()
assert dummy in pkg_list
assert pkg_list[dummy].source.lower() == dummy_url


@pytest.mark.asyncio
async def test_list_wheel_name_mismatch(mock_fetch: mock_fetch_cls) -> None:
async def test_list_wheel_name_mismatch(host_micropip_with_mock_fetch) -> None:
micropip, mock_fetch = host_micropip_with_mock_fetch

dummy_pkg_name = "dummy-Dummy"
mock_fetch.add_pkg_version(dummy_pkg_name)
dummy_url = "https://dummy.com/dummy_dummy-1.0.0-py3-none-any.whl"

await micropip.install(dummy_url)

pkg_list = micropip.list()
pkg_list = micropip.list_packages()
assert dummy_pkg_name in pkg_list
assert pkg_list[dummy_pkg_name].source.lower() == dummy_url

Expand Down

0 comments on commit 9cc277e

Please sign in to comment.