Skip to content

Commit f8a88c1

Browse files
authored
Allow braced range syntax in internal sections of tox.ini file (#3631)
1 parent 6432872 commit f8a88c1

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

docs/changelog/3571.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expand braced range syntax in all internal sections of ``tox.ini`` (e.g. ``deps``, ``testenv``). Syntax like py3{10-14} can be used in those sections now.
2+
- by :user:`marcosboger`

docs/config.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,12 @@ Both enumerations (``{1,2,3}``) and numerical ranges (``{1-3}``) are supported,
16631663
[tox]
16641664
env_list = py3{8-10, 11, 13-14}
16651665
1666+
[testenv]
1667+
deps =
1668+
py{310,311-314}: urllib3
1669+
setenv =
1670+
py{310,311-314}: FOO=bar
1671+
16661672
will create the following envs:
16671673

16681674
.. code-block:: shell

src/tox/config/loader/ini/factor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def expand_factors(value: str) -> Iterator[tuple[list[list[tuple[str, bool]]] |
6464

6565
def find_factor_groups(value: str) -> Iterator[list[tuple[str, bool]]]:
6666
"""Transform '{py,!pi}-{a,b},c' to [{'py', 'a'}, {'py', 'b'}, {'pi', 'a'}, {'pi', 'b'}, {'c'}]."""
67+
value = expand_ranges(value)
6768
for env in expand_env_with_negation(value):
6869
result = [name_with_negate(f) for f in env.split("-")]
6970
yield result

src/tox/config/loader/str_convert.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from typing import TYPE_CHECKING, Any
1111

1212
from tox.config.loader.convert import Convert
13-
from tox.config.loader.ini.factor import expand_ranges
1413
from tox.config.types import Command, EnvList
1514

1615
if TYPE_CHECKING:
@@ -115,7 +114,6 @@ def to_command(value: str) -> Command | None:
115114
def to_env_list(value: str) -> EnvList:
116115
from tox.config.loader.ini.factor import extend_factors # noqa: PLC0415
117116

118-
value = expand_ranges(value)
119117
elements = list(chain.from_iterable(extend_factors(expr) for expr in value.split("\n")))
120118
return EnvList(elements)
121119

src/tox/config/source/ini_section.py

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

3-
from tox.config.loader.ini.factor import expand_ranges, extend_factors
3+
from tox.config.loader.ini.factor import extend_factors
44
from tox.config.loader.section import Section
55

66

@@ -15,7 +15,7 @@ def is_test_env(self) -> bool:
1515

1616
@property
1717
def names(self) -> list[str]:
18-
return list(extend_factors(expand_ranges(self.name)))
18+
return list(extend_factors(self.name))
1919

2020

2121
TEST_ENV_PREFIX = "testenv"

tests/config/loader/ini/test_factor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,40 @@ def test_ini_loader_raw_with_factors(
273273
assert outcome == result
274274

275275

276+
def test_generative_ranges_in_deps(tox_ini_conf: ToxIniCreator) -> None:
277+
config = tox_ini_conf(
278+
"""
279+
[testenv]
280+
deps-x =
281+
py{310-314}: black
282+
""",
283+
)
284+
assert list(config) == ["py310", "py311", "py312", "py313", "py314"]
285+
286+
287+
def test_generative_ranges_in_deps_with_mixed_approach(tox_ini_conf: ToxIniCreator) -> None:
288+
config = tox_ini_conf(
289+
"""
290+
[testenv]
291+
deps-x =
292+
py3{10-14}: black
293+
""",
294+
)
295+
assert list(config) == ["py310", "py311", "py312", "py313", "py314"]
296+
297+
298+
def test_generative_ranges_in_setenv(tox_ini_conf: ToxIniCreator) -> None:
299+
config = tox_ini_conf(
300+
"""
301+
[testenv]
302+
setenv =
303+
foo{1,2}: FOO=bar
304+
foo{3-5}: FOO=baz
305+
""",
306+
)
307+
assert list(config) == ["foo1", "foo2", "foo3", "foo4", "foo5"]
308+
309+
276310
def test_generative_section_name_with_ranges(tox_ini_conf: ToxIniCreator) -> None:
277311
config = tox_ini_conf(
278312
"""

0 commit comments

Comments
 (0)