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

build: make build formats dict available, handle deprecation of poetry.core.masonry.builder #8877

Merged
merged 1 commit into from
Jan 27, 2024
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
30 changes: 26 additions & 4 deletions src/poetry/console/commands/build.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cleo.helpers import option

from poetry.console.commands.env_command import EnvCommand
from poetry.utils.env import build_environment


if TYPE_CHECKING:
from pathlib import Path


class BuildCommand(EnvCommand):
name = "build"
description = "Builds a package, as a tarball and a wheel by default."
Expand All @@ -20,17 +26,33 @@ class BuildCommand(EnvCommand):
"poetry.core.masonry.builders.wheel",
]

def handle(self) -> int:
from poetry.core.masonry.builder import Builder
def _build(
self,
fmt: str,
executable: str | Path | None = None,
*,
target_dir: Path | None = None,
) -> None:
from poetry.masonry.builders import BUILD_FORMATS

if fmt in BUILD_FORMATS:
builders = [BUILD_FORMATS[fmt]]
elif fmt == "all":
builders = list(BUILD_FORMATS.values())
else:
raise ValueError(f"Invalid format: {fmt}")

for builder in builders:
builder(self.poetry, executable=executable).build(target_dir)

def handle(self) -> int:
with build_environment(poetry=self.poetry, env=self.env, io=self.io) as env:
fmt = self.option("format") or "all"
package = self.poetry.package
self.line(
f"Building <c1>{package.pretty_name}</c1> (<c2>{package.version}</c2>)"
)

builder = Builder(self.poetry)
builder.build(fmt, executable=env.python)
self._build(fmt, executable=env.python)

return 0
12 changes: 11 additions & 1 deletion src/poetry/masonry/builders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from __future__ import annotations

from poetry.core.masonry.builders.sdist import SdistBuilder
from poetry.core.masonry.builders.wheel import WheelBuilder

from poetry.masonry.builders.editable import EditableBuilder


__all__ = ["EditableBuilder"]
__all__ = ["BUILD_FORMATS", "EditableBuilder"]


# might be extended by plugins
BUILD_FORMATS = {
"sdist": SdistBuilder,
"wheel": WheelBuilder,
}
Secrus marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 46 additions & 0 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,63 @@

from typing import TYPE_CHECKING

import pytest

from poetry.factory import Factory


if TYPE_CHECKING:
from pathlib import Path

from cleo.testers.command_tester import CommandTester

from poetry.poetry import Poetry
from poetry.utils.env import VirtualEnv
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter


@pytest.fixture
def tmp_project_path(tmp_path: Path) -> Path:
return tmp_path / "project"


@pytest.fixture
def tmp_poetry(tmp_project_path: Path, fixture_dir: FixtureDirGetter) -> Poetry:
# copy project so that we start with a clean directory
shutil.copytree(fixture_dir("simple_project"), tmp_project_path)
poetry = Factory().create_poetry(tmp_project_path)
return poetry


@pytest.fixture
def tmp_tester(
tmp_poetry: Poetry, command_tester_factory: CommandTesterFactory
) -> CommandTester:
return command_tester_factory("build", tmp_poetry)


def get_package_glob(poetry: Poetry) -> str:
return f"{poetry.package.name.replace('-', '_')}-{poetry.package.version}*"


def test_build_format_is_not_valid(tmp_tester: CommandTester) -> None:
with pytest.raises(ValueError, match=r"Invalid format.*"):
tmp_tester.execute("--format not_valid")


@pytest.mark.parametrize("format", ["sdist", "wheel", "all"])
def test_build_creates_packages_in_dist_directory_if_no_output_is_specified(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry, format: str
) -> None:
tmp_tester.execute(f"--format {format}")
build_artifacts = tuple(
(tmp_project_path / "dist").glob(get_package_glob(tmp_poetry))
)
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)


def test_build_with_multiple_readme_files(
fixture_dir: FixtureDirGetter,
tmp_path: Path,
Expand Down
Loading