Skip to content

Commit bd38464

Browse files
committed
build: add --clean option
Resolves: #1329
1 parent 342f2a9 commit bd38464

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

docs/cli.md

Lines changed: 1 addition & 0 deletions
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
* `--output (-o)`: Set output directory for build artifacts. Default is `dist`.
552553

553554
## publish

src/poetry/console/commands/build.py

Lines changed: 10 additions & 0 deletions
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
"output",
1925
"o",
@@ -63,6 +69,10 @@ def handle(self) -> int:
6369

6470
if not dist_dir.is_absolute():
6571
dist_dir = self.poetry.pyproject_path.parent / dist_dir
72+
73+
if self.option("clean"):
74+
remove_directory(path=dist_dir, force=True)
75+
6676
self._build(fmt, executable=env.python, target_dir=dist_dir)
6777

6878
return 0

tests/console/commands/test_build.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ def test_build_creates_packages_in_dist_directory_if_no_output_is_specified(
6262
assert all(archive.exists() for archive in build_artifacts)
6363

6464

65+
@pytest.mark.parametrize("clean", [True, False])
66+
def test_build_with_clean(
67+
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry, clean: bool
68+
) -> None:
69+
(tmp_project_path / "dist" / "hello").touch(exist_ok=True)
70+
71+
tmp_tester.execute("--clean" if clean else "")
72+
build_artifacts = tuple((tmp_project_path / "dist").glob("*"))
73+
74+
assert len(build_artifacts) == 2 if clean else 3
75+
assert all(archive.exists() for archive in build_artifacts)
76+
77+
6578
def test_build_not_possible_in_non_package_mode(
6679
fixture_dir: FixtureDirGetter,
6780
command_tester_factory: CommandTesterFactory,

0 commit comments

Comments
 (0)