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 bc2e133 commit b820fda
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 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
30 changes: 15 additions & 15 deletions tests/utils/test_isolated_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from poetry.puzzle.exceptions import SolverProblemError
from poetry.puzzle.provider import IncompatibleConstraintsError
from poetry.repositories import RepositoryPool
from poetry.repositories.installed_repository import InstalledRepository
from poetry.utils.env import ephemeral_environment
from poetry.utils.isolated_build import IsolatedBuildInstallError
from poetry.utils.isolated_build import IsolatedEnv
from tests.helpers import get_dependency
from poetry.utils.isolated_build import isolated_builder


if TYPE_CHECKING:
Expand All @@ -24,6 +23,7 @@
from pytest_mock import MockerFixture

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


@pytest.fixture()
Expand All @@ -40,19 +40,6 @@ def setup(mocker: MockerFixture, pool: RepositoryPool) -> None:
mocker.patch.object(Factory, "create_pool", return_value=pool)


def test_isolated_env_install_success(pool: RepositoryPool) -> None:
with ephemeral_environment(Path(sys.executable)) as venv:
env = IsolatedEnv(venv, pool)
assert not InstalledRepository.load(venv).find_packages(
get_dependency("poetry-core")
)

env.install({"poetry-core"})
assert InstalledRepository.load(venv).find_packages(
get_dependency("poetry-core")
)


@pytest.mark.parametrize(
("requirements", "exception"),
[
Expand All @@ -78,3 +65,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 b820fda

Please sign in to comment.