diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 38612ff52..253d9c550 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -352,15 +352,16 @@ def convert_script_files(self) -> list[Path]: abs_path = Path.joinpath(self._path, source) - if not abs_path.exists(): - raise RuntimeError( - f"{abs_path} in script specification ({name}) is not found." - ) - - if not abs_path.is_file(): - raise RuntimeError( - f"{abs_path} in script specification ({name}) is not a file." - ) + if not self._package.build_script: + # scripts can be generated by build_script, in this case they do not exist here + if not abs_path.exists(): + raise RuntimeError( + f"{abs_path} in script specification ({name}) is not found." + ) + if not abs_path.is_file(): + raise RuntimeError( + f"{abs_path} in script specification ({name}) is not a file." + ) script_files.append(abs_path) diff --git a/tests/masonry/builders/fixtures/generated_script_file/README.rst b/tests/masonry/builders/fixtures/generated_script_file/README.rst new file mode 100644 index 000000000..a7508bd51 --- /dev/null +++ b/tests/masonry/builders/fixtures/generated_script_file/README.rst @@ -0,0 +1,2 @@ +Module 1 +======== diff --git a/tests/masonry/builders/fixtures/generated_script_file/build.py b/tests/masonry/builders/fixtures/generated_script_file/build.py new file mode 100644 index 000000000..31b594839 --- /dev/null +++ b/tests/masonry/builders/fixtures/generated_script_file/build.py @@ -0,0 +1,14 @@ +from pathlib import Path +from setuptools.command.build_py import build_py + + +class BuildPyCommand(build_py): + def run(self): + with open("generated_script_file/generated/script.sh", "w", encoding="utf-8") as f: + f.write('#!/usr/bin/env bash\n\necho "Hello World!"\n') + ret = super().run() + return ret + + +def build(setup_kwargs): + setup_kwargs["cmdclass"] = {"build_py": BuildPyCommand} diff --git a/tests/masonry/builders/fixtures/generated_script_file/generated_script_file/__init__.py b/tests/masonry/builders/fixtures/generated_script_file/generated_script_file/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/generated_script_file/generated_script_file/generated/__init__.py b/tests/masonry/builders/fixtures/generated_script_file/generated_script_file/generated/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/generated_script_file/pyproject.toml b/tests/masonry/builders/fixtures/generated_script_file/pyproject.toml new file mode 100644 index 000000000..dfea04cc4 --- /dev/null +++ b/tests/masonry/builders/fixtures/generated_script_file/pyproject.toml @@ -0,0 +1,20 @@ +[tool.poetry] +name = "generated_script_file" +version = "0.1" +description = "Some description." +authors = [ + "Poetry Maintainers " +] +license = "MIT" +readme = "README.rst" + +[tool.poetry.scripts] +sh-script = { reference = "generated_script_file/generated/script.sh", type = "file" } + +[tool.poetry.build] +script = "build.py" +generate-setup-file = true + +[build-system] +requires = ["poetry-core", "setuptools"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/masonry/builders/test_wheel.py b/tests/masonry/builders/test_wheel.py index 6410f068f..a7f6c890e 100644 --- a/tests/masonry/builders/test_wheel.py +++ b/tests/masonry/builders/test_wheel.py @@ -451,3 +451,20 @@ def test_build_py_only_included() -> None: with zipfile.ZipFile(str(whl)) as z: assert "build_with_build_py_only/generated/file.py" in z.namelist() + + +def test_generated_script_file(tmp_path: Path) -> None: + """Tests that a file that is generated by build.py can be used as script.""" + root = fixtures_dir / "generated_script_file" + # test only works on a fresh root without already generated script file: + tmp_root = tmp_path / "generated_script_file" + shutil.copytree(root, tmp_root) + + WheelBuilder.make(Factory().create_poetry(tmp_root)) + + whl = next((tmp_root / "dist").glob("generated_script_file-0.1-*.whl")) + + assert whl.exists() + + with zipfile.ZipFile(str(whl)) as z: + assert "generated_script_file-0.1.data/scripts/script.sh" in z.namelist()