From 140132d0222a31171ce2c9d7a52cda89602280db Mon Sep 17 00:00:00 2001 From: Bartosz Sokorski Date: Wed, 25 Sep 2024 12:38:47 +0200 Subject: [PATCH] Fix spaces in scripts shebang --- .../masonry/builders/test_editable_builder.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/masonry/builders/test_editable_builder.py b/tests/masonry/builders/test_editable_builder.py index 148c9af93d2..ab8190373ed 100644 --- a/tests/masonry/builders/test_editable_builder.py +++ b/tests/masonry/builders/test_editable_builder.py @@ -251,6 +251,41 @@ def test_builder_installs_proper_files_for_standard_packages( assert fox_script == tmp_venv._bin_dir.joinpath("fox").read_text(encoding="utf-8") +def test_builder_generates_scripts_with_escaped_shebang_when_spaces_in_path( + simple_poetry: Poetry, + tmp_path: Path, + fixture_dir: FixtureDirGetter, +) -> None: + simple_poetry = Factory().create_poetry(fixture_dir("simple_project")) + env_manager = EnvManager(simple_poetry) + venv_path = tmp_path / "path with space" / "venv" + env_manager.build_venv(venv_path) + tmp_venv = VirtualEnv(venv_path) + + builder = EditableBuilder(simple_poetry, tmp_venv, NullIO()) + + builder.build() + + assert tmp_venv._bin_dir.joinpath("baz").exists() + + expected_shebang = f"""/bin/sh +'''exec' '{tmp_path}/path with space/venv/bin/python' "$0" "$@" +' '''""" + + baz_script = f"""\ +#!{expected_shebang} +# -*- coding: utf-8 -*- +import re +import sys +from bar import baz +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\\.pyw|\\.exe)?$", "", sys.argv[0]) + sys.exit(baz.boom.bim()) +""" + + assert baz_script == tmp_venv._bin_dir.joinpath("baz").read_text(encoding="utf-8") + + def test_builder_falls_back_on_setup_and_pip_for_packages_with_build_scripts( mocker: MockerFixture, extended_poetry: Poetry, tmp_path: Path ) -> None: