From c1ce7534e3ae0d02e82234a3736678d11c95651d Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 22 Jan 2025 06:50:53 +0100 Subject: [PATCH] feat(cli): ensure build-system is defined when running poetry build --- src/poetry/console/commands/build.py | 9 +++++++++ tests/console/commands/test_build.py | 14 ++++++++++++++ .../build_systems/no_build_system/README.md | 2 ++ .../build_systems/no_build_system/pyproject.toml | 11 +++++++++++ .../no_build_system/simple_project/__init__.py | 0 5 files changed, 36 insertions(+) create mode 100644 tests/fixtures/build_systems/no_build_system/README.md create mode 100644 tests/fixtures/build_systems/no_build_system/pyproject.toml create mode 100644 tests/fixtures/build_systems/no_build_system/simple_project/__init__.py diff --git a/src/poetry/console/commands/build.py b/src/poetry/console/commands/build.py index 0bc293b1e16..976b21c6ddd 100644 --- a/src/poetry/console/commands/build.py +++ b/src/poetry/console/commands/build.py @@ -114,6 +114,9 @@ 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( @@ -121,6 +124,12 @@ def build(self, options: BuildOptions) -> int: ) 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( diff --git a/tests/console/commands/test_build.py b/tests/console/commands/test_build.py index 18b57a3bf25..9efd00b0ed0 100644 --- a/tests/console/commands/test_build.py +++ b/tests/console/commands/test_build.py @@ -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" + ) diff --git a/tests/fixtures/build_systems/no_build_system/README.md b/tests/fixtures/build_systems/no_build_system/README.md new file mode 100644 index 00000000000..f7fe15470f9 --- /dev/null +++ b/tests/fixtures/build_systems/no_build_system/README.md @@ -0,0 +1,2 @@ +My Package +========== diff --git a/tests/fixtures/build_systems/no_build_system/pyproject.toml b/tests/fixtures/build_systems/no_build_system/pyproject.toml new file mode 100644 index 00000000000..946593d6093 --- /dev/null +++ b/tests/fixtures/build_systems/no_build_system/pyproject.toml @@ -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" diff --git a/tests/fixtures/build_systems/no_build_system/simple_project/__init__.py b/tests/fixtures/build_systems/no_build_system/simple_project/__init__.py new file mode 100644 index 00000000000..e69de29bb2d