Skip to content

Commit 997128c

Browse files
authored
Better message when command parsing on empty input (#2807)
1 parent d202833 commit 997128c

File tree

6 files changed

+27
-1
lines changed

6 files changed

+27
-1
lines changed

docs/changelog/2695.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fail more gracefully when pip :ref:`install_command` is empty - by :user:`jayaddison`.

src/tox/config/loader/str_convert.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def to_command(value: str) -> Command:
6363
pos = splitter.instream.tell()
6464
except ValueError:
6565
args.append(value[pos:])
66+
if len(args) == 0:
67+
raise ValueError(f"attempting to parse {value!r} into a command failed")
6668
if args[0] != "-" and args[0].startswith("-"):
6769
args[0] = args[0][1:]
6870
args = ["-"] + args

src/tox/tox_env/python/pip/pip_install.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ def _execute_installer(self, deps: Sequence[Any], of_type: str) -> None:
160160
outcome.assert_success()
161161

162162
def build_install_cmd(self, args: Sequence[str]) -> list[str]:
163-
cmd: Command = self._env.conf["install_command"]
163+
try:
164+
cmd: Command = self._env.conf["install_command"]
165+
except ValueError as exc:
166+
raise Fail(f"unable to determine pip install command: {str(exc)}") from exc
164167
install_command = cmd.args
165168
try:
166169
opts_at = install_command.index("{packages}")

tests/config/loader/test_str_convert.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_str_convert_ok(raw: str, value: Any, of_type: type[Any]) -> None:
6262
("a", TypeVar, TypeError, r"a cannot cast to .*typing.TypeVar.*"),
6363
("3", Literal["1", "2"], ValueError, r"3 must be one of \('1', '2'\)"),
6464
("3", Union[str, int], TypeError, r"3 cannot cast to typing.Union\[str, int\]"),
65+
("", Command, ValueError, r"attempting to parse '' into a command failed"),
6566
],
6667
)
6768
def test_str_convert_nok(raw: str, of_type: type[Any], msg: str, exc_type: type[Exception]) -> None:

tests/session/cmd/test_show_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def test_show_config_exception(tox_project: ToxProjectCreator) -> None:
9999
assert txt in outcome.out
100100

101101

102+
def test_show_config_empty_install_command_exception(tox_project: ToxProjectCreator) -> None:
103+
project = tox_project({"tox.ini": "[testenv:a]\ninstall_command="})
104+
outcome = project.run("c", "-e", "a", "-k", "install_command")
105+
outcome.assert_success()
106+
txt = "\ninstall_command = # Exception: " "ValueError(\"attempting to parse '' into a command failed\")"
107+
assert txt in outcome.out
108+
109+
102110
@pytest.mark.parametrize("stdout_is_atty", [True, False])
103111
def test_pass_env_config_default(tox_project: ToxProjectCreator, stdout_is_atty: bool, mocker: MockerFixture) -> None:
104112
mocker.patch("sys.stdout.isatty", return_value=stdout_is_atty)

tests/tox_env/python/pip/test_pip_install.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from unittest.mock import Mock
77

88
import pytest
9+
from packaging.requirements import Requirement
910

1011
from tox.pytest import CaptureFixture, ToxProjectCreator
12+
from tox.tox_env.errors import Fail
1113

1214

1315
@pytest.mark.parametrize("arg", [object, [object]])
@@ -35,6 +37,15 @@ def test_pip_install_empty_list(tox_project: ToxProjectCreator) -> None:
3537
assert execute_calls.call_count == 0
3638

3739

40+
def test_pip_install_empty_command_error(tox_project: ToxProjectCreator) -> None:
41+
proj = tox_project({"tox.ini": "[testenv]\ninstall_command="})
42+
result = proj.run("l")
43+
pip = result.state.envs["py"].installer
44+
45+
with pytest.raises(Fail, match="unable to determine pip install command"):
46+
pip.install([Requirement("name")], "section", "type")
47+
48+
3849
def test_pip_install_flags_only_error(tox_project: ToxProjectCreator) -> None:
3950
proj = tox_project({"tox.ini": "[testenv:py]\ndeps=-i a"})
4051
result = proj.run("r")

0 commit comments

Comments
 (0)