Skip to content

Commit 72844e0

Browse files
committed
build: add --clean option
Resolves: #1329
1 parent 104dc1c commit 72844e0

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

docs/cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ Note that, at the moment, only pure python wheels are supported.
548548
### Options
549549

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

src/poetry/console/commands/build.py

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from poetry.console.commands.env_command import EnvCommand
88
from poetry.utils.env import build_environment
9+
from poetry.utils.helpers import remove_directory
910

1011

1112
class BuildCommand(EnvCommand):
@@ -14,6 +15,11 @@ class BuildCommand(EnvCommand):
1415

1516
options = [
1617
option("format", "f", "Limit the format to either sdist or wheel.", flag=False),
18+
option(
19+
"clean",
20+
"Clean output directory before building.",
21+
flag=True,
22+
),
1723
option(
1824
"local-version",
1925
"l",
@@ -74,6 +80,10 @@ def handle(self) -> int:
7480

7581
if not dist_dir.is_absolute():
7682
dist_dir = self.poetry.pyproject_path.parent / dist_dir
83+
84+
if self.option("clean"):
85+
remove_directory(path=dist_dir, force=True)
86+
7787
self._build(fmt, executable=env.python, target_dir=dist_dir)
7888

7989
return 0

tests/console/commands/test_build.py

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from poetry.factory import Factory
11+
from poetry.utils.helpers import remove_directory
1112

1213

1314
if TYPE_CHECKING:
@@ -82,6 +83,35 @@ def test_build_with_local_version_label(
8283
assert all(archive.exists() for archive in build_artifacts)
8384

8485

86+
@pytest.mark.parametrize("clean", [True, False])
87+
def test_build_with_clean(
88+
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry, clean: bool
89+
) -> None:
90+
dist_dir = tmp_project_path.joinpath("dist")
91+
dist_dir.joinpath("hello").touch(exist_ok=True)
92+
93+
tmp_tester.execute("--clean" if clean else "")
94+
build_artifacts = tuple(dist_dir.glob("*"))
95+
96+
assert len(build_artifacts) == 2 if clean else 3
97+
assert all(archive.exists() for archive in build_artifacts)
98+
99+
100+
def test_build_with_clean_non_existing_output(
101+
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry
102+
) -> None:
103+
dist_dir = tmp_project_path.joinpath("dist")
104+
105+
remove_directory(dist_dir, force=True)
106+
assert not dist_dir.exists()
107+
108+
tmp_tester.execute("--clean")
109+
build_artifacts = tuple(dist_dir.glob("*"))
110+
111+
assert len(build_artifacts) == 2
112+
assert all(archive.exists() for archive in build_artifacts)
113+
114+
85115
def test_build_not_possible_in_non_package_mode(
86116
fixture_dir: FixtureDirGetter,
87117
command_tester_factory: CommandTesterFactory,

0 commit comments

Comments
 (0)