Skip to content

Commit c2be629

Browse files
Allow appending to deps with the command line (#3259)
* Support __iadd__ in PythonDeps * Allow appending to deps with -x testenv.deps+=foo Fixes: #3256 --------- Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
1 parent d28a9ee commit c2be629

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

docs/changelog/3256.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow appending to ``deps`` with ``--override testenv.deps+=foo`` - by :user:`stefanor`.

src/tox/config/loader/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import TYPE_CHECKING, Any, List, Mapping, TypeVar
66

77
from tox.plugin import impl
8+
from tox.tox_env.python.pip.req_file import PythonDeps
89

910
from .convert import Convert, Factory
1011
from .str_convert import StrConvert
@@ -148,6 +149,8 @@ def load( # noqa: PLR0913
148149
converted.update(converted_override)
149150
elif isinstance(converted, SetEnv) and isinstance(converted_override, SetEnv):
150151
converted.update(converted_override, override=True)
152+
elif isinstance(converted, PythonDeps) and isinstance(converted_override, PythonDeps):
153+
converted += converted_override
151154
else:
152155
msg = "Only able to append to lists and dicts"
153156
raise ValueError(msg)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ def unroll(self) -> tuple[list[str], list[str]]:
118118
self._unroll = result_opts, result_req
119119
return self._unroll
120120

121+
def __iadd__(self, other: PythonDeps) -> PythonDeps: # noqa: PYI034
122+
self._raw += "\n" + other._raw
123+
return self
124+
121125
@classmethod
122126
def factory(cls, root: Path, raw: object) -> PythonDeps:
123127
if not isinstance(raw, str):

tests/config/test_main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
from functools import partial
45
from pathlib import Path
56
from typing import TYPE_CHECKING, List
67

@@ -9,6 +10,7 @@
910
from tox.config.loader.api import Override
1011
from tox.config.loader.memory import MemoryLoader
1112
from tox.config.sets import ConfigSet
13+
from tox.tox_env.python.pip.req_file import PythonDeps
1214

1315
if TYPE_CHECKING:
1416
from tests.conftest import ToxIniCreator
@@ -98,6 +100,37 @@ def test_config_override_appends_to_empty_setenv(tox_ini_conf: ToxIniCreator) ->
98100
assert conf["setenv"].load("foo") == "bar"
99101

100102

103+
def test_config_override_appends_to_pythondeps(tox_ini_conf: ToxIniCreator, tmp_path: Path) -> None:
104+
example = """
105+
[testenv]
106+
deps = foo
107+
"""
108+
conf = tox_ini_conf(example, override=[Override("testenv.deps+=bar")]).get_env("testenv")
109+
conf.add_config(
110+
"deps",
111+
of_type=PythonDeps,
112+
factory=partial(PythonDeps.factory, tmp_path),
113+
default=PythonDeps("", root=tmp_path),
114+
desc="desc",
115+
)
116+
assert conf["deps"].lines() == ["foo", "bar"]
117+
118+
119+
def test_config_override_appends_to_empty_pythondeps(tox_ini_conf: ToxIniCreator, tmp_path: Path) -> None:
120+
example = """
121+
[testenv]
122+
"""
123+
conf = tox_ini_conf(example, override=[Override("testenv.deps+=bar")]).get_env("testenv")
124+
conf.add_config(
125+
"deps",
126+
of_type=PythonDeps,
127+
factory=partial(PythonDeps.factory, tmp_path),
128+
default=PythonDeps("", root=tmp_path),
129+
desc="desc",
130+
)
131+
assert conf["deps"].lines() == ["bar"]
132+
133+
101134
def test_config_override_cannot_append(tox_ini_conf: ToxIniCreator) -> None:
102135
example = """
103136
[testenv]

tests/tox_env/python/pip/test_req_file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ def test_opt_only_req_file(tmp_path: Path) -> None:
6767
python_deps = PythonDeps(raw="-rr.txt", root=tmp_path)
6868
assert not python_deps.requirements
6969
assert python_deps.options == Namespace(features_enabled=["fast-deps"])
70+
71+
72+
def test_req_iadd(tmp_path: Path) -> None:
73+
a = PythonDeps(raw="foo", root=tmp_path)
74+
b = PythonDeps(raw="bar", root=tmp_path)
75+
a += b
76+
assert a.lines() == ["foo", "bar"]

0 commit comments

Comments
 (0)