Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix script entrypoints with extras #3431

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def handle(self):
self.line("")
return 0

builder.extras(extras)

builder.build()

if self._io.supports_ansi() and not self.io.is_debug():
Expand Down
13 changes: 13 additions & 0 deletions poetry/masonry/builders/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def __init__(self, poetry, env, io):
self._env = env
self._io = io

self._extras = []

def build(self):
self._debug(
" - Building package <c1>{}</c1> in <info>editable</info> mode".format(
Expand Down Expand Up @@ -143,6 +145,12 @@ def _add_scripts(self):

scripts = entry_points.get("console_scripts", [])
for script in scripts:
if script.endswith("]"):
script, extras = script[:-1].split("[")
extras = set(extras.split(","))
if extras - set(self._extras):
continue

name, script = script.split(" = ")
module, callable_ = script.split(":")
callable_holder = callable_.split(".", 1)[0]
Expand Down Expand Up @@ -255,3 +263,8 @@ def _get_file_hash(self, filepath):
def _debug(self, msg):
if self._io.is_debug():
self._io.write_line(msg)

def extras(self, extras): # type: (list) -> EditableBuilder
self._extras = extras

return self
3 changes: 3 additions & 0 deletions tests/fixtures/project_with_extras/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ cachy = { version = ">=0.2.0", optional = true }
extras_a = [ "pendulum" ]
extras_b = [ "cachy" ]

[tool.poetry.scripts]
foo = { callable = "project_with_extras:foo", extras = ["extras_a"]}

[tool.poetry.dev-dependencies]
36 changes: 36 additions & 0 deletions tests/masonry/builders/test_editable_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def extended_without_setup_poetry():
return poetry


@pytest.fixture()
def project_with_extras():
poetry = Factory().create_poetry(
Path(__file__).parent.parent.parent / "fixtures" / "project_with_extras"
)

return poetry


@pytest.fixture()
def env_manager(simple_poetry):
return EnvManager(simple_poetry)
Expand Down Expand Up @@ -228,3 +237,30 @@ def test_builder_should_execute_build_scripts(extended_without_setup_poetry):
assert [
["python", str(extended_without_setup_poetry.file.parent / "build.py")]
] == env.executed


def test_builder_doesnt_install_extra_scripts(project_with_extras, tmp_venv):
builder = EditableBuilder(project_with_extras, tmp_venv, NullIO())

builder.build()

assert not tmp_venv._bin_dir.joinpath("foo").is_file()


def test_builder_does_install_extra_scripts(project_with_extras, tmp_venv):
builder = EditableBuilder(project_with_extras, tmp_venv, NullIO())

builder.extras(["extras_a"])
builder.build()

foo_script = """\
#!{python}
from project_with_extras import foo

if __name__ == '__main__':
foo()
""".format(
python=tmp_venv._bin("python")
)

assert tmp_venv._bin_dir.joinpath("foo").read_text() == foo_script