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

env/run: ensure all args are quoted as required #3015

Merged
merged 1 commit into from
Oct 1, 2020
Merged
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
25 changes: 9 additions & 16 deletions poetry/utils/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,11 @@

WINDOWS = sys.platform == "win32"

if PY2:
import pipes

shell_quote = pipes.quote
else:
import shlex

shell_quote = shlex.quote

try:
from shlex import quote
except ImportError:
# PY2
from pipes import quote # noqa

if PY34:
from importlib.machinery import EXTENSION_SUFFIXES
Expand Down Expand Up @@ -288,10 +284,7 @@ def to_str(string):


def list_to_shell_command(cmd):
executable = cmd[0]

if " " in executable:
executable = '"{}"'.format(executable)
cmd[0] = executable

return " ".join(cmd)
return " ".join(
'"{}"'.format(token) if " " in token and token[0] not in {"'", '"'} else token
for token in cmd
)
21 changes: 21 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from poetry.factory import Factory
from poetry.utils._compat import PY2
from poetry.utils._compat import Path
from poetry.utils.env import GET_BASE_PREFIX
from poetry.utils.env import EnvCommandError
from poetry.utils.env import EnvManager
from poetry.utils.env import NoCompatiblePythonVersionFound
Expand Down Expand Up @@ -74,6 +75,26 @@ def test_virtualenvs_with_spaces_in_their_path_work_as_expected(tmp_dir, manager
assert venv.run("python", "-V", shell=True).startswith("Python")


def test_env_commands_with_spaces_in_their_arg_work_as_expected(tmp_dir, manager):
venv_path = Path(tmp_dir) / "Virtual Env"
manager.build_venv(str(venv_path))
venv = VirtualEnv(venv_path)
assert venv.run("python", venv.pip, "--version", shell=True).startswith(
"pip {} from ".format(venv.pip_version)
)


def test_env_shell_commands_with_stdinput_in_their_arg_work_as_expected(
tmp_dir, manager
):
venv_path = Path(tmp_dir) / "Virtual Env"
manager.build_venv(str(venv_path))
venv = VirtualEnv(venv_path)
assert venv.run("python", "-", input_=GET_BASE_PREFIX, shell=True).strip() == str(
venv.get_base_prefix()
)


@pytest.fixture
def in_project_venv_dir(poetry):
os.environ.pop("VIRTUAL_ENV", None)
Expand Down