Skip to content

Commit

Permalink
Test pypi index alias lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-cbarber committed May 1, 2024
1 parent 60bfd80 commit 15a3a3a
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions test/impl/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,47 @@
import argparse
import subprocess
from pathlib import Path
from textwrap import dedent
from typing import List

import pytest

from whl2conda.impl.download import download_wheel
from whl2conda.impl.download import download_wheel, lookup_pypi_index
from whl2conda.settings import settings

# pylint: disable=too-many-statements

def test_lookup_pypi_index(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
"""Unit test for lookup_pypi_index function"""
home = tmp_path / "home"
home.mkdir()
monkeypatch.setenv("HOME", str(home))
monkeypatch.setenv("USERPROFILE", str(home))

assert lookup_pypi_index("foo") == "foo"

pypirc_file = home / ".pypirc"
pypirc_file.write_text(
dedent(
"""
[foo]
repository = foo-pypirc
"""
)
)
assert pypirc_file == Path("~/.pypirc").expanduser()

assert lookup_pypi_index("foo") == "foo-pypirc"
assert lookup_pypi_index("bar") == "bar"

settings.pypi_indexes["foo"] = "foo-settings"

assert lookup_pypi_index("foo") == "foo-settings"


def test_download_wheel_whitebox(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture,
) -> None:
"""
Whitebox test of download_wheel function. This does not do any
Expand Down Expand Up @@ -80,6 +110,9 @@ def call_pip_download(cmd: List[str], **_kwargs) -> subprocess.CompletedProcess:
home_dir = tmp_path / "home"
home_dir.mkdir()
monkeypatch.chdir(home_dir)
monkeypatch.setenv("HOME", str(home_dir))
monkeypatch.setenv("USERPROFILE", str(home_dir))
assert Path.home() == home_dir

whl = download_wheel("pylint")
assert whl.parent == home_dir
Expand All @@ -91,6 +124,19 @@ def call_pip_download(cmd: List[str], **_kwargs) -> subprocess.CompletedProcess:
out, err = capsys.readouterr()
assert not out and not err

somewhere_from_pypirc = "https://pypirc.somewhere.com/pypi/"

pypirc_file = home_dir / ".pypirc"
pypirc_file.write_text(
dedent(
f"""
[somewhere]
repository = {somewhere_from_pypirc}
"""
)
)
assert pypirc_file == Path("~/.pypirc").expanduser()

alt_dir = tmp_path / "alt"
assert not alt_dir.exists()

Expand All @@ -108,6 +154,14 @@ def call_pip_download(cmd: List[str], **_kwargs) -> subprocess.CompletedProcess:
assert "something happened" in err
stderr = b""

whl = download_wheel("foo", index="somewhere")
assert download_args[0].index == somewhere_from_pypirc

somewhere_from_settings = "https://settings.somewhere.com/pypi/"
settings.pypi_indexes["somewhere"] = somewhere_from_settings
whl = download_wheel("foo", index="somewhere")
assert download_args[0].index == somewhere_from_settings

n_wheels = 0
with pytest.raises(FileNotFoundError, match="No wheels downloaded"):
download_wheel("bar")
Expand Down

0 comments on commit 15a3a3a

Please sign in to comment.