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 342f2a9 commit bd38464
Show file tree
Hide file tree
Showing 3 changed files with 24 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.
* `--output (-o)`: Set output directory for build artifacts. Default is `dist`.

## publish
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(
"output",
"o",
Expand Down Expand Up @@ -63,6 +69,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
13 changes: 13 additions & 0 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def test_build_creates_packages_in_dist_directory_if_no_output_is_specified(
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:
(tmp_project_path / "dist" / "hello").touch(exist_ok=True)

tmp_tester.execute("--clean" if clean else "")
build_artifacts = tuple((tmp_project_path / "dist").glob("*"))

assert len(build_artifacts) == 2 if clean else 3
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 bd38464

Please sign in to comment.