Skip to content

Commit

Permalink
build: add --clean option
Browse files Browse the repository at this point in the history
Resolves: #1329
  • Loading branch information
abn committed Feb 29, 2024
1 parent 104dc1c commit 72844e0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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
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

0 comments on commit 72844e0

Please sign in to comment.