Skip to content

Commit

Permalink
build: make build formats dict available, handle deprecation of `poet…
Browse files Browse the repository at this point in the history
…ry.core.masonry.builder`

Co-authored-by: Bartosz Sokorski <b.sokorski@gmail.com>
  • Loading branch information
radoering and Secrus committed Jan 14, 2024
1 parent 05f39ef commit a2f81ba
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
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,
}
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

0 comments on commit a2f81ba

Please sign in to comment.