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

Allow optional output directory for building #527

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
10 changes: 8 additions & 2 deletions src/poetry/core/masonry/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ def __init__(self, poetry: Poetry) -> None:
"wheel": WheelBuilder,
}

def build(self, fmt: str, executable: str | Path | None = None) -> None:
def build(
self,
fmt: str,
executable: str | Path | None = None,
*,
target_dir: Path | None = None,
) -> None:
if fmt in self._formats:
builders = [self._formats[fmt]]
elif fmt == "all":
Expand All @@ -30,4 +36,4 @@ def build(self, fmt: str, executable: str | Path | None = None) -> None:
raise ValueError(f"Invalid format: {fmt}")

for builder in builders:
builder(self._poetry, executable=executable).build()
builder(self._poetry, executable=executable).build(target_dir)
70 changes: 70 additions & 0 deletions tests/masonry/test_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from __future__ import annotations

import shutil

from contextlib import contextmanager
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Iterator

import pytest

from poetry.core.factory import Factory
from poetry.core.masonry.builder import Builder


if TYPE_CHECKING:
from poetry.core.poetry import Poetry


def get_project(name: str) -> Path:
project_directory = Path(__file__).parent / "builders" / "fixtures" / name
assert project_directory.is_dir()
return project_directory


@contextmanager
def get_project_context(name: str) -> Iterator[Path]:
project_directory = get_project(name)
try:
yield project_directory
finally:
shutil.rmtree(project_directory / "dist")


def get_poetry(name: str) -> Poetry:
return Factory().create_poetry(get_project(name))


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


def test_builder_factory_raises_error_when_format_is_not_valid() -> None:
with pytest.raises(ValueError, match=r"Invalid format.*"):
Builder(get_poetry("complete")).build("not_valid")


@pytest.mark.parametrize("format", ["sdist", "wheel", "all"])
def test_builder_creates_places_built_files_in_specified_directory(
tmp_path: Path, format: str
) -> None:
poetry = get_poetry("complete")
Builder(poetry).build(format, target_dir=tmp_path)
build_artifacts = tuple(tmp_path.glob(get_package_glob(poetry)))
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)


@pytest.mark.parametrize("format", ["sdist", "wheel", "all"])
def test_builder_creates_packages_in_dist_directory_if_no_output_is_specified(
format: str,
) -> None:
with get_project_context("complete") as project:
poetry = Factory().create_poetry(project)
Builder(poetry).build(format, target_dir=None)
package_directory = project / "dist"
build_artifacts = tuple(package_directory.glob(get_package_glob(poetry)))
assert package_directory.is_dir()
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)