Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
poetry run: deprecate uninstalled entry points
Browse files Browse the repository at this point in the history
wagnerluis1982 committed Mar 5, 2023
1 parent f27308f commit 5f3cf7a
Showing 3 changed files with 41 additions and 14 deletions.
27 changes: 27 additions & 0 deletions src/poetry/console/commands/run.py
Original file line number Diff line number Diff line change
@@ -63,6 +63,9 @@ def run_script(self, script: str | dict[str, str], args: list[str]) -> int:
if script_path.exists():
args = [str(script_path), *args[1:]]
break
else:
# If reach this point, the script is not installed
self._warning_not_installed_script(args[0])

if isinstance(script, dict):
script = script["callable"]
@@ -81,3 +84,27 @@ def run_script(self, script: str | dict[str, str], args: list[str]) -> int:
]

return self.env.execute(*cmd)

def _warning_not_installed_script(self, script: str) -> None:
self.line_error(
(
f"Warning: '{script}' is an entry point defined in pyproject.toml,"
" but it's not installed as a script."
" You may get improper `sys.argv[0]`."
),
style="warning",
)
self.line_error("")
self.line_error(
(
"The support to run uninstalled scripts "
"will be removed in a future release."
),
style="warning",
)
self.line_error("")
self.line_error(
"Run `poetry install` to resolve and get rid of this message.",
style="warning",
)
self.line_error("")
11 changes: 7 additions & 4 deletions tests/console/commands/test_run.py
Original file line number Diff line number Diff line change
@@ -171,12 +171,15 @@ def test_run_script_sys_argv0(
environment=tmp_venv,
)
assert install_tester.execute() == 0
if not installed_script:
for path in tmp_venv.script_dirs[0].glob("check-argv0*"):

script = "check-argv0"
if installed_script:
script = tmp_venv.script_dirs[0] / script
else:
for path in tmp_venv.script_dirs[0].glob(script + "*"):
path.unlink()

tester = command_tester_factory(
"run", poetry=poetry_with_scripts, environment=tmp_venv
)
argv1 = "absolute" if installed_script else "relative"
assert tester.execute(f"check-argv0 {argv1}") == 0
assert tester.execute(f"check-argv0 {script}") == 0
17 changes: 7 additions & 10 deletions tests/fixtures/scripts/scripts/check_argv0.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
from __future__ import annotations

import os
import sys

from pathlib import Path


def main() -> int:
path = Path(sys.argv[0])
if sys.argv[1] == "absolute":
if not path.is_absolute():
raise RuntimeError(f"sys.argv[0] is not an absolute path: {path}")
if not path.exists():
raise RuntimeError(f"sys.argv[0] does not exist: {path}")
if sys.argv[0] == sys.argv[1]:
if not os.path.exists(sys.argv[0]):
raise RuntimeError(f"sys.argv[0] does not exist: {sys.argv[0]}")
else:
if path.is_absolute():
raise RuntimeError(f"sys.argv[0] is an absolute path: {path}")
raise RuntimeError(
f"unexpected sys.argv[0]: '{sys.argv[0]}', should be '{sys.argv[1]}'"
)

return 0

0 comments on commit 5f3cf7a

Please sign in to comment.