Skip to content

Commit

Permalink
isolated build: fallback to PyPI only pool
Browse files Browse the repository at this point in the history
When isolated builder context manager is invoked outside a Poetry
project context, fall back to a PyPI only package source.
  • Loading branch information
abn committed Mar 19, 2024
1 parent 4de46a8 commit 8e12432
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/poetry/utils/isolated_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ def isolated_builder(

from poetry.factory import Factory

# we recreate the project's Poetry instance in order to retrieve the correct repository pool
# when a pool is not provided
pool = pool or Factory().create_poetry().pool
try:
# we recreate the project's Poetry instance in order to retrieve the correct repository pool
# when a pool is not provided
pool = pool or Factory().create_poetry().pool
except RuntimeError:
# the context manager is not being called within a Poetry project context
# fallback to a default pool using only PyPI as source
from poetry.repositories import RepositoryPool
from poetry.repositories.pypi_repository import PyPiRepository

# fallback to using only PyPI
pool = RepositoryPool(repositories=[PyPiRepository()])

python_executable = (
python_executable or EnvManager.get_system_env(naive=True).python
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/test_isolated_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from poetry.utils.env import ephemeral_environment
from poetry.utils.isolated_build import IsolatedBuildInstallError
from poetry.utils.isolated_build import IsolatedEnv
from poetry.utils.isolated_build import isolated_builder
from tests.helpers import get_dependency


Expand All @@ -24,6 +25,7 @@
from pytest_mock import MockerFixture

from poetry.repositories.pypi_repository import PyPiRepository
from tests.types import FixtureDirGetter


@pytest.fixture()
Expand Down Expand Up @@ -78,3 +80,16 @@ def test_isolated_env_install_failure(
with pytest.raises(IsolatedBuildInstallError) as e:
env.install({"a", "b>1"})
assert e.value.requirements == {"a", "b>1"}


def test_isolated_builder_outside_poetry_project_context(
tmp_working_directory: Path, fixture_dir: FixtureDirGetter
) -> None:
source = fixture_dir("project_with_setup")
destination = tmp_working_directory / "dist"

try:
with isolated_builder(source, "wheel") as builder:
builder.metadata_path(destination)
except RuntimeError:
pytest.fail("Isolated builder did not fallback to default repository pool")

0 comments on commit 8e12432

Please sign in to comment.