Skip to content

Commit

Permalink
feat(cli): ensure build-system is defined when running poetry build
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Jan 22, 2025
1 parent 9d9423e commit c1ce753
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/poetry/console/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,22 @@ def _get_builder(self) -> Callable[..., None]:

return self._build

def _has_build_system_defined(self) -> bool:
return "build-system" in self.poetry.file.read()

def build(self, options: BuildOptions) -> int:
if not self.poetry.is_package_mode:
self.io.write_error_line(
"Building a package is not possible in non-package mode."
)
return 1

if not self._has_build_system_defined():
self.io.write_error_line(
"No build-system defined. Please define one in pyproject.toml."
)
return 1

dist_dir = Path(options.output)
package = self.poetry.package
self.io.write_line(
Expand Down
14 changes: 14 additions & 0 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,17 @@ def test_build_handler_build_isolated(
handler.build(BuildOptions(clean=True, formats=["wheel"], output="dist"))

assert mock_builder.build.call_count == 1


def test_build_handler_raise_on_no_build_system(
fixture_dir: FixtureDirGetter,
command_tester_factory: CommandTesterFactory,
) -> None:
poetry = Factory().create_poetry(fixture_dir("build_systems/no_build_system"))
tester = command_tester_factory("build", poetry)

assert tester.execute() == 1
assert (
tester.io.fetch_error()
== "No build-system defined. Please define one in pyproject.toml.\n"
)
2 changes: 2 additions & 0 deletions tests/fixtures/build_systems/no_build_system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
My Package
==========
11 changes: 11 additions & 0 deletions tests/fixtures/build_systems/no_build_system/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "core-from-git"
version = "1.2.3"
description = "Some description."
authors = [
{ name = "Poetry Contributors", email = "no-reply@python-poetry.org" }
]
license = { text = "MIT" }
readme = "README.md"
keywords = ["packaging", "dependency", "poetry"]
requires-python = ">=3.4"
Empty file.

0 comments on commit c1ce753

Please sign in to comment.