Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure setup.py editable builds do not use pep517 #5590

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/poetry/utils/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def pip_install(
# lot of packages.
args = ["install", "--disable-pip-version-check", "--prefix", str(environment.path)]

if not is_wheel:
if not is_wheel and not editable:
args.insert(1, "--use-pep517")

if upgrade:
Expand Down
23 changes: 8 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from tests.helpers import TestLocker
from tests.helpers import TestRepository
from tests.helpers import get_package
from tests.helpers import isolated_environment
from tests.helpers import mock_clone
from tests.helpers import mock_download

Expand Down Expand Up @@ -246,27 +247,19 @@ def _pep517_metadata(cls: PackageInfo, path: Path) -> PackageInfo:

@pytest.fixture
def environ() -> Iterator[None]:
original_environ = dict(os.environ)

yield

os.environ.clear()
os.environ.update(original_environ)
with isolated_environment():
yield


@pytest.fixture(autouse=True)
def isolate_environ() -> Iterator[None]:
"""Ensure the environment is isolated from user configuration."""
original_environ = dict(os.environ)

for var in os.environ:
if var.startswith("POETRY_"):
del os.environ[var]

yield
with isolated_environment():
for var in os.environ:
if var.startswith("POETRY_") or var in {"PYTHONPATH", "VIRTUAL_ENV"}:
del os.environ[var]

os.environ.clear()
os.environ.update(original_environ)
yield


@pytest.fixture(autouse=True)
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/extended_project/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

from pathlib import Path
from typing import Any


def build(setup_kwargs: dict[str, Any]):
assert setup_kwargs["name"] == "extended-project"
assert setup_kwargs["version"] == "1.2.3"

dynamic_module = Path(__file__).parent / "extended_project" / "built.py"
dynamic_module.write_text("# Generated by build.py")
6 changes: 4 additions & 2 deletions tests/fixtures/extended_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules"
]

build = "build.py"
[tool.poetry.build]
script = "build.py"
generate-setup-file = true

# Requirements
[tool.poetry.dependencies]
python = "~2.7 || ^3.4"
python = "^3.7"
20 changes: 20 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import os
import re
import shutil
Expand All @@ -9,6 +10,7 @@
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator

from poetry.core.masonry.utils.helpers import escape_name
from poetry.core.masonry.utils.helpers import escape_version
Expand Down Expand Up @@ -230,3 +232,21 @@ def find_links_for_package(self, package: Package) -> list[Link]:
f"-{escape_version(package.version.text)}-py2.py3-none-any.whl"
)
]


@contextlib.contextmanager
def isolated_environment(
environ: dict[str, Any] | None = None, clear: bool = False
) -> Iterator[None]:
original_environ = dict(os.environ)

if clear:
os.environ.clear()

if environ:
os.environ.update(environ)

yield

os.environ.clear()
os.environ.update(original_environ)
38 changes: 38 additions & 0 deletions tests/masonry/builders/test_editable_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

from poetry.factory import Factory
from poetry.masonry.builders.editable import EditableBuilder
from poetry.repositories.installed_repository import InstalledRepository
from poetry.utils.env import EnvCommandError
from poetry.utils.env import EnvManager
from poetry.utils.env import MockEnv
from poetry.utils.env import VirtualEnv
from poetry.utils.env import ephemeral_environment


if TYPE_CHECKING:
Expand Down Expand Up @@ -203,6 +206,41 @@ def test_builder_falls_back_on_setup_and_pip_for_packages_with_build_scripts(
assert [] == env.executed


def test_builder_setup_generation_runs_with_pip_editable(tmp_dir: str):
# create an isolated copy of the project
fixture = Path(__file__).parent.parent.parent / "fixtures" / "extended_project"
extended_project = Path(tmp_dir) / "extended_project"

shutil.copytree(fixture, extended_project)
assert extended_project.exists()

poetry = Factory().create_poetry(extended_project)

# we need a venv with setuptools since we are verifying setup.py builds
with ephemeral_environment(flags={"no-setuptools": False}) as venv:
builder = EditableBuilder(poetry, venv, NullIO())
builder.build()

# is the package installed?
repository = InstalledRepository.load(venv)
assert repository.package("extended-project", "1.2.3")

# check for the module built by build.py
try:
output = venv.run_python_script(
"from extended_project import built; print(built.__file__)"
).strip()
except EnvCommandError:
pytest.fail("Unable to import built module")
else:
built_py = Path(output).resolve()

expected = extended_project / "extended_project" / "built.py"

# ensure the package was installed as editable
assert built_py == expected.resolve()


def test_builder_installs_proper_files_when_packages_configured(
project_with_include: Poetry, tmp_venv: VirtualEnv
):
Expand Down