From ba0caff864e405454d099001972488bf9cee6c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:42:44 +0100 Subject: [PATCH] installer: fix PATH when building a dependency from source (#8630) (cherry picked from commit c6387b87e37490fca59c51365adc878c5d0ceaa2) --- src/poetry/installation/chef.py | 6 +-- .../my_package/__init__.py | 0 .../pyproject.toml | 3 ++ .../project_with_setup_calls_script/setup.py | 23 +++++++++ tests/installation/test_chef.py | 48 +++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/project_with_setup_calls_script/my_package/__init__.py create mode 100644 tests/fixtures/project_with_setup_calls_script/pyproject.toml create mode 100644 tests/fixtures/project_with_setup_calls_script/setup.py diff --git a/src/poetry/installation/chef.py b/src/poetry/installation/chef.py index d801fb6dce2..4d5546969dd 100644 --- a/src/poetry/installation/chef.py +++ b/src/poetry/installation/chef.py @@ -129,10 +129,8 @@ def _prepare( with ephemeral_environment(self._env.python) as venv: env = IsolatedEnv(venv, self._pool) - builder = ProjectBuilder( - directory, - python_executable=env.python_executable, - runner=quiet_subprocess_runner, + builder = ProjectBuilder.from_isolated_env( + env, directory, runner=quiet_subprocess_runner ) env.install(builder.build_system_requires) diff --git a/tests/fixtures/project_with_setup_calls_script/my_package/__init__.py b/tests/fixtures/project_with_setup_calls_script/my_package/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/fixtures/project_with_setup_calls_script/pyproject.toml b/tests/fixtures/project_with_setup_calls_script/pyproject.toml new file mode 100644 index 00000000000..82470ca4e74 --- /dev/null +++ b/tests/fixtures/project_with_setup_calls_script/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools", ""] +build-backend = "setuptools.build_meta:__legacy__" diff --git a/tests/fixtures/project_with_setup_calls_script/setup.py b/tests/fixtures/project_with_setup_calls_script/setup.py new file mode 100644 index 00000000000..815e58c9e10 --- /dev/null +++ b/tests/fixtures/project_with_setup_calls_script/setup.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +import subprocess + +from setuptools import setup + +if subprocess.call(["exit-code"]) != 42: + raise RuntimeError("Wrong exit code.") + +kwargs = dict( + name="project-with-setup-calls-script", + license="MIT", + version="0.1.2", + description="Demo project.", + author="Sébastien Eustace", + author_email="sebastien@eustace.io", + url="https://github.com/demo/demo", + packages=["my_package"], + install_requires=["pendulum>=1.4.4", "cachy[msgpack]>=0.2.0"], +) + + +setup(**kwargs) diff --git a/tests/installation/test_chef.py b/tests/installation/test_chef.py index bcd4f421760..9a7e63c75c9 100644 --- a/tests/installation/test_chef.py +++ b/tests/installation/test_chef.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import shutil import sys import tempfile @@ -10,6 +11,7 @@ import pytest +from build import ProjectBuilder from poetry.core.packages.utils.link import Link from poetry.factory import Factory @@ -165,3 +167,49 @@ def test_prepare_directory_editable( # cleanup generated tmp dir artifact os.unlink(wheel) + + +@pytest.mark.network +def test_prepare_directory_script( + config: Config, + config_cache_dir: Path, + artifact_cache: ArtifactCache, + fixture_dir: FixtureDirGetter, + tmp_path: Path, + mocker: MockerFixture, +) -> None: + """ + Building a project that requires calling a script from its build_requires. + """ + # make sure the scripts project is on the same drive (for Windows tests in CI) + scripts_dir = tmp_path / "scripts" + shutil.copytree(fixture_dir("scripts"), scripts_dir) + + orig_build_system_requires = ProjectBuilder.build_system_requires + + class CustomPropertyMock: + def __get__( + self, obj: ProjectBuilder, obj_type: type[ProjectBuilder] | None = None + ) -> set[str]: + assert isinstance(obj, ProjectBuilder) + return { + req.replace("", f"scripts @ {scripts_dir.as_uri()}") + for req in orig_build_system_requires.fget(obj) # type: ignore[attr-defined] + } + + mocker.patch( + "build.ProjectBuilder.build_system_requires", + new_callable=CustomPropertyMock, + ) + chef = Chef( + artifact_cache, EnvManager.get_system_env(), Factory.create_pool(config) + ) + archive = fixture_dir("project_with_setup_calls_script").resolve() + + wheel = chef.prepare(archive) + + assert wheel.name == "project_with_setup_calls_script-0.1.2-py3-none-any.whl" + + assert wheel.parent.parent == Path(tempfile.gettempdir()) + # cleanup generated tmp dir artifact + os.unlink(wheel)