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: add --clean option #9067

Merged
merged 1 commit into from
Feb 29, 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
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ Note that, at the moment, only pure python wheels are supported.
### Options

* `--format (-f)`: Limit the format to either `wheel` or `sdist`.
* `--clean`: Clean output directory before building.
* `--local-version (-l)`: Add or replace a local version label to the build.
* `--output (-o)`: Set output directory for build artifacts. Default is `dist`.

Expand Down
10 changes: 10 additions & 0 deletions src/poetry/console/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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


class BuildCommand(EnvCommand):
Expand All @@ -14,6 +15,11 @@ class BuildCommand(EnvCommand):

options = [
option("format", "f", "Limit the format to either sdist or wheel.", flag=False),
option(
"clean",
"Clean output directory before building.",
flag=True,
),
option(
"local-version",
"l",
Expand Down Expand Up @@ -74,6 +80,10 @@ def handle(self) -> int:

if not dist_dir.is_absolute():
dist_dir = self.poetry.pyproject_path.parent / dist_dir

if self.option("clean"):
remove_directory(path=dist_dir, force=True)

self._build(fmt, executable=env.python, target_dir=dist_dir)

return 0
30 changes: 30 additions & 0 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from poetry.factory import Factory
from poetry.utils.helpers import remove_directory


if TYPE_CHECKING:
Expand Down Expand Up @@ -82,6 +83,35 @@ def test_build_with_local_version_label(
assert all(archive.exists() for archive in build_artifacts)


@pytest.mark.parametrize("clean", [True, False])
def test_build_with_clean(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry, clean: bool
) -> None:
dist_dir = tmp_project_path.joinpath("dist")
dist_dir.joinpath("hello").touch(exist_ok=True)

tmp_tester.execute("--clean" if clean else "")
build_artifacts = tuple(dist_dir.glob("*"))

assert len(build_artifacts) == 2 if clean else 3
abn marked this conversation as resolved.
Show resolved Hide resolved
assert all(archive.exists() for archive in build_artifacts)


def test_build_with_clean_non_existing_output(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry
) -> None:
dist_dir = tmp_project_path.joinpath("dist")

remove_directory(dist_dir, force=True)
assert not dist_dir.exists()

tmp_tester.execute("--clean")
build_artifacts = tuple(dist_dir.glob("*"))

assert len(build_artifacts) == 2
assert all(archive.exists() for archive in build_artifacts)


def test_build_not_possible_in_non_package_mode(
fixture_dir: FixtureDirGetter,
command_tester_factory: CommandTesterFactory,
Expand Down
Loading