Skip to content

Commit 864332f

Browse files
authored
Fix space not accepted in factor filter expression (#2744)
Resolves #2718
1 parent 5e6f7e6 commit 864332f

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

docs/changelog/2718.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix space not accepted in factor filter expression - by :user:`gaborbernat`.

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ def explode_factor(group: list[tuple[str, bool]]) -> str:
4646
return "-".join([name for name, _ in group])
4747

4848

49-
def expand_factors(value: str) -> Iterator[tuple[Iterator[list[tuple[str, bool]]] | None, str]]:
49+
def expand_factors(value: str) -> Iterator[tuple[list[list[tuple[str, bool]]] | None, str]]:
5050
for line in value.split("\n"):
51-
match = re.match(r"^((?P<factor_expr>[\w{}.!,-]+):\s+)?(?P<content>.*?)$", line)
52-
if match is None: # pragma: no cover
53-
raise RuntimeError("for a valid factor regex this cannot happen")
54-
groups = match.groupdict()
55-
factor_expr, content = groups["factor_expr"], groups["content"]
56-
if factor_expr is not None:
57-
factors = find_factor_groups(factor_expr)
58-
yield factors, content
59-
else:
60-
yield None, content
51+
factors: list[list[tuple[str, bool]]] | None = None
52+
marker_at, content = line.find(":"), line
53+
if marker_at != -1:
54+
try:
55+
factors = list(find_factor_groups(line[:marker_at].strip()))
56+
except ValueError:
57+
pass # when cannot extract factors keep the entire line
58+
else:
59+
content = line[marker_at + 1 :].strip()
60+
yield factors, content
6161

6262

6363
def find_factor_groups(value: str) -> Iterator[list[tuple[str, bool]]]:
@@ -76,6 +76,8 @@ def expand_env_with_negation(value: str) -> Iterator[str]:
7676
parts = [re.sub(r"\s+", "", elem).split(",") for elem in elements]
7777
for variant in product(*parts):
7878
variant_str = "".join(variant)
79+
if not re.fullmatch(r"!?[\w._][\w._-]*", variant_str):
80+
raise ValueError(variant_str)
7981
yield variant_str
8082

8183

tests/config/loader/ini/replace/test_replace_posargs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def test_replace_pos_args(syntax: str, replace_one: ReplaceOne) -> None:
3737
[
3838
("magic", "magic"),
3939
("magic:colon", "magic:colon"),
40-
("magic\n b:c", "magic\nb:c"), # an unescaped newline keeps the newline
41-
("magi\\\n c:d", "magic:d"), # an escaped newline merges the lines
40+
("magic\n b c", "magic\nb c"), # an unescaped newline keeps the newline
41+
("magi\\\n c d", "magic d"), # an escaped newline merges the lines
4242
("\\{a\\}", "{a}"), # escaped curly braces
4343
],
4444
)

tests/config/loader/ini/test_factor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def complex_example() -> str:
2727
py: py only
2828
!py: not py
2929
{py,!pi}-{a,b}{,-dev},c: complex
30+
py, d: space
3031
extra: extra
3132
more-default
3233
""",
@@ -46,6 +47,7 @@ def test_factor_env_discover(complex_example: str) -> None:
4647
"pi-b",
4748
"pi-b-dev",
4849
"c",
50+
"d",
4951
"extra",
5052
]
5153

tests/session/cmd/test_list_envs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_list_env(project: ToxProject) -> None:
3636
3737
additional environments:
3838
fix -> fix it
39+
pypy -> with pypy
3940
"""
4041
outcome.assert_out_err(expected, "")
4142

@@ -62,6 +63,7 @@ def test_list_env_quiet(project: ToxProject) -> None:
6263
py31
6364
py
6465
fix
66+
pypy
6567
"""
6668
outcome.assert_out_err(expected, "")
6769

0 commit comments

Comments
 (0)