Skip to content

Commit

Permalink
feat(cli): ensure build backend 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 626abfc
Show file tree
Hide file tree
Showing 8 changed files with 54 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_backend_defined(self) -> bool:
return "build-backend" in self.poetry.file.read().get("build-system", {})

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_backend_defined():
self.io.write_error_line(
"No build backend defined. Please define one in pyproject.toml."
)
return 1

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

assert mock_builder.build.call_count == 1


@pytest.mark.parametrize("project", ["no_build_system", "no_build_backend"])
def test_build_handler_raise_on_no_build_system(
project: str,
fixture_dir: FixtureDirGetter,
command_tester_factory: CommandTesterFactory,
) -> None:
poetry = Factory().create_poetry(fixture_dir(f"build_systems/{project}"))
tester = command_tester_factory("build", poetry)

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

[build-system]
requires = ["poetry-core>=1.1.0a7"]
Empty file.
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 = "simple-project"
version = "1.2.3"
description = "Some description."
authors = [
{ name = "Poetry Contributors", email = "[email protected]" }
]
license = { text = "MIT" }
readme = "README.md"
keywords = ["packaging", "dependency", "poetry"]
requires-python = ">=3.4"
Empty file.

0 comments on commit 626abfc

Please sign in to comment.