Skip to content

Commit

Permalink
tests: Ensure the local_pypi fixtures works with uv, too
Browse files Browse the repository at this point in the history
The local_pypi fixture is used in tests to provide a reproducible
package installation environment, i.e. without _actually_ fetching
packages from PyPI.

This is done for `pip install` by setting a couple of `PIP_*`
environment variables that forces `pip` to only look at a local
directory of package files. However, the new `uv pip install` method
does not obey the same environment variables.

To configure the same for `uv` we need to write a TOML configuration
file and point `uv` to this via `UV_CONFIG_FILE` (because corresponding
`UV_NO_INDEX` and `UV_FIND_LINKS` environment variables does not yet
exist, see astral-sh/uv#1789 for details).

We setup a temporary file containing this TOML config for `uv` and
make sure it is automatically deleted after the test is run.
  • Loading branch information
jherland committed Jul 23, 2024
1 parent 9497fd7 commit 0b11c2e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import venv
from pathlib import Path
from tempfile import mkdtemp
from tempfile import NamedTemporaryFile, mkdtemp
from textwrap import dedent
from typing import Callable, Dict, List, Optional, Set, Tuple, Union

Expand Down Expand Up @@ -30,9 +30,32 @@ def inside_tmp_path(monkeypatch, tmp_path):
def local_pypi(request, monkeypatch): # noqa: PT004
cache_dir = TarballPackage.cache_dir(request.config.cache)
TarballPackage.get_tarballs(request.config.cache)
# set the test's env variables so that pip would install from the local repo
monkeypatch.setenv("PIP_NO_INDEX", "True")
monkeypatch.setenv("PIP_FIND_LINKS", str(cache_dir))
# Make sure we install from the local repo, and not from PyPI.
# This goes for both the uv and pip install methods.

# uv does not (yet) allow this to be configured via the environment, so we
# need to go via a temporary config file:
tmp_uv_config = NamedTemporaryFile("wt", delete=False, suffix=".toml")
try:
tmp_uv_config.write(
dedent(f"""
[pip]
no-index = true
find-links = ["{Path(cache_dir).as_posix()}"]
""")
)
tmp_uv_config.close()

# Point uv to the temporary config file
monkeypatch.setenv("UV_CONFIG_FILE", tmp_uv_config.name)

# pip is configured via these env vars
monkeypatch.setenv("PIP_NO_INDEX", "True")
monkeypatch.setenv("PIP_FIND_LINKS", str(cache_dir))

yield # Test code runs here
finally: # Clean up temporary file afterwards
Path(tmp_uv_config.name).unlink()


@pytest.fixture()
Expand Down

0 comments on commit 0b11c2e

Please sign in to comment.